redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

discriminator property must be required ?

Open champtar opened this issue 8 months ago • 5 comments

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

champtar avatar Mar 28 '25 13:03 champtar

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.

champtar avatar Mar 28 '25 14:03 champtar

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

jeremyfiel avatar Mar 31 '25 16:03 jeremyfiel

Should we output a warning then ? As of 3.1, it's clearly called out as undefined behavior when missing.

champtar avatar Mar 31 '25 16:03 champtar

You can write a custom configurable rule that checks the anyOf / oneOf schemas against the discriminating value

jeremyfiel avatar Apr 01 '25 15:04 jeremyfiel

@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.

tatomyr avatar Apr 07 '25 06:04 tatomyr

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

baywet avatar Sep 29 '25 18:09 baywet