1

I'm looking for some advice with my Open API 3.0 Spec, This is an example of my User schema object. I'm hoping to make some of the fields optional somehow but I'm not sure how to define them.

When I paste the full spec in https://editor.swagger.io it displays everything in the 'Try it out' function. Some of the other models are larger so it can be quite cumbersome deleting all the related fields, maybe they could show fieldName: {} so you know they're there available? I'm not sure how this should be handled.

This schema object is used for POST, PUT and GET as a $ref object, I'm trying to keep it DRY as possible.

It maybe also worth noting that due to tooling not working quite right just yet (or maybe it's my spec) I'm converting the end built result to swagger 2.0 to generate Swift 4 and Angular Typescript libraries.

Also it's worth noting I call these extra fields with the api by using an includes query string param e.g. users?includes=person,person.telephone,createdByUser,repStats etc... So I only want them when included in a GET request.

See the commented fields below.

User:
  type: object
  properties:
    uuid:
      type: string
    username:
      type: string
    password:
      type: string
      format: password
    email:
      type: string
    personUuid:
      type: string
    createdAt:
      type: string
      format: date-time
    updatedAt:
      type: string
      format: date-time
    deletedAt:
      type: string
      format: date-time
    createdByUuid:
      type: string
    updatedByUuid:
      type: string
    # Relations.
    # I want these read only and optional on GET requests.
    # They can be there on PUT/POST requests, the server will simply
    # ignore them.
    person:
      $ref: '#/components/schemas/Person'
    createdByUser:
      $ref: '#/components/schemas/User'
    updatedByUser:
      $ref: '#/components/schemas/User'
    repStats:
      $ref: '#/components/schemas/RepStats'
louisl
  • 99
  • 1
  • 10
  • 1
    All properties are [optional by default](https://stackoverflow.com/q/40113049/113116). As for read-only, see [How to declare a $ref property as readOnly in OpenAPI (Swagger)?](https://stackoverflow.com/q/51402156/113116). – Helen Dec 07 '18 at 11:05

0 Answers0