Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"/" route cannot be defined after Api() call #523

Closed
Jc-L opened this issue Feb 22, 2023 · 3 comments
Closed

"/" route cannot be defined after Api() call #523

Jc-L opened this issue Feb 22, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@Jc-L
Copy link

Jc-L commented Feb 22, 2023

Minimal Code to reproduce issue

from flask import Flask
from flask_restx import Api

app = Flask(__name__)

# NB: moving this line after the `route('/')` definition makes it work
api = Api(app, version='1.0', title='MyAPI', doc='/api')

@app.route('/')
def index():
    return 'OK'

app.run(debug=True)

Repro Steps (if applicable)

  1. The provided code answers a 404 on a request on /

However, if the call to API() is done after the setting of the / route ,then things work as expected.

Expected Behavior

Requests to / should answer properly.

Actual Behavior

Requests to / answer with a 404.

Error Messages/Stack Trace

N/A

Environment

  • Python 3.10.8
  • Flask 2.2.3
  • Flask-RESTX 1.0.6

Additional Context

Already mentioned in #452 as a question, but seems more a bug.

@Jc-L Jc-L added the bug Something isn't working label Feb 22, 2023
@peter-doggart
Copy link
Contributor

There is a full discussion about this over on the old project by the maintainers: noirbizarre/flask-restplus#712 (comment)

I don't think we can call it a bug, because the code explicitly sets out this is the desired response:

def render_root(self):
self.abort(HTTPStatus.NOT_FOUND)

The issue is that this project uses the root view to build other URLs during API creation, therefore it must ensure the root view is created before the API is initialized to allow this to happen. As other users have pointed out, that is why it works correctly if you register the root view first.

Maybe we should make this clearer in the docs?

@Jc-L
Copy link
Author

Jc-L commented Feb 22, 2023

Thanks @peter-doggart. I reached the render_root() while investigating, but from a user perspective, the final behavior was unexpected and needed some time to be narrowed down (hence the bug label).

I understand from the discussion that there is no simple way to go around this. Making it appear in the documentation would indeed be helpful to avoid users scratching their heads on this.

@peter-doggart
Copy link
Contributor

There is now a warning about this behaviour in the Quick Start Guide

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants