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(わかりやすくするために、スカラー キーの前に追加の改行を 1 つまたは 2 つ置くことができます。これらは、リテラル ブロック スタイルのスカラーではデフォルトで切り取られます。

于 2015-07-14T06:10:12.817 に答える