0

This question relates to Open API spec 3 and writing API specifications.

I have been trying to work out how to set fields as required for a specific method requestBody when I am using a "oneOf" in a schema object.

I know how to set required fields of a referenced object, but when you try set the required properties of oneOf objects the same way, it does not work.

I also know that I can set the required fields in the schema object itself, however I do not want to do this as the required fields differ on each method (i.e all fields required for a post, but only some required for a patch).

So essentially, how do I set the required fields in a request body of a schema used in oneOf?

I have created an example below, in it, I want to:

  • Set the fields Bird - wingSpan and beakLength as required in the Post method
  • Set the fields Cat - tailLength as required in the Post method
  • Not have any required fields for Bird and Cat in the Patch method.
openapi: 3.0.3
servers:
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/Enable-Networks/TroubleTicket/1.0.0

info:
  title: Pet API
  version: 1.0.0

paths:
  /pet:
    patch:
      summary: Update a pet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              allOf:
                - $ref: "#/components/schemas/Pet"
                - type: object
                  required:
                    - age
      responses:
        "204":
          description: The request was a success and the pet was successfully created.

    post:
      summary: Create a pet
      requestBody:
        required: true
        content:
          application/json:
            schema:
              allOf:
                - $ref: "#/components/schemas/Pet"
                - type: object
                  required:
                    - name
                    - age
                    - species
                  properties:
                    species:
                      type: object
                      required:
                        - ???
      responses:
        "204":
          description: The request was a success and the pet was successfully created.

components:
  schemas:
    Cat:
      type: object
      properties:
        tailLength: 
          type: integer
        whiskerLength:
          type: integer
    Bird:
      type: object 
      properties:
        wingSpan:
          type: integer
        beakLength:
          type: integer
        highestAltitude:
          type: integer

    Pet:
      type: object
      properties:
        name:
          type: string
        age:
          type: string
        species:
          oneOf:
            - $ref: '#/components/schemas/Cat'
            - $ref: '#/components/schemas/Bird'

Any help on this would be appreciated!

I have read the documentation 10x, tried everything from discriminators to not using refs.

I have tried many different combinations of the below and still nothing works.

 requestBody:
    required: true
    content:
      application/json:
        schema:
          allOf:
            - $ref: "#/components/schemas/Pet"
            - type: object
              required:
                - name
                - age
                - species
              properties:
                species:
                  type: object
                  required:
                    - ???

0 Answers0