Skip to content
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

Checking required fields doesn't honor discriminator #816

Open
johannes-riecken opened this issue Jun 28, 2023 · 0 comments
Open

Checking required fields doesn't honor discriminator #816

johannes-riecken opened this issue Jun 28, 2023 · 0 comments

Comments

@johannes-riecken
Copy link

I have a super-class ShapeBase, with objects looking like {"kind":"...","x":1,"y":2} and sub-classes Circle (e.g. {"kind":"Circle","x":1,"y":2,"r":3} and Rectangle (e.g. {"kind":"Rectangle","x":1,"y":2,"w":3,"h":4}).

Given that the discriminator declaration constrains that for a Shape of type Circle the "kind" must be "Circle", I'd assume the fields of Circle being required, and those of Rectangle not being required.

Input: {"kind":"Circle","x":1,"y":2}
Expected: Validation fails, Actual: Validation fails🟢

Input: {"kind":"Circle","x":1,"y":2,"w":3}
Expected: Validation fails, Actual: Validation fails🟢

Input: {"kind":"Circle","x":1,"y":2,"w":3,"h":4}
Expected: Validation fails, Actual: Validation passes🔴

I used the example request validation from the README and the following openapi.yml:

openapi: 3.0.0
info:
  title: "foo"
  version: "0.0.1"
  license:
    url: "https://www.example.com"
    name: "MIT"
servers:
  - url: "localhost:8080"
paths:
  /shapes:
    summary: 'all the shapes'
    description: 'for CRUD'
    get:
      responses:
        default:
          description: 'list all shapes'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Shape'
    post:
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Shape'
      responses:
        default:
          description: 'create a shape'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Shape'
components:
  schemas:
    Shape:
      type: object
      oneOf:
        - $ref: '#/components/schemas/Circle'
        - $ref: '#/components/schemas/Rectangle'
      discriminator:
        propertyName: kind
    ShapeBase:
      type: object
      properties:
        kind:
          type: string
        x:
          type: integer
        y:
          type: integer
      required: ['kind', 'x', 'y']
    Circle:
      type: object
      allOf:
        - type: object
          properties:
            r:
              type: integer
          required: ['r']
        - $ref: '#/components/schemas/ShapeBase'
    Rectangle:
      type: object
      allOf:
        - properties:
            w:
              type: integer
            h:
              type: integer
          required: ['w', 'h']
        - $ref: '#/components/schemas/ShapeBase'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant