openapi-examples-validator icon indicating copy to clipboard operation
openapi-examples-validator copied to clipboard

Validation and polymorphism

Open gigaga opened this issue 4 years ago • 2 comments

Hi,

first, thank a lot for your magic tool. It is really great to be able to validate our examples!! However, I have a question about polymorphism. Indeed, can you tell me how validate examples with polymorphism based.

For example, with the following spec :

openapi: 3.0.2
info:
  title: toto
  version: 1.0.7
paths:
  "/pet":
    post:
      tags:
      - pet
      summary: Add a new pet to the store
      description: Add a new pet to the store
      operationId: addPet
      requestBody:
        description: Create a new pet in the store
        content:
          application/json:
            schema:
              oneOf:
              - $ref: "#/components/schemas/Cat"
              - $ref: "#/components/schemas/Dog"
            examples:
              dog:
                value:
                  name: "12"
                  dogName: 123
                  type: "DOG"
        required: true
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                "$ref": "#/components/schemas/Pet"
        '405':
          description: Invalid input
components:
  schemas:
    Dog:
      allOf:
        - "$ref": "#/components/schemas/Pet"
        - "type": object
          properties:
            dogName:
              type: string
    Cat:
      allOf:
        - "$ref": "#/components/schemas/Pet"
        - "type": object
          properties:
            catName:
              type: string
    Pet:
      required:
      - name
      type: object
      properties:
        id:
          type: integer
          format: int64
          example: 10
        name:
          type: string
          example: doggie
        type:
          type: string
      discriminator:
        propertyName: type
        mapping:
          DOG: "#/components/schemas/Dog"
          CAT: "#/components/schemas/Cat"

We have an endpoint to create several "Pet", so, we have an abstract schema "Pet" with others schema which inherits of Pet, like "Dog". So we are trying to provide "Dog" creation but, in this case, property dogName is invalid but openapi-examples-validator doesn't see that.

Can you help me ?

Regards,

gigaga avatar Nov 25 '21 15:11 gigaga

TL;DR

Polymorphism isn't supported yet.

More detailed explanation

The OEV (openapi-examples-validator) uses ajv for validation, under the hood. The discriminator-option has been added with a "more recent" version of AJV (v8.6.1).

OEV still uses v6.12.6 of AJV, because starting with version 7 the support for JSON Schema draft 4 has been dropped, which the OpenAPI-specification (prior to version 3.1.0) relies on.

While there are ways to support draft-04 with a more recent version of AJV, more (potential breaking) changes will be necessary. This is why it hasn't been upgraded yet. Of course, it will be done eventually but I can't tell you yet when I'll get to do this.

codekie avatar Nov 25 '21 16:11 codekie

Thx for these explanations. I'm going to try to implement it my self and I will make a PR if any.

Regards

gigaga avatar Nov 25 '21 16:11 gigaga

Merged with #185 @gigaga thanks, for your contribution

codekie avatar May 22 '23 20:05 codekie