How to describe a model in Swagger for an array with simple objects?

后端 未结 7 2040
长发绾君心
长发绾君心 2020-12-15 15:30

I have a REST services to document, some of them accepts simple array like:

[
  { \"name\":\"a\" },
  { \"name\":\"b\" },
  { \"name\":\"c\" }
]
7条回答
  •  一整个雨季
    2020-12-15 16:12

    Paste this to http://editor.swagger.io/#/ and click on "try this operation"

    {
      "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"
              }
            }
          }
        }
      }
    }
    

提交回复
热议问题