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

Starting with Flask version 2.2.0, there's a UserWarning raised for Blueprint #485

Closed morpheus65535 closed 1 year ago

morpheus65535 commented 2 years ago

Starting with Flask version 2.2.0, there's a UserWarning raised for Blueprint. It will raise an exception in 2.3.0. With Flask 2.1.3, there's no warning.

Code

from flask import Flask, Blueprint
from flask_restx import Api

app = Flask(__name__)
api_bp = Blueprint('api', __name__)
api = Api(api_bp)
app.register_blueprint(api_bp, url_prefix="/api")

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

Repro Steps (if applicable)

  1. Create a basic API with code provided
  2. Run the code
  3. Check output

Expected Behavior

flask-restx should use Flask method properly to prevent it from getting broken in future.

Actual Behavior

Flask raise UserWarning.

Error Messages/Stack Trace

/Users/morpheus/Desktop/flask-restx-issue/venv/lib/python3.10/site-packages/flask_restx/api.py:322: UserWarning: The setup method 'add_url_rule' can no longer be called on the blueprint 'api'. It has already been registered at least once, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the blueprint are done before registering it.
This warning will become an exception in Flask 2.3.
  app_or_blueprint.add_url_rule(self._doc, "doc", self.render_doc)
/Users/morpheus/Desktop/flask-restx-issue/venv/lib/python3.10/site-packages/flask/blueprints.py:490: UserWarning: The setup method 'record' can no longer be called on the blueprint 'api'. It has already been registered at least once, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the blueprint are done before registering it.
This warning will become an exception in Flask 2.3.
  self.record(
/Users/morpheus/Desktop/flask-restx-issue/venv/lib/python3.10/site-packages/flask_restx/api.py:323: UserWarning: The setup method 'add_url_rule' can no longer be called on the blueprint 'api'. It has already been registered at least once, any changes will not be applied consistently.
Make sure all imports, decorators, functions, etc. needed to set up the blueprint are done before registering it.
This warning will become an exception in Flask 2.3.
  app_or_blueprint.add_url_rule(self.prefix or "/", "root", self.render_root)

Environment

Additional Context

This is your last chance to provide any pertinent details, don't let this opportunity pass you by!

marcorichetta commented 2 years ago

Relevant issue https://github.com/pallets/flask/issues/4571

jessereich-mcs commented 2 years ago

Just getting around to investigating this warning. Any thoughts on how to properly register these blueprints?