Swagger POST中需要六个字段中的两个[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我正在尝试编写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,然后只写出需要哪些字段。

有谁知道哪条路是对的?

swagger
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.