56

文書化するRESTサービスがあり、そのうちのいくつかは次のような単純な配列を受け入れます:

[
  { "name":"a" },
  { "name":"b" },
  { "name":"c" }
]

これを Swagger モデル セクションでどのように説明すればよいですか? 次のような「名前付き配列」しか作成できません

model {
properties: { "arr": { "type":"array", ......

しかし、それは次のようなデータを説明しています:

"arr": [
  { "name":"a" },
  { "name":"b" },
  { "name":"c" }
]
4

7 に答える 7

22

Tony YUEN は近くにいましたが、葉巻はありませんでした。これは、OpenAPI/Swagger で YAML を使用する適切な定義です。

  /test:
post:
  summary: test 123
  description: test 123
  parameters:
    - name: param1
      in: body
      required: true
      description: test param1
      schema:
          $ref: '#/definitions/stackoverflow'
  responses:
    200:
      description: OK

これにより、次が生成されます。

stackoverflow2[
  {
     name: string
  }
]

Tony の例では次のようになります。

[
  stackoverflow { 
                 name: string
  }
]

Swagger/OpenAPI を YAML として完成 (コピー & ペースト)

    swagger: '2.0'

################################################################################
#                              API Information                                 #
################################################################################
info:
  version: "Two-point-Oh!"
  title: Simple objects in array test
  description: |
    Simple objects in array test

################################################################################
#                                   Parameters                                 #
################################################################################

paths:
  /test:
    post:
      summary: Array with named objects
      description: Array with named objects
      parameters:
        - name: param1
          in: body
          required: true
          description: test param1
          schema:
            type: array
            items:
              $ref: '#/definitions/stackoverflow'
      responses:
        200:
          description: OK
  /test2:
    post:
      summary: Array with simpel (nameless) objects
      description: Array with simpel (nameless)  objects
      parameters:
        - name: param1
          in: body
          required: true
          description: test param1
          schema:
              $ref: '#/definitions/stackoverflow2'
      responses:
        200:
          description: OK
definitions:
  stackoverflow:
    type: object
    properties:
      name:
        type: string
        description: name of the object
  stackoverflow2:
    type: array
    items:
      type: object
      properties:
        name:
          type: string
          description: name of the object

これは、Swagger/OpenAPI の JSON バージョンです。

    {
  "swagger" : "2.0",
  "info" : {
    "description" : "Simple objects in array test\n",
    "version" : "Two-point-Oh!",
    "title" : "Simple objects in array test"
  },
  "paths" : {
    "/test" : {
      "post" : {
        "summary" : "Array with named objects",
        "description" : "Array with named objects",
        "parameters" : [ {
          "in" : "body",
          "name" : "param1",
          "description" : "test param1",
          "required" : true,
          "schema" : {
            "type" : "array",
            "items" : {
              "$ref" : "#/definitions/stackoverflow"
            }
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "OK"
          }
        }
      }
    },
    "/test2" : {
      "post" : {
        "summary" : "Array with simpel (nameless) objects",
        "description" : "Array with simpel (nameless)  objects",
        "parameters" : [ {
          "in" : "body",
          "name" : "param1",
          "description" : "test param1",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/stackoverflow2"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "OK"
          }
        }
      }
    }
  },
  "definitions" : {
    "stackoverflow" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string",
          "description" : "name of the object"
        }
      }
    },
    "stackoverflow2" : {
      "type" : "array",
      "items" : {
        "$ref" : "#/definitions/stackoverflow2_inner"
      }
    },
    "stackoverflow2_inner" : {
      "properties" : {
        "name" : {
          "type" : "string",
          "description" : "name of the object"
        }
      }
    }
  }
}
于 2016-11-14T11:04:12.973 に答える
12

これをhttp://editor.swagger.io/#/に貼り付けて 、「この操作を試す」をクリックします

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Privacy-Service API"
  },
  "paths": {
    "/allNames": {
      "post": {
        "consumes": [
          "application/json",
          "application/xml"
        ],
        "produces": [
          "application/json",
          "application/xml"
        ],
        "parameters": [
          {
            "name": "request",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/ArrayOfNames"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of names",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "ArrayOfNames": {
      "type": "array",
      "items": {
        "minItems": 1,
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}
于 2016-12-21T21:20:58.427 に答える
0

私の理解が正しければ、次のことが必要になると思います。

あなたが言及したように、

それらのいくつかは単純な配列を受け入れます

次に、パラメーターを介して渡されます。

"parameters" : [{
  "name" : "param_name",
  "type" : "array",
  "items" : {
    "$ref" : "M"
  }
  ...
}]
...

モデル セクションの場合:

"models" : {
  "M": {
    "id" : "M",
    "properties": {
       "name" : {
         "type" : "string"
       }
    }
  }
于 2013-11-29T08:09:37.063 に答える