kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

Checking required fields doesn't honor discriminator

Open johannes-riecken opened this issue 1 year ago • 0 comments

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'

johannes-riecken avatar Jun 28 '23 09:06 johannes-riecken