EugeneMi
EugeneMi

Reputation: 3575

How to ommit object fields from a post request in swagger

Let's say I have a user model defined with: id, first_name, last_name, created_at, and I have an object definition for it in swagger.

A POST request to create a user would expect first_name and last_name. How would I write that in swagger? Swagger expects a single object for the Should I create a new object just for the that?

Upvotes: 0

Views: 490

Answers (2)

dirtyqwerty
dirtyqwerty

Reputation: 305

How about

myuser:
  allOf:
  - $ref: '#/definitions/myuserbase'
  - type: object
    properties:
      id:
        description: |
          Unique identifier for the classifier.
        type: string
        example: user1
      created_at:
        description: |
          Date and time the user was created.
        type: string
        format: date-time          
    required:
      - id

and

myuserbase:
  type: object
    properties:
      first_name:
        description: |
          First name of the user
        type: string
      last_name:
        description: |
          Last name of the user
        type: string 
    required:
      - first_name
      - last_name

Upvotes: 1

William Cheng
William Cheng

Reputation: 10807

You can mark first_name, last_name as the required fields. Here is an example:

required:
  - first_name
  - last_name

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L658-L660

(in other words, no need to create a separate object)

Upvotes: 0

Related Questions