codekie / openapi-examples-validator

Validates embedded examples in OpenAPI-files
MIT License
57 stars 11 forks source link

Validation and polymorphism #155

Closed gigaga closed 1 year ago

gigaga commented 2 years ago

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,

codekie commented 2 years ago

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.

gigaga commented 2 years ago

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

Regards

codekie commented 1 year ago

Merged with #185 @gigaga thanks, for your contribution