codekie / openapi-examples-validator

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

discriminator does not support allOf #187

Closed wcloete closed 1 year ago

wcloete commented 1 year ago

Hi since https://github.com/codekie/openapi-examples-validator/pull/185 I have an API that is failing validation. It uses the discriminator keyword on the parent definition and allOf on the children.

Example

This was adapted from the OAS documentation for discriminator

API spec:

openapi: 3.0.3
info:
  title: An API for a cat.
  version: 0.1.9
paths:
  /cat:
    get:
      responses:
        '200':
          description: It is just a cat.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Cat'
              examples:
                ex1:
                  value:
                    petType: "Cat"
                    name: "misty"
components:
  schemas:
    Pet:
      type: object
      required:
      - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
    Lizard:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Lizard`
        properties:
          lovesRocks:
            type: boolean

Validate with:

openapi-examples-validator openapi.yaml

Error:

Validating examples
Schemas with examples found: 1
Examples without schema found: 0
Total examples found: 1

Errors found.

[
    {
        "type": "Validation",
        "message": "discriminator: requires oneOf keyword",
        "examplePath": "/paths/~1cat/get/responses/200/content/application~1json/examples/ex1/value"
    }
]

Other info

Version: openapi-examples-validator@5.0.0 Operating system: https://hub.docker.com/_/node, image ID acd15857ce39 OpenAPI version: 3.0.3

codekie commented 1 year ago

@wcloete Thank you for your report, but I use Ajv under the hood for the validation and it only supports oneOf with the discriminator keyword (yet), so there's not much I can do about it at this point.

codekie commented 1 year ago

@wcloete I just had a closer look at your example again.. Your example is not how the discriminator is supposed to be used (see also the Discriminator-section).

If you just want to make a model composition, you don't need the discriminator. Drop the discriminator it in your example and it works.

wcloete commented 1 year ago

Strange, because as I say, I took the schemas almost verbatim from the docs - my only change was to remove mapping and add an endpoint.

The paragraph above the example even says

In both the oneOf and anyOf use cases, all possible schemas MUST be listed explicitly. To avoid redundancy, the discriminator MAY be added to a parent schema definition, and all schemas comprising the parent schema in an allOf construct may be used as an alternate schema.

But as you say, Ajv only supports oneOf according to their documentation, so this one will keep failing until they add support.

Thank you for your time, and for a nice library!

codekie commented 1 year ago

I was about to post an example with a oneOf, a discriminator and a allOf example, but then figured that it doesn't work due to this bug: https://github.com/ajv-validator/ajv/issues/2261 , but then saw that you already saw https://github.com/ajv-validator/ajv/issues/2281 😉 .. I have a watch on 2261 and when the corresponding pull request is merged, I'll look into it again.

codekie commented 1 year ago

I can't promise that this will resolve your issue, but 2261 really is a problem as that is a common use case.

wcloete commented 1 year ago

Yes, it looks like 2261 covers what I need. I'll keep an eye out for updates too.