Skip to content

Commit

Permalink
fix(opapi): create a deepcopy of the schema before setting openapi pr…
Browse files Browse the repository at this point in the history
…operties (#288)
  • Loading branch information
franklevasseur committed May 6, 2024
1 parent 30bf1fb commit 8479609
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion opapi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bpinternal/opapi",
"version": "0.10.14",
"version": "0.10.15",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
13 changes: 9 additions & 4 deletions opapi/src/opapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ import { exportStateAsTypescript, ExportStateAsTypescriptOptions } from './gener
import { generateHandler } from './handler-generator'
export { Operation, Parameter } from './state'

type ExtendApi = typeof extendApi
type AnatineSchemaObject = NonNullable<Parameters<ExtendApi>[1]>
type AnatineSchemaObject = NonNullable<Parameters<typeof extendApi>[1]>

export const schema: <T extends OpenApiZodAny>(schema: T, schemaObject?: AnatineSchemaObject & { $ref?: string }) => T =
extendApi
export const schema = <T extends OpenApiZodAny>(
schema: T,
schemaObject?: AnatineSchemaObject & { $ref?: string },
): T => {
const This = (schema as any).constructor
const copy = new This(schema._def) as T
return extendApi(copy, schemaObject)
}

export type OpenApi<
SchemaName extends string = string,
Expand Down
2 changes: 1 addition & 1 deletion opapi/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ export function createState<SchemaName extends string, DefaultParameterName exte
}

if (schemas[name]) {
throw new VError(`Operation ${name} already exists`)
throw new VError(`Schema ${name} already exists`)
}

schemas[name] = {
Expand Down

0 comments on commit 8479609

Please sign in to comment.