Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Second Blueprint registered in Flask Restplus does not show up in swagger

Given this Flask Restplus app:

from flask import Blueprint, Flask
from flask_restplus import Api, Resource

base_api_blueprint = Blueprint('base_api', __name__)
base_api = Api(base_api_blueprint)

bff_api_blueprint = Blueprint('bff_api', __name__)
bff_api = Api(bff_api_blueprint)

@base_api.route('/base', endpoint='base-endpoint')
class BaseResource(Resource):
    def get(self):
        return {"from":"base"}


@bff_api.route('/bff', endpoint='bff-endpoint')
class BffResource(Resource):
    def get(self):
        return {"from":"bff"}

app = Flask(__name__)
app.register_blueprint(base_api_blueprint)
app.register_blueprint(bff_api_blueprint)
api = Api(app)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)

When I navigate to the swagger doc at http://localhost:8080/ I see the swagger for base_api_blueprint but nothing for bff_api_blueprint. If I reverse the order of the register_blueprint calls (so bff_api_blueprint is registered first), the opposite happens -> the swagger for bff_api_blueprint shows, with nothing for base_api_blueprint.

Regardless of what the swagger says, both blueprints are operational i.e. they return HTTP responses.

How can I "merge" the swagger of both blueprints into one?

like image 704
Mike Hogan Avatar asked Jan 25 '26 12:01

Mike Hogan


1 Answers

From your code, if you want to combine documentation then you will be using one Api instance like so:

from flask import Blueprint, Flask
from flask_restplus import Api, Resource

base_api_blueprint = Blueprint('base_api', __name__)
# base_api = Api(base_api_blueprint)
api = Api(base_api_blueprint, doc="/combined/") #the end point to your combined documentation api

base_ns = api.namespace('base_api', description='Base API')
bff_ns = api.namespace('bff_api', description='Bff API')

# bff_api_blueprint = Blueprint('bff_api', __name__)
# bff_api = Api(bff_api_blueprint)

@base_ns.route('/base', endpoint='base-endpoint')
class BaseResource(Resource):
    def get(self):
        return {"from":"base"}


@bff_ns.route('/bff', endpoint='bff-endpoint')
class BffResource(Resource):
    def get(self):
        return {"from":"bff"}

app = Flask(__name__)
app.register_blueprint(base_api_blueprint)
# app.register_blueprint(bff_api_blueprint)
api = Api(app)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)

This approach also uses namespaces and now your end points will end up like this:

/combined/
/base_api/base
/bff_api/bff

If you have organised your project using blueprints, you can import the same api instance into other sections of the project. From Flask-RESTplus documentation, namespace is the same pattern as Flask’s blueprint.

like image 195
user3657232 Avatar answered Jan 27 '26 13:01

user3657232



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!