Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swagger - Invalid JSON errors

I'm just starting with Swagger UI and I'm trying to understand how it works. So far I've entered some JSON (manually) and this is the result:

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

However, swagger's validator says it's not valid. The error I get is this

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

Note that it works as expected (so far). It displays the data (models and models' structure) properly. Make requests and retrieves responses. Yet the validator says it's not valid. (The yellow badge saying 'Invalid', not the red one that says 'Error').

What am I missing?

like image 990
Ivanka Todorova Avatar asked Jul 16 '26 02:07

Ivanka Todorova


1 Answers

Your spec is indeed not valid. In your Place definition, you use "type": "double" to describe the type of the lat (and also lng) property, but such a type does not exist.

If you want to describe a numeric value of 'double' size, you should change the definition as follows:

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

Do that everywhere you use the double type, and it should resolve that issue.

like image 170
Ron Avatar answered Jul 17 '26 16:07

Ron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!