Openapi-specification: Question: 'required' fields based on context

Created on 24 Apr 2018  路  4Comments  路  Source: OAI/OpenAPI-Specification

Hi,

here is the definition of Dog:

Dog:
  properties:
    name:
      type: string
    breed:
      type: string

there is no required fields.

Now I would like to reference the Dog definition in different contexts and want add/modify required parameters on Dog definition. Something like this:

LoadRequest:
  type: object
  properties:
    required:
      - **breed**
    dog:
      $ref: "#/definitions/Dog"

SaveRequest:
  type: object
  properties:
    required:
      - **breed**
      - **name**
    dog:
      $ref: "#/definitions/Dog"

Main idea is to have one Object definition and many references with different required fields. Is possible to define required field based on reference somehow? Or somehow else? I know that one solution is to define three Dogs :/ - every Dog with different required fields. But it is lot of duplication and in real the object Dog is much more bigger. Any idea?

Thanks
kolisko

Most helpful comment

There is a similar query here.

You can use the allOf keyword to extend your Dog definition. Assuming LoadRequest and saveRequest are Dogs, rather than having dog as a property:

LoadRequest:
  allOf:
    - $ref: "#/definitions/Dog"
    - required:
      - breed

SaveRequest:
  allOf:
    - $ref: "#/definitions/Dog"
    - required:
      - breed
      - name

In essence, allOf requires that the instance validates against all of the schemas in the given array. The first (the $ref to Dog) requires that if present, name and breed are strings, but neither are required. The second schema in the array enforces that the given properties must be present (but not what types they are). In combination, they give you the validation you're looking for.

In OpenAPI 3.0.x #/definitions/... would be #/components/schemas/... (assuming a self-contained OAS document).

All 4 comments

There is a similar query here.

You can use the allOf keyword to extend your Dog definition. Assuming LoadRequest and saveRequest are Dogs, rather than having dog as a property:

LoadRequest:
  allOf:
    - $ref: "#/definitions/Dog"
    - required:
      - breed

SaveRequest:
  allOf:
    - $ref: "#/definitions/Dog"
    - required:
      - breed
      - name

In essence, allOf requires that the instance validates against all of the schemas in the given array. The first (the $ref to Dog) requires that if present, name and breed are strings, but neither are required. The second schema in the array enforces that the given properties must be present (but not what types they are). In combination, they give you the validation you're looking for.

In OpenAPI 3.0.x #/definitions/... would be #/components/schemas/... (assuming a self-contained OAS document).

In my API definition I'm using allOf and required just as @MikeRalphson suggested. Everything is working great so far, but I have a question about requiring nested properties. Let's say that in the example above, that the breed property is actually another object that has properties for breed_name, size, and breed_origin.

How would I be able to specify, using the strategy above, that the nested property breed_name is required?

The original question in this issue was answered in 2018, subsequent questions if still relevant should be filed as their own issues.

Was this page helpful?
0 / 5 - 0 ratings