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

How to find discriminator targets #3591

Closed
floitsch opened this issue Feb 18, 2024 · 2 comments
Closed

How to find discriminator targets #3591

floitsch opened this issue Feb 18, 2024 · 2 comments
Assignees
Labels
clarification requests to clarify, but not change, part of the spec discriminator
Milestone

Comments

@floitsch
Copy link

I just implemented a JSON-Schema validator and would like to add OpenAPI support to it.

I'm now stuck at adding discriminators with the "allOf" variant.
According to the spec a "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."

However, I can't find the definition of "parent". The term "parent" in JSON Schema seems to mean something unrelated in JSON Schema, and I didn't see anything in the OpenAPI spec. I'm assuming it is the "parent" in the inheritance hierarchy, but that's not something that is well defined in JSON Schema (afaik).

Also: I can't just do this dynamically (waiting to encounter an discriminator), as I need to have the mappings targets statically. After, all, I might get the generic schema as "type" and then use the discriminator to validate the more specialized one.

There are a few cases that are obvious:

    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
...

According to #2165 (comment) an allOf of an allOf is also a valid target.

components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Reptile:
      allOf:
        - $ref: '#/components/schemas/Pet'
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Reptile'
        - type: object
          properties:
            lovesRocks:
              type: boolean

What about a reference in an if?

components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
    Reptile:
      if: true  # Or some complex schema.
      then:
        allOf:
          - $ref: '#/components/schemas/Pet'

I'm guessing that one isn't valid, but my gut instinct would have said the same for the allOf of the allOf.

There are also some interesting cases:
If I have a schema in a different schema-resource (for example 'https://gigantic-server.com/schemas/Monster') that references my discriminator schema with "allOf", then clearly I can't find that schema. However, what if that same schema-resource is loaded through other means?
Does that schema then count as a target for the mapping?

Roughly:

# Let's give this the URI "URI-DISCRIMINATOR"
Pet:
  type: object
  required:
    - petType
  properties:
    petType:
      type: string
  discriminator:
    propertyName: petType
Unrelated:
  # An unrelated schema references the external schema resource.
  if: https://gigantic-server.com/schemas/Monster

# With https://gigantic-server.com/schemas/Monster:
all-of:
  - $ref: URI-DISCRIMINATOR/Pet
  ...

It feels like finding all possible schemas that have the discriminator in it can become quite complicated.
Is there a description somewhere of how I can find all possible targets?

@handrews handrews added discriminator clarification requests to clarify, but not change, part of the spec labels Feb 18, 2024
@handrews
Copy link
Member

I'm not aware of any comprehensive explanation. If the OpenAPI Description is a single document, then my assumption is that the entire schemas section of the Components Object should be searched for schemas that use allOf to reference the schema with the discriminator. If your description is multiple documents, it's ambiguous as to which Components document should be searched.

@handrews
Copy link
Member

handrews commented Jun 6, 2024

PRs merged! closing.

@handrews handrews closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarification requests to clarify, but not change, part of the spec discriminator
Projects
None yet
Development

No branches or pull requests

2 participants