redocly-cli
redocly-cli copied to clipboard
discriminator property must be required ?
Describe the bug
I'm not certain, but I think the discriminator property must be required
https://spec.openapis.org/oas/v3.1.0.html#fixed-fields-20
The expectation now is that a property with name petType MUST be present in the response payload, and the value will correspond to the name of a schema defined in the OAS document.
To Reproduce
Here an example
openapi: 3.1.0
info:
title: test
version: v1
servers:
- url: /
paths:
/aaa:
get:
operationId: aaa
summary: aaa
responses:
"200":
description: successful operation
content:
application/json:
schema:
$ref: "#/components/schemas/objX"
components:
schemas:
objstr:
type: object
#required: [objectType]
properties:
objectType:
type: string
enum: ["str"]
objint:
type: object
#required: [objectType]
properties:
objectType:
type: string
enum: ["int"]
objX:
anyOf:
- $ref: "#/components/schemas/objstr"
- $ref: "#/components/schemas/objint"
discriminator:
propertyName: objectType
mapping:
"int": "#/components/schemas/objint"
"str": "#/components/schemas/objstr"
Expected behavior
Validation fails because we are missing required: [objectType]
Additional context
oapi-codegen fails to generate correct code with this example, but I'm not 100% clear if it is valid or not https://github.com/oapi-codegen/oapi-codegen/pull/1789#issuecomment-2759047235
If it's not valid it would be great if redocly-cli was rejecting such example
OpenAPI 3.1.1 is clearer: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.1.md#fixed-fields-22
This property SHOULD be required in the payload schema, as the behavior when the property is absent is undefined.
Forcing it to be required is against the spec language, so while it's not totally incorrect to assume that, an optional definition does not violate the spec
this behavior will slightly change with the 3.2 minor release coming soon, hopefully by Summer 25.
https://github.com/OAI/OpenAPI-Specification/pull/4339
Should we output a warning then ? As of 3.1, it's clearly called out as undefined behavior when missing.
You can write a custom configurable rule that checks the anyOf / oneOf schemas against the discriminating value
@champtar, as @jeremyfiel said, you'll need to write a custom rule using plugin if you want to use that now. You can check out our cookbook as well for more practical examples.
I think #2320 gives us a way forward here. I'd be happy to address this issue in a follow up PR assuming this one gets merged in first and we agree that this is something that needs to be fixed. CC @jeremyfiel