如何在OpenAPI(Swagger)中描述此POST JSON请求主体?

Ani*_*ary 42 swagger swagger-2.0 openapi

我有一个POST请求,它使用以下JSON请求体.如何使用OpenAPI(Swagger)描述此请求体?

{
    "testapi": {
        "testapiContext": {
            "messageId": "kkkk8",
            "messageDateTime": "2014-08-17T14:07:30+0530"
        },
        "testapiBody": {
            "cameraServiceRq": {
                "osType": "android",
                "deviceType": "samsung555"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我尝试了以下内容,但我仍然坚持定义身体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"
             }
Run Code Online (Sandbox Code Playgroud)

我想将输入体内联定义,作为文档的示例.

Ani*_*ary 39

我使用它:

    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
Run Code Online (Sandbox Code Playgroud)


Ull*_*uri 9

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
...
Run Code Online (Sandbox Code Playgroud)


Ant*_*hon 5

将多行标量包含到 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:
.
.
Run Code Online (Sandbox Code Playgroud)

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