【问题标题】:Swagger specification for anything other than HTTP GET除了 HTTP GET 之外的任何东西的 Swagger 规范
【发布时间】:2015-06-04 22:51:32
【问题描述】:

我正忙于了解 swagger.json 规范的工作原理(在我的例子中是 api.json)。在研究过程中,我可以找到许多关于如何处理 GET 请求的示例,但对于 POST 或其他任何内容都没有。我迫切需要实现 POST 部分,但我觉得我需要更好地理解这一点,而不是复制和粘贴代码并依靠反复试验来使其工作。 Swagger.io 网站上的内容对初学者不友好。有人可以解释下面的示例代码中发生了什么,尤其是在两种情况下的“get:”之后:

{
"swagger": "2.0",
"info": {
    "version": "v1",
    "title": "FoodTrucks",
"description": "An TryApp sample API App where you can find Food trucks."
},
"host": "microsoft-apiapp1fe6951749ff4b76b8cc9194bc29ba61.azurewebsites.net:443",
"schemes": ["http", "https"],
"basePath": "/api/data",
"paths": {
    "/foodtrucks": {
        "get": {
            "tags": ["FoodTrucks"],
            "operationId": "getFoodTrucks",
            "consumes": [],
            "produces": ["application/json",
            "text/json",
            "application/xml",
            "text/xml"],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/Restaurant"
                        }
                    }
                }
            },
            "deprecated": false
        }
    },
    "/foodtrucks/{id}": {
        "get": {
            "tags": ["FoodTrucks"],
            "operationId": "getFoodTruckDetails",
            "consumes": [],
            "produces": ["application/json",
            "text/json",
            "application/xml",
            "text/xml"],
            "parameters": [{
                "name": "id",
                "in": "path",
                "required": true,
                "type": "integer",
                "format": "int32"
            }],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/Restaurant"
                        }
                    }
                }
            },
            "deprecated": false
        }
    }
},
"definitions": {
    "Restaurant": {
        "type": "object",
        "properties": {
            "id": {
                "format": "int32",
                "type": "integer"
            },
            "name": {
                "type": "string"
            },
            "likes": {
                "format": "int32",
                "type": "integer"
            },
            "savory": {
                "type": "boolean"
            },
            "sweet": {
                "type": "boolean"
            },
            "vegetarian": {
                "type": "boolean"
            },
            "bookable": {
                "type": "boolean"
            },
            "city": {
                "type": "string"
            }
        }
    }
 }
}

请您也提供一个简单的 POST 示例。

【问题讨论】:

    标签: rest http-post swagger swagger-editor swagger-2.0


    【解决方案1】:

    Swagger 的 extended examples 包含 POST。这是一个逐块解释的示例:

    "post": {
        "description": "Creates a new pet in the store.  Duplicates are allowed",
    

    描述是对操作的友好解释,主要来自documentation

        "operationId": "addPet",
    

    OperationId 是操作的友好名称。必须是独一无二的。

        "produces": [
          "application/json"
        ],
    

    Produces 是端点输出的 MIME 类型

        "parameters": [
          {
            "name": "pet",
            "in": "body",
            "description": "Pet to add to the store",
            "required": true,
            "schema": {
              "$ref": "#/definitions/NewPet"
    

    架构引用 ($ref) 指的是“定义”块中的数据类型定义。请参阅this JSON 中的 NewPet 部分。

            }
          }
        ],
    

    parameters block of the documentation 中最好地描述了参数。

        "responses": {
          "200": {
            "description": "pet response",
            "schema": {
              "$ref": "#/definitions/Pet"
            }
          },
    

    同样,回复最好在response documentation中描述

          "default": {
            "description": "unexpected error",
            "schema": {
              "$ref": "#/definitions/ErrorModel"
            }
          }
    

    默认是default response,如果没有其他返回。

        }
      }
    

    【讨论】:

    • 谢谢Pranspach。我认为我最大的问题实际上是 JSON 模式。你能给我发一些好的网站的链接来解释它是如何工作的吗?
    • 我没有链接,但在您的示例中,Restaurant 在“定义”部分中定义为具有属性 id、name 等的对象(想想 Restaurant.java)......他们通过指定格式(swagger 文档中的“数据类型”部分)“输入”。该餐厅端点将返回 [ { id:1, name:"restaurant", ... } ]
    • 我刚刚找到了问题的根源。当我提交 Ajax POST 时,我检查了 Chrome 中的 Javascript 控制台,我收到一条错误消息:“请求的资源中不存在‘Access-Control-Allow-Origin’标头”。我正在尝试弄清楚如何在服务器上启用 CORS,但很难找到任何可以执行此操作的资源。我正在使用 swaggerize-express,因为这是我在创建 Node.js Web API 时遵循的 Azure 示例中使用的内容。你知道怎么做吗?
    猜你喜欢
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多