Skip to content

Optional property per operation #1935

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

Closed
marcortw opened this issue May 28, 2019 · 2 comments
Closed

Optional property per operation #1935

marcortw opened this issue May 28, 2019 · 2 comments

Comments

@marcortw
Copy link

I am trying to reuse a schema similar to the 'reusable bodies' section described here, with the difference that I'd like to reuse the same schema in the requestBody and in the response:

  /pets:
    post:
      summary: Creates a new pet
      requestBody:
        content:
          '*/*':
            schema:
              $ref: '#/components/schemas/PetBody'
        required: true
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/PetBody'

This works, but I'd like to make an exception for an 'id' property of the PetBody. Assuming that a Pet's unique identifier gets created after the POST on the server, I wonder how I could split or compose the PetBody schema such that an 'id' could be marked required: true only in the response?

Something like this works, but I'd also like to reference the id parameter and possibly a few more through additional references which doesn't seem to work according to #1467

    PetBodyResponse:
      allOf:
        - $ref: '#/components/schemas/PetBody'
        - type: object
          properties:
            id:
              type: string

Thanks in advance for any good suggestions,
Marco

@hkosova
Copy link
Contributor

hkosova commented May 31, 2019

There's no need for separate schemas. You can use a single PetBody schema and define id as required and readOnly: true:

Declares the property as "read only". This means that it MAY be sent as part of a response but SHOULD NOT be sent as part of the request. If the property is marked as readOnly being true and is in the required list, the required will take effect on the response only.

@marcortw
Copy link
Author

Great, this is exactly what I needed! Thank you very much for the hint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants