I'm currently using the zodios-openapi library in conjunction with the @asteasolutions/zod-to-openapilibrary to generate OpenAPI schemas from Zod schemas. The @asteasolutions/zod-to-openapi library provides an openapi()method that allows adding OpenAPI metadata to Zod schemas. However, the makeJsonSchema function in zodios-openapi does not currently recognise or utilise this metadata.
To work around this, I've been accessing the _def property directly in the makeJsonSchema function to retrieve the OpenAPI metadata:
function makeJsonSchema(schema: z.ZodTypeAny) {
const jsonSchema = zodToJsonSchema(schema, {
target: "openApi3",
$refStrategy: "none",
}) as OpenAPIV3.SchemaObject;
if ((schema as any)._def.openapi) {
const openapi = (schema as any)._def.openapi;
Object.assign(jsonSchema, openapi);
}
return jsonSchema;
}
While this approach works, it relies on the internal structure of the Zod schemas and could potentially break in future versions of the Zod or @asteasolutions/zod-to-openapi libraries.
I propose adding support for the openapi() method from @asteasolutions/zod-to-openapi in the makeJsonSchema function. This would allow developers to add OpenAPI metadata to their Zod schemas using the openapi() method, and the makeJsonSchema function would automatically include this metadata when generating the JSON schema.
This change would provide a consistent way for developers to add OpenAPI metadata to their schemas, and it would avoid the need to modify the Zod schemas directly or access internal properties.
That being said, this change would still involve accessing the internal _def property directly, which might not be considered 'best practise' but short of adding support in Zod for metadata consisting of examples etc, this is a workaround.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I'm currently using the
zodios-openapi
library in conjunction with the@asteasolutions/zod-to-openapi
library to generate OpenAPI schemas from Zod schemas. The@asteasolutions/zod-to-openapi
library provides anopenapi()
method that allows adding OpenAPI metadata to Zod schemas. However, themakeJsonSchema
function in zodios-openapi does not currently recognise or utilise this metadata.To work around this, I've been accessing the
_def
property directly in themakeJsonSchema
function to retrieve the OpenAPI metadata:While this approach works, it relies on the internal structure of the Zod schemas and could potentially break in future versions of the Zod or
@asteasolutions/zod-to-openapi
libraries.I propose adding support for the
openapi()
method from@asteasolutions/zod-to-openapi
in themakeJsonSchema
function. This would allow developers to add OpenAPI metadata to their Zod schemas using theopenapi()
method, and themakeJsonSchema
function would automatically include this metadata when generating the JSON schema.This change would provide a consistent way for developers to add OpenAPI metadata to their schemas, and it would avoid the need to modify the Zod schemas directly or access internal properties.
That being said, this change would still involve accessing the internal
_def
property directly, which might not be considered 'best practise' but short of adding support in Zod for metadata consisting of examples etc, this is a workaround.