-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
ddd955f
commit ef9128d
Showing
8 changed files
with
168 additions
and
29 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,74 @@ | ||
import zodToJsonSchema from "zod-to-json-schema"; | ||
import type { SchemaObject } from "@omer-x/openapi-types/schema"; | ||
import type { ZodType } from "zod"; | ||
|
||
function deepEqual(a: unknown, b: unknown): boolean { | ||
if (typeof a !== typeof b) return false; | ||
switch (typeof a) { | ||
case "object": { | ||
if (a === null) return a === b; | ||
if (!b) return false; | ||
if (Array.isArray(a)) { | ||
if (!Array.isArray(b)) return false; | ||
return a.every((item, index) => deepEqual(item, b[index])); | ||
} | ||
return Object.entries(a).every(([key, value]) => deepEqual(value, (b as Record<string, unknown>)[key])); | ||
} | ||
case "function": | ||
case "symbol": | ||
return false; | ||
default: | ||
return a === b; | ||
} | ||
} | ||
|
||
export default function maskWithReference( | ||
schema: SchemaObject, | ||
storedSchemas: Record<string, ZodType>, | ||
self: boolean | ||
): SchemaObject { | ||
if (self) { | ||
for (const [schemaName, zodSchema] of Object.entries(storedSchemas)) { | ||
if (deepEqual(schema, zodToJsonSchema(zodSchema))) { | ||
return { | ||
$ref: `#/components/schemas/${schemaName}`, | ||
}; | ||
} | ||
} | ||
} | ||
if ("$ref" in schema) return schema; | ||
if (schema.oneOf) { | ||
return { | ||
...schema, | ||
oneOf: schema.oneOf.map(i => maskWithReference(i, storedSchemas, true)), | ||
}; | ||
} | ||
if (schema.anyOf) { | ||
return { | ||
...schema, | ||
anyOf: schema.anyOf.map(i => maskWithReference(i, storedSchemas, true)), | ||
}; | ||
} | ||
switch (schema.type) { | ||
case "object": | ||
return { | ||
...schema, | ||
properties: Object.entries(schema.properties).reduce((props, [propName, prop]) => ({ | ||
...props, | ||
[propName]: maskWithReference(prop, storedSchemas, true), | ||
}), {}), | ||
}; | ||
case "array": | ||
if (Array.isArray(schema.items)) { | ||
return { | ||
...schema, | ||
items: schema.items.map(i => maskWithReference(i, storedSchemas, true)), | ||
}; | ||
} | ||
return { | ||
...schema, | ||
items: maskWithReference(schema.items, storedSchemas, true), | ||
}; | ||
} | ||
return schema; | ||
} |
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,57 @@ | ||
import maskWithReference from "./mask"; | ||
import type { MediaTypeObject } from "@omer-x/openapi-types/media-type"; | ||
import type { OperationObject } from "@omer-x/openapi-types/operation"; | ||
import type { ParameterObject } from "@omer-x/openapi-types/parameter"; | ||
import type { ReferenceObject } from "@omer-x/openapi-types/reference"; | ||
import type { RequestBodyObject } from "@omer-x/openapi-types/request-body"; | ||
import type { ResponseObject, ResponsesObject } from "@omer-x/openapi-types/response"; | ||
import type { SchemaObject } from "@omer-x/openapi-types/schema"; | ||
import type { ZodType } from "zod"; | ||
|
||
function maskSchema(storedSchemas: Record<string, ZodType>, schema?: SchemaObject) { | ||
if (!schema) return schema; | ||
return maskWithReference(schema, storedSchemas, true); | ||
} | ||
|
||
function maskParameterSchema(param: ParameterObject | ReferenceObject, storedSchemas: Record<string, ZodType>) { | ||
if ("$ref" in param) return param; | ||
return { ...param, schema: maskSchema(storedSchemas, param.schema) } as ParameterObject; | ||
} | ||
|
||
function maskContentSchema(storedSchemas: Record<string, ZodType>, bodyContent?: Record<string, MediaTypeObject>) { | ||
if (!bodyContent) return bodyContent; | ||
return Object.entries(bodyContent).reduce((collection, [contentType, content]) => ({ | ||
...collection, | ||
[contentType]: { | ||
...content, | ||
schema: maskSchema(storedSchemas, content.schema), | ||
}, | ||
}), {} as Record<string, MediaTypeObject>); | ||
} | ||
|
||
function maskRequestBodySchema(storedSchemas: Record<string, ZodType>, body?: RequestBodyObject | ReferenceObject) { | ||
if (!body || "$ref" in body) return body; | ||
return { ...body, content: maskContentSchema(storedSchemas, body.content) } as RequestBodyObject; | ||
} | ||
|
||
function maskResponseSchema(storedSchemas: Record<string, ZodType>, response: ResponseObject | ReferenceObject) { | ||
if ("$ref" in response) return response; | ||
return { ...response, content: maskContentSchema(storedSchemas, response.content) }; | ||
} | ||
|
||
function maskSchemasInResponses(storedSchemas: Record<string, ZodType>, responses?: ResponsesObject) { | ||
if (!responses) return responses; | ||
return Object.entries(responses).reduce((collection, [key, response]) => ({ | ||
...collection, | ||
[key]: maskResponseSchema(storedSchemas, response), | ||
}), {} as ResponsesObject); | ||
} | ||
|
||
export default function maskOperationSchemas(operation: OperationObject, storedSchemas: Record<string, ZodType>) { | ||
return { | ||
...operation, | ||
parameters: operation.parameters?.map(p => maskParameterSchema(p, storedSchemas)), | ||
requestBody: maskRequestBodySchema(storedSchemas, operation.requestBody), | ||
responses: maskSchemasInResponses(storedSchemas, operation.responses), | ||
} as OperationObject; | ||
} |
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 |
---|---|---|
@@ -1,14 +1,20 @@ | ||
import zodToJsonSchema from "zod-to-json-schema"; | ||
import maskWithReference from "./mask"; | ||
import type { SchemaObject } from "@omer-x/openapi-types/schema"; | ||
import type { ZodType } from "zod"; | ||
|
||
export function bundleSchemas(schemas: Record<string, ZodType>) { | ||
return Object.keys(schemas).reduce((collection, schemaName) => { | ||
const bundledSchemas = Object.keys(schemas).reduce((collection, schemaName) => { | ||
return { | ||
...collection, | ||
[schemaName]: zodToJsonSchema(schemas[schemaName], { | ||
target: "openApi3", | ||
}), | ||
} as Record<string, SchemaObject>; | ||
}, {} as Record<string, SchemaObject>); | ||
|
||
return Object.entries(bundledSchemas).reduce((bundle, [schemaName, schema]) => ({ | ||
...bundle, | ||
[schemaName]: maskWithReference(schema, schemas, false), | ||
}), {} as Record<string, SchemaObject>); | ||
} |
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