Validation Request: Enforce refs in OpenAPI Discriminated Unions #364
-
Issue DescriptionWhen using discriminated unions with OpenAPI, the specification requires all union members to use Current BehaviorConsider this example: const apple = z.object({
type: z.literal('apple'),
color: z.string(),
});
const orange = z
.object({
type: z.literal('orange'),
diameter: z.number(),
})
.openapi({ ref: 'Orange' });
const data = z.discriminatedUnion('type', [apple, orange]); This generates an OpenAPI spec without the {
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "apple"
},
"color": {
"type": "string"
}
},
"required": [
"type",
"color"
]
},
{
"$ref": "#/components/schemas/Orange"
}
]
} Desired BehaviorI propose adding validation to throw an error when generating OpenAPI specs for discriminated unions where any member lacks a ref. This would help prevent the common mistake of adding a new union member without the required The discriminator property works correctly when all members have refs: const apple = z.object({
type: z.literal('apple'),
color: z.string(),
}).openapi({ ref: 'Apple' }); // Added ref
const orange = z.object({
type: z.literal('orange'),
diameter: z.number(),
}).openapi({ ref: 'Orange' });
const data = z.discriminatedUnion('type', [apple, orange]); QuestionIs it possible to add this validation to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I'm open to making it an option eg. https://github.com/samchungy/zod-openapi#createdocumentoptions but I don't think I want to throw an error by default as Zod discriminated unions can accept more permutations than JSON Schema/OpenAPI is compatible with |
Beta Was this translation helpful? Give feedback.
4.1.0
https://github.com/samchungy/zod-openapi/releases/tag/v4.1.0