Swagger POST所需的六个字段中的两个

时间:2018-07-23 18:43:18

标签: swagger

我正在尝试编写Swagger文档,在POST请求中需要五个字段中的两个。我已经阅读了一些有关如何执行此操作的选项,但似乎找不到确切的答案。

选项1

/my-endpoint:
post:
  x-swagger-router-controller: example.spec
  operationId: example_operationId
  parameters:
  - in: body
    name: recovery_data
    description: TODO
    schema:
      $ref: '#/definitions/RecoveryData'
    required: true
  responses:
    200:
      description: TODO
      schema:
        $ref: '#/definitions/OkResponse'

RecoveryData:
type: object
properties:
  required_field_1:
    description: TODO
    type: array
    required: true
  required_field_2:
    description: TODO
    type: string
    required: true
  optional_field_1:
    description: TODO
    type: boolean
    required: false
  optional_field_2:
    description: TODO
    type: boolean
    required: false
  optional_field_3:
    description: TODO
    type: boolean
    required: false
  optional_field_4:
    description: TODO
    type: string
    required: false

选项2

 /my-endpoint:
post:
  x-swagger-router-controller: example.spec
  operationId: example_operationId
  parameters:
  - in: body
    name: recovery_data
    description: TODO
    schema:
      $ref: '#/definitions/RecoveryData'
  responses:
    200:
      description: TODO
      schema:
        $ref: '#/definitions/OkResponse'

RecoveryData:
type: object
properties:
  required_field_1:
    description: TODO
    type: array
    required: true
  required_field_2:
    description: TODO
    type: string
    required: true
  optional_field_1:
    description: TODO
    type: boolean
  optional_field_2:
    description: TODO
    type: boolean
  optional_field_3:
    description: TODO
    type: boolean
  optional_field_4:
    description: TODO
    type: string

在选项1中,我在参数:请求的一部分中指定required: true,然后写出架构中实际需要或不需要的参数。在选项2中,我的参数中没有required: true,然后仅写出需要哪些字段。

有人知道哪种方法是对的吗?

1 个答案:

答案 0 :(得分:0)

这与问题How to specify if a field is optional or required in swagger?

有点重复

答案似乎是:

RecoveryData:
  type: object
  required:
    - required_field_1
    - required_field_2
  properties:
    required_field_1:
      description: TODO
      type: array
    required_field_2:
      description: TODO
      type: string
    optional_field_1:
      description: TODO
      type: boolean
    optional_field_2:
      description: TODO
      type: boolean
    optional_field_3:
      description: TODO
      type: boolean
    optional_field_4:
      description: TODO
      type: string