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

discriminator does not support allOf #187

Closed
wcloete opened this issue May 24, 2023 · 6 comments
Closed

discriminator does not support allOf #187

wcloete opened this issue May 24, 2023 · 6 comments

Comments

@wcloete
Copy link

wcloete commented May 24, 2023

Hi since #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: [email protected]
Operating system: https://hub.docker.com/_/node, image ID acd15857ce39
OpenAPI version: 3.0.3

@codekie
Copy link
Owner

codekie commented May 25, 2023

@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 codekie closed this as completed May 25, 2023
@codekie
Copy link
Owner

codekie commented May 25, 2023

@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
Copy link
Author

wcloete commented May 25, 2023

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
Copy link
Owner

codekie commented May 25, 2023

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: ajv-validator/ajv#2261 , but then saw that you already saw ajv-validator/ajv#2281 😉 .. I have a watch on 2261 and when the corresponding pull request is merged, I'll look into it again.

@codekie
Copy link
Owner

codekie commented May 25, 2023

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

@wcloete
Copy link
Author

wcloete commented May 26, 2023

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

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

2 participants