如何在swagger中从post请求中省略对象字段

时间:2016-10-07 01:46:01

标签: swagger

假设我有一个用户模型定义:id,first_name,last_name,created_at,我在swagger中有一个对象定义。

创建用户的POST请求需要first_name和last_name。我怎么会大摇大摆呢? Swagger期望单个对象我应该为此创建一个新对象吗?

2 个答案:

答案 0 :(得分:1)

怎么样

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

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

答案 1 :(得分:0)

您可以将first_namelast_name标记为必填字段。这是一个例子:

required:
  - first_name
  - last_name

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

(换句话说,不需要创建单独的对象)