如何基于OpenAPI 3.0中的路径参数更改requestBody类型?

时间:2019-12-08 10:29:01

标签: openapi

根据Swagger send body and formData parameterOpenAPI 2.0 Specification,在OpenAPI 2.0中,in: bodyin: formData参数不能同时用于同一操作。那是有道理的。

但是我正在使用OpenAPI 3.0,我想知道是否有一种方法可以根据路径参数切换requestBody?当storepath1时,requestBody应该将内容与application/json一起使用;当storepath2时,requestBody应该使用带有multipart/form-data的内容。

  /customs/{store}:
    post:
      description: Customs server calls Nomad to receive the filing result of one
        order
      operationId: post_customs_callback
      parameters:
      - description: ID of the store.
        explode: true
        in: path
        name: store
        required: true
        schema:
          type: string
        style: simple
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              type: object
              properties: # Request parts
                openReq:
                  type: string

#          application/json:
#            schema:
#              type: string
        description: Order details

1 个答案:

答案 0 :(得分:1)

OpenAPI规范没有基于特定参数值更改请求/响应正文的方法。但是,在您的方案中,您可以改用两个非参数化路径– /customs/path1用于JSON请求,/customs/path2用于多部分请求。

openapi: 3.0.0
...

paths:
  /customs/path1:
    post:
      ...
      requestBody:
        required: true
        content:
          application/json:
            schema:
              ...

  /customs/path2:
    post:
      ...
      requestBody:
        required: true
        content:
          multipart/form-data:
            schema:
              ...