-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
301 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import type { OpenAPIV3, OpenAPIV3_1 } from 'openapi-types'; | ||
import type { z } from 'zod'; | ||
|
||
import { convertZodToJsonSchema } from './zod-to-json'; | ||
|
||
const createComponentMap = ( | ||
schemas: Record<string, OpenAPIV3.SchemaObject | OpenAPIV3_1.SchemaObject>, | ||
) => { | ||
const map = new Map<string, string>(); | ||
|
||
Object.entries(schemas).forEach(([key, value]) => map.set(JSON.stringify(value), key)); | ||
|
||
return map; | ||
}; | ||
|
||
const createComponentReplacer = (componentMapVK: Map<string, string>, schemasObject: object) => | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function componentReplacer(this: any, key: string, value: any) { | ||
if (typeof value !== 'object') return value; | ||
|
||
// Check if the parent is the schemas object, if so, return the value as is. This is where the schemas are defined. | ||
if (this === schemasObject) return value; | ||
|
||
const stringifiedValue = JSON.stringify(value); | ||
if (componentMapVK.has(stringifiedValue)) | ||
return { $ref: `#/components/schemas/${componentMapVK.get(stringifiedValue)}` }; | ||
|
||
if (value.nullable === true) { | ||
const nonNullableValue = { ...value }; | ||
delete nonNullableValue.nullable; | ||
const stringifiedNonNullableValue = JSON.stringify(nonNullableValue); | ||
if (componentMapVK.has(stringifiedNonNullableValue)) | ||
return { | ||
anyOf: [ | ||
{ $ref: `#/components/schemas/${componentMapVK.get(stringifiedNonNullableValue)}` }, | ||
], | ||
nullable: true, | ||
}; | ||
} | ||
|
||
return value; | ||
}; | ||
|
||
export const resolveRefs = ( | ||
openapiObject: Partial<OpenAPIV3.Document | OpenAPIV3_1.Document>, | ||
zodSchemas: Record<string, z.ZodTypeAny>, | ||
) => { | ||
const schemas: Record<string, OpenAPIV3.SchemaObject | OpenAPIV3_1.SchemaObject> = {}; | ||
for (const key in zodSchemas) { | ||
schemas[key] = convertZodToJsonSchema(zodSchemas[key]); | ||
} | ||
|
||
const document = { | ||
...openapiObject, | ||
components: { | ||
...openapiObject.components, | ||
schemas: { | ||
...openapiObject.components?.schemas, | ||
...schemas, | ||
}, | ||
}, | ||
}; | ||
|
||
const componentMapVK = createComponentMap(schemas); | ||
const componentReplacer = createComponentReplacer(componentMapVK, document.components.schemas); | ||
|
||
// Using the componentReplacer function we deep check if the document has any schemas that are the same as the zod schemas provided | ||
// When a match is found replace them with a $ref. | ||
return JSON.parse(JSON.stringify(document, componentReplacer)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { z } from 'zod'; | ||
import { zodToJsonSchema } from 'zod-to-json-schema'; | ||
|
||
const zodToJsonSchemaOptions = { | ||
target: 'openApi3', | ||
$refStrategy: 'none', | ||
} as const; | ||
|
||
export const convertZodToJsonSchema = (zodSchema: z.ZodTypeAny) => { | ||
return zodToJsonSchema(zodSchema, zodToJsonSchemaOptions); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.