flask-restful-swagger icon indicating copy to clipboard operation
flask-restful-swagger copied to clipboard

API prefix does not work

Open NMO13 opened this issue 5 years ago • 0 comments

I have an app with a URL prefix:

api = Api(app, prefix="/api/v1")

And a simple view:


class AnimalView(Resource):
    @swagger.operation(notes="get an animal by ID", )
    def get(self, animal_id):
        return build_data_response({}, 200)

And I simply register this view with:

def register_views(api):
    from app.views.animal import AnimalView
    from flask_restful_swagger import swagger, registry
    api = swagger.docs(
    api,
    apiVersion="0.1",
    basePath="http://localhost:5000/api/v1",
    resourcePath="/",
    produces=["application/json", "text/html"],
    api_spec_url="/spec3",
    description="A Basic API")

But this doesn't work. It seems like the library cannot find the static files. I debugged the flask-restful-swagger code and found out that the prefix for x-api-prefix needs to be set. So I changed the code to:

def register_views(api):
    from app.views.animal import AnimalView
    from flask_restful_swagger import swagger, registry
    api = swagger.docs(
    api,
    apiVersion="0.1",
    basePath="http://localhost:5000/api/v1",
    resourcePath="/",
    produces=["application/json", "text/html"],
    api_spec_url="/spec",
    description="A Basic API")

    api.add_resource(AnimalView, "/animals/<animal_id>")

    registry["app"]["x-api-prefix"] = "/api/v1"

And now it works. So shouldn't the x-api-prefix become a parameter of docs()? So for example to call it like this:

    api = swagger.docs(
    api,
    apiVersion="0.1",
    basePath="http://localhost:5000/api/v1",
    resourcePath="/",
    produces=["application/json", "text/html"],
    api_spec_url="/spec",
    description="A Basic API",
    x-api-prefix="/api/v1"
)

Because at the moment it is a ("minor") hack. Or did I overlooked something?

NMO13 avatar Nov 30 '20 17:11 NMO13