45

我有一个使用以下 JSON 请求正文的 POST 请求。如何使用 OpenAPI (Swagger) 描述此请求正文?

{
  "testapi":{
    "testapiContext":{
      "messageId":"kkkk8",
      "messageDateTime":"2014-08-17T14:07:30+0530"
    },
    "testapiBody":{
      "cameraServiceRq":{
        "osType":"android",
        "deviceType":"samsung555"
      }
    }
  }
}

到目前为止,我尝试了以下方法,但我坚持定义 body schema

swagger: "2.0"
info:
  version: 1.0.0
  title: get camera
  license:
    name: MIT
host: localhost
basePath: /test/service
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /getCameraParameters:
    post:
      summary: Create new parameters
      operationId: createnew
      consumes:
        - application/json
        - application/xml
      produces:
        - application/json
        - application/xml
      parameters:
        - name: pet
          in: body
          description: The pet JSON you want to post
          schema:  # <--- What do I write here?
            
          required: true
      responses: 
        200: 
          description: "200 response"
          examples: 
            application/json: 
             {
               "status": "Success"
             }

我想将输入正文内联定义为文档示例。

4

3 回答 3

43

我使它与:

    post:
      consumes:
        - application/json
      produces:
        - application/json
        - text/xml
        - text/html
      parameters:
        - name: body
          in: body
          required: true
          schema:
            # Body schema with atomic property examples
            type: object
            properties:
              testapi:
                type: object
                properties:
                  messageId:
                    type: string
                    example: kkkk8
                  messageDateTime:
                    type: string
                    example: '2014-08-17T14:07:30+0530'
              testapiBody:
                type: object
                properties:
                  cameraServiceRq:
                    type: object
                    properties:
                      osType:
                        type: string
                        example: android
                      deviceType:
                        type: string
                        example: samsung555
            # Alternatively, we can use a schema-level example
            example:
              testapi:
                testapiContext:
                  messageId: kkkk8
                  messageDateTime: '2014-08-17T14:07:30+0530'
                testapiBody:
                  cameraServiceRq:
                    osType: android
                    deviceType: samsung555
于 2015-07-21T06:39:08.873 回答
4

将多行标量包含到 YAML 中的最易读的方法是使用块文字样式。这要求您仅通过使用缩进来更改 JSON 示例(如果您检索键的值,缩进将被删除):

.
.
produces:
  - application/json
example: |
  {
      "testapi": {
          "testapiContext": {
              "messageId": "kkkk8",
              "messageDateTime": "2014-08-17T14:07:30+0530"
     },
          "testapiBody": {
              "cameraServiceRq": {
                  "osType": "android",
                  "deviceType": "samsung555"
              }
          }
      }
  }
paths:
  /getCameraParameters:
.
.

paths(为了清楚起见,您可以在标量键之前放置一两个额外的换行符,默认情况下它们会在文字块样式标量上被剪裁。

于 2015-07-14T06:10:12.817 回答
0

openapiversion >=3.0.0允许使用requestBody,这将允许在parameters.

在您的情况下,它看起来像这样:

...
      requestBody:
        description: The pet JSON you want to post
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                testapi:
                  type: object
                  properties:
                    messageId:
                      type: string
                      example: kkkk8
                    messageDateTime:
                      type: string
                      example: '2014-08-17T14:07:30+0530'
                testapiBody:
                  type: object
                  properties:
                    cameraServiceRq:
                      type: object
                      properties:
                        osType:
                          type: string
                          example: android
                        deviceType:
                          type: string
                          example: samsung555
              example:
                testapi:
                  testapiContext:
                    messageId: kkkk8
                    messageDateTime: '2014-08-17T14:07:30+0530'
                  testapiBody:
                    cameraServiceRq:
                      osType: android
                      deviceType: samsung555
...
于 2021-07-30T17:42:51.000 回答