Skip to content

How to find discriminator targets #3591

@floitsch

Description

@floitsch

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?

Metadata

Metadata

Assignees

Labels

clarificationrequests to clarify, but not change, part of the specdiscriminator

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions