Swagger - 无效的JSON错误

时间:2015-01-07 13:31:40

标签: json swagger swagger-ui

我刚刚开始使用Swagger UI,我正在努力了解它是如何工作的。 到目前为止,我已经输入了一些JSON(手动),这就是结果:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "PhakeApps API",
        "contact": {
            "name": "PhakeApps API team",
            "url": "http://phakeapps.com/"
        },
        "license": {
            "name": "Creative Commons 4.0 International",
            "url": "http://creativecommons.org/licenses/by/4.0/"
        }
    },
    "host": "api.phakeapps.com",
    "basePath": "/v1",
    "schemes": [
        "http"
    ],
    "produces": [
        "application/json"
    ],
    "consumes": [
        "application/json"
    ],
    "paths": {
        "/places/search": {
            "post": {
                "tags": [
                    "Places"
                ],
                "description": "Search for (a) place(s) <br /><br /> <b>id</b> - The ID of the request. <br /> <b>api_key</b> - API Key for the platform the request is sent. <i>Currently, not required.</i> <br /> <b>Params</b> - Required. <i>See model & model schema.</i>",
                "operationId": "PlacesSearch",
                "produces": [
                    "application/json"
                ],
                "consumes": [
                    "application/json"
                ],
                "parameters": [
                    {
                        "name": "request",
                        "in": "body",
                        "paramType": "body",
                        "description": "Object containing the <u>id</u>, <u>api_key</u> and certain <u>params</u>.",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/Search"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "Success",
                        "schema": {
                            "$ref": "#/definitions/PlacesResult"
                        }
                    },
                    "403": {
                        "description": "Validation error or Server Failure",
                        "schema": {
                            "$ref": "#/definitions/Error"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "PlacesResult": {
            "required": [
                "data",
                "id",
                "code"
            ],
            "properties": {
                "data": {
                    "$ref": "#/definitions/Places"
                },
                "id": {
                    "type": "integer",
                    "format": "int32"
                },
                "code": {
                    "type": "integer",
                    "format": "int32"
                }
            }
        },
        "Places": {
            "required": [
                "places"
            ],
            "properties": {
                "places": {
                    "$ref": "#/definitions/Place"
                }
            }
        },
        "City": {
            "required": [
                "id",
                "name"
            ],
            "properties": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "Neighbourhood": {
            "required": [
                "id",
                "name"
            ],
            "properties": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "Cuisine": {
            "required": [
                "id",
                "name"
            ],
            "properties": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                }
            }
        },
        "Place": {
            "required": [
                "id",
                "name",
                "city",
                "neighbourhood",
                "address",
                "cuisine",
                "price",
                "photos_cnt",
                "lat",
                "lng",
                "is_fav"
            ],
            "properties": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                },
                "city": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/City"
                    }
                },
                "neighbourhood": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Neighbourhood"
                    }
                },
                "address": {
                    "type": "string"
                },
                "cuisine": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Cuisine"
                    }
                },
                "price": {
                    "type": "integer",
                    "format": "int32"
                },
                "photos_cnt": {
                    "type": "integer",
                    "format": "int32"
                },
                "lat": {
                    "type": "double"
                },
                "lng": {
                    "type": "double"
                },
                "is_fav": {
                    "type": "boolean"
                }
            }
        },
        "Search": {
            "required": [
                "id",
                "api_key",
                "params"
            ],
            "properties": {
                "id": {
                    "type": "integer",
                    "format": "int64"
                },
                "api_key": {
                    "type": "string"
                },
                "params": {
                    "$ref": "#/definitions/SearchParams"
                }
            }
        },
        "SearchParams": {
            "required": [
                "user_id",
                "city_id",
                "people",
                "dt",
                "locale"
            ],
            "properties": {
                "user_id": {
                    "type": "string",
                    "default": "956dda4c21c72e48f5f17a7cd783a0f7"
                },
                "city_id": {
                    "type": "string",
                    "default": "4ec4b3e6098c9d23c925b0c2451eb06a"
                },
                "people": {
                    "type": "integer",
                    "format": "int32",
                    "minimum": 1,
                    "default": 2
                },
                "dt": {
                    "type": "integer",
                    "format": "int32",
                    "default": "1427742000"
                },
                "locale": {
                    "type": "string",
                    "default": "bg"
                },
                "place_id": {
                    "type": "string",
                    "default": "0"
                },
                "neighborhood_id": {
                    "type": "string",
                    "default": "0"
                },
                "cuisine_id": {
                    "type": "string",
                    "default": "0"
                },
                "kids_place": {
                    "type": "boolean",
                    "default": false
                },
                "price": {
                    "type": "integer",
                    "format": "int64",
                    "default": 1
                },
                "outdoors": {
                    "type": "boolean",
                    "default": false
                }
            }
        },
        "Error": {
            "required": [
                "code",
                "data"
            ],
            "properties": {
                "code": {
                    "type": "integer",
                    "format": "int32"
                },
                "data": {
                    "type": "array",
                    "items": {
                        "type": "array"
                    }
                }
            }
        }
    }
}

但是,swagger的验证员说它无效。我得到的错误就是这个

[
    {
        "level": "error",
        "domain": "validation",
        "keyword": "anyOf",
        "message": "instance failed to match at least one required schema among 2",
        "schema": {
            "loadingURI": "http://json-schema.org/draft-04/schema#",
            "pointer": "/properties/type"
        },
        "instance": {
            "pointer": "/definitions/Place/properties/lat/type"
        }
    }
]

请注意,它按预期工作(到目前为止)。它正确显示数据(模型和模型的结构)。发出请求并检索响应。然而验证人说它无效。 (黄色徽章上写着“无效”,而不是说“错误”的红色徽章)。

我错过了什么?

1 个答案:

答案 0 :(得分:4)

您的规格确实无效。在Place定义中,您使用"type": "double"来描述lat(以及lng)属性的类型,但这种类型不存在。

如果您想描述&#39; double&#39;的数值。大小,你应该改变定义如下:

            "lng": {
                "type": "number",
                "format": "double"
            }

在使用double类型的任何地方都这样做,它应解决该问题。