Skip to content

Question on nested oneOf definition #2260

@sonic-kaka

Description

@sonic-kaka

Using the following nested oneOf definition as an example

mammal:
  oneOf:
    - $ref: '#/components/schemas/whale'
    - $ref: '#/components/schemas/zebra'
    - $ref: '#/components/schemas/pig'
whale:
  type: object
  properties:
    hasBaleen:
      type: boolean
    hasTeeth:
      type: boolean
  required:
    - hasBaleen
    - hasTeeth
pig:
  oneOf:
    - $ref: '#/components/schemas/basque_pig'
    - $ref: '#/components/schemas/danish_pig'
basque_pig:
  type: object
  properties:
    color:
      type: string
  required:
    - color
danish_pig:
  type: object
  properties:
    weight:
      type: double
  required:
    - weight

UPDATE (JUN 12): I've removed the classname property to avoid confusion on the correctness of the object.

The API server's endpoint returns mammal (not provided in the spec above). If let's say the API client (in Java for example) submits an HTTP request to the endpoint and the server returns a danish_pig, e.g.

{
  "weight": 32.1
}

One can further process the object returned by the server and send it back (via another endpoint expecting mammal in the payload). For example, switch the payload from danish_pig to basque_pig and send it to the server, which should accepts it without issue. Switching the payload from danish_pig to something else (e.g. bird, snake) should result in an error in the client side if the validation is performed or the server should return an error as bird, snake do not conform to the mammal definition.

Here is the question: should the client obtain the danish_pig object directly? Or should it obtain pig object directly? Or actually the return object should be mammal object instead?

Thank you for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions