toobulkeh
toobulkeh

Reputation: 1616

Reusable component outside of components/schema?

Is there another place to put reusable components outside of components/schema?

I have a "partial" (for lack of a better term) that I'd like to $ref elsewhere, but I do not want as a "Model":

Some examples:

NotFoundError:
  type: object
  properties:
    code:
      type: integer
      format: int32
      example: 404
    message:
      type: string
      example: "Resource Not Found"
ID-Array:
  type: array
  items:
    $ref: '#/components/schemas/ID'
  writeOnly: true
ID:
  type: integer
  format: int32
  example: 1
ID-ReadOnly:
  allOf:
    - $ref: '#/components/schemas/ID'
    - readOnly: true
ID-WriteOnly:
  allOf:
    - $ref: '#/components/schemas/ID'
    - writeOnly: true

Upvotes: 1

Views: 216

Answers (1)

Helen
Helen

Reputation: 97590

A possible solution is to put the partials under some extension key, say x-partials, and change the $refs accordingly. $ref does not care what the path is as long as the target is in the expected format.

x-partials:
  ID:
    type: integer
    format: int32
    example: 1

components:
  schemas:
    ID-Array:
      type: array
      items:
        $ref: '#/x-partials/ID'
      writeOnly: true
    ID-ReadOnly:
      allOf:
        - $ref: '#/x-partials/ID'
        - readOnly: true
    ID-WriteOnly:
      allOf:
        - $ref: '#/x-partials/ID'
        - writeOnly: true

This way, for example, the partials won't show up in the "Models" section of Swagger UI.

Upvotes: 1

Related Questions