Skip to content

Mixing string and int for code in @api.response can result in "duplicate mapping key" in swagger #661

@flayman

Description

@flayman

It took me far too long to track down this error, which was staring me in the face. If you inherit a resource and set a response on both the parent and child using the same code but inputting one as an int and the other as a string, you get both responses in the swagger.json, which doesn't parse. A simple example:

@api.route("/parent")
@api.response(404, 'Not found')
class Resource1(Resource):
    def get(self):
        return api.payload

@api.response('404', 'Also not found')
@api.route("/child")
class Resource2(Resource1):
    def get(self):
        """
        I have my own docstring.
        """
        return {}

swagger.json will contain:

{
    "/child": {
        "get": {
             "responses": {
                 "404": {
                     "description": "Not found"
                 },
                 "404": {
                     "description": "Also not found"
                 }
             }
        }
    }
}

Probably people should avoid doing this, but on the other hand the code should be coerced into a string before it is added to the dictionary, since it appears as a string key in the final json. That would solve the problem. If this is feasible, I'd also suggest that flask-restplus parses its swagger json when it starts up and raises an appropriate json parser error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions