Open marceloverdijk opened 4 months ago
So just to elaborate, I want to setup the schema so this is generated into the openapi spec file:
"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": {
"$ref": "#/components/schemas/CircuitType",
"description": "The circuit type.",
"example": "STREET"
},
..
},
"required": [..],
"description": "Represents a circuit."
},
..
@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 allOf
logic since what you suggest:
"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 example
to the CircuitType
schema definition and for the type
field in the Circuit
schema it does not use anyOf
but just the "type": { "$ref": "#/components/schemas/CircuitType" }
.
I checked my project and it uses the latest 7.1.1
:
"node_modules/@asteasolutions/zod-to-openapi": {
"version": "7.1.1",
"resolved": "https://registry.npmjs.org/@asteasolutions/zod-to-openapi/-/zod-to-openapi-7.1.1.tgz",
but my setup is different. I'm targeting openapi spec 3.1 and I'm using Cloudflare Chanfana. Maybe that's causing some impact as Chanfana is using zod/openapi as well (https://github.com/asteasolutions/zod-to-openapi/issues/234#issuecomment-2198022860)
I'm also facing another (blocking) issue now (https://github.com/asteasolutions/zod-to-openapi/issues/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?