-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to document a field, but not change the top level schema type? #245
Comments
So just to elaborate, I want to setup the schema so this is generated into the openapi spec file:
|
@marceloverdijk thank you for bringing this up. However I am unable to reproduce. Locally what I test with is: import { z } from 'zod';
import { extendZodWithOpenApi } from './zod-extensions';
import { OpenApiGeneratorV3 } from './v3.0/openapi-generator';
extendZodWithOpenApi(z);
export const CircuitTypeSchema = z
.enum(['RACE', 'ROAD', 'STREET'])
.openapi('CircuitType', { description: 'Represents a circuit type.' });
export const CircuitSchema = z
.object({
id: z
.string()
.openapi({ description: 'The unique identifier.', example: 'melbourne' }),
name: z
.string()
.openapi({ description: 'The name.', example: 'Melbourne' }),
type: CircuitTypeSchema.openapi({
description: 'The circuit type.',
example: 'STREET',
}),
})
.openapi('Circuit', { description: 'Represents a circuit.' });
const generator = new OpenApiGeneratorV3([CircuitTypeSchema, CircuitSchema]);
const doc = generator.generateDocument({} as never);
console.log(JSON.stringify(doc, null, 4)); And the resulting JSON looks like this: {
"components": {
"schemas": {
"CircuitType": {
"type": "string",
"enum": [
"RACE",
"ROAD",
"STREET"
],
"description": "Represents a circuit type."
},
"Circuit": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "The unique identifier.",
"example": "melbourne"
},
"name": {
"type": "string",
"description": "The name.",
"example": "Melbourne"
},
"type": {
"allOf": [
{
"$ref": "#/components/schemas/CircuitType"
},
{
"description": "The circuit type.",
"example": "STREET"
}
]
}
},
"required": [
"id",
"name",
"type"
],
"description": "Represents a circuit."
}
},
"parameters": {}
},
"paths": {}
} We've implemented the "type": {
"$ref": "#/components/schemas/CircuitType",
"description": "The circuit type.",
"example": "STREET"
}, is not valid according to the OpenAPI specification. Can you please double check your example |
Interesting and thx for your feedback. In my case it overwrites the description and add the I checked my project and it uses the latest
but my setup is different. |
I'm also facing another (blocking) issue now (#234) so I will create a minimal reproducible project that I can share which will showcase both issues. |
When I have a schema like:
this will generate a openapi spec like:
which is good except, that I explicitly want to set the
description
andexample
for theCircuit.type
field (which is now not specified.So I tried with:
which unfortunately does not change the
Circuit.type
but the top-levelCircuitType
schema only:and the
Circuit.type
:Is there a way to document (openapi) the
Circuit.type
field, bit not affect the top-level schema type?The text was updated successfully, but these errors were encountered: