我该如何使用Swagger的模型部分?

19
在Swagger API文档中,除了API数组之外还有一个模型对象条目,但没有关于它的文档说明。我该如何使用这个“models”部分?
{
   apiVersion: "0.2",
   swaggerVersion: "1.1",
   basePath: "http://petstore.swagger.wordnik.com/api",
   resourcePath: "/pet.{format}"

   ...

   apis: [...]
   models: {...}
}
1个回答

18

模型就像Java中的POJO类一样,具有变量和属性。在模型部分,您可以定义自己的自定义类,并将其引用为数据类型。

如果您看下面

     {
        "path": "/pet.{format}",
        "description": "Operations about pets",
        "operations": [
            {
                "httpMethod": "POST",
                "summary": "Add a new pet to the store",
                "responseClass": "void",
                "nickname": "addPet",
                "parameters": [
                    {
                        "description": "Pet object that needs to be added to the store",
                        "paramType": "body",
                        "required": true,
                        "allowMultiple": false,
                        "dataType": "Pet"
                    }
                ],
                "errorResponses": [
                    {
                        "code": 405,
                        "reason": "Invalid input"
                    }
                ]
            }

在参数部分,有一个参数,其dataTypePet,pet在模型中定义如下:

{
"models": {
    "Pet": {
        "id": "Pet",
        "properties": {
            "id": {
                "type": "long"
            },
            "status": {
                "allowableValues": {
                    "valueType": "LIST",
                    "values": [
                        "available",
                        "pending",
                        "sold"
                    ]
                },
                "description": "pet status in the store",
                "type": "string"
            },
            "name": {
                "type": "string"
            },
            "photoUrls": {
                "items": {
                    "type": "string"
                },
                "type": "Array"
            }
        }
    }
}}

您可以拥有嵌套模型,有关更多信息,请参见Swagger PetStore示例

因此,模型就像类一样。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接