python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.16k stars 335 forks source link

Swagger documentation with two or more blueprints #70

Open PedroLuiz99 opened 4 years ago

PedroLuiz99 commented 4 years ago

Hello! Fist of all, thanks for the awesome project!

So, I work with a flask-restx project with two independent Blueprints, each of them with your own documentation:

bp_mapi = Blueprint('api_mapi', __name__)
api_mapi = Api(bp_mapi,
               title="Harpo Management API",
               version=MAPI_VERSION,
               doc="/mapi/doc",
               prefix='/mapi'
               )
bp_host = Blueprint('api_host', __name__)
api_host = Api(bp_host,
               title="Harpo Host communication API",
               version=MAPI_VERSION,
               doc="/host/doc",
               decorators=[check_host_jwt]
               )

The blueprints are registered in this order:

    app.register_blueprint(bp_host)
    app.register_blueprint(bp_mapi)

The problem is when I try to access /mapi/doc, the project shows the host documentation.

It's an issue or am I forgeting something?

Thanks in advance!

X1Zeth2X commented 4 years ago

Since you are using a prefix it will prefix your doc route as well. At the moment, you are using prefix="/mapi" it will prefix your doc="/mapi/doc" which ends up becoming /mapi/mapi/doc. The solution is simply removing /mapi from your doc route, so something like this should do:

api_mapi = Api(bp_mapi,
               title="Harpo Management API",
               version=MAPI_VERSION,
               doc="/doc", # Just remove '/mapi' since you're already prefixing it.
               prefix='/mapi'
               )
PedroLuiz99 commented 4 years ago

Hi, Sorry for the late response.

I've tried this but now the /mapi/doc route gives me a 404 error.

X1Zeth2X commented 4 years ago

Sorry I missed another part of your code, but have you tried using app.register_blueprint(bp_mapi, url_prefix="/mapi") instead of applying prefix in the Api itself?

martincpt commented 1 year ago

I just ran into the same issue. The root problem is that you "merge" your blueprints by not defining the url_prefix, which is the same what I did as well.

After I set the url_prefix on one of my blueprint I was able to reach both of them.

However, I merge my blueprints on purpose because I don't want to use url prefixes and I want to keep my app in separate modules.

I haven't tried namespaces yet but maybe that's the way to go. I just don't like the idea that I have to keep update my master Api instance by adding new namespaces.

Would be nice to merge the docs as well, if I merge the blueprints.

martincpt commented 1 year ago

Update: I was able to access both of my docs without using url_prefix but using different swagger file name and doc path with default_swagger_filename parameter.