question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] auto-rewrite doesn't take openapi_prefix into account

See original GitHub issue

Describe the bug

The problem is basically the one described here: https://github.com/tiangolo/fastapi/issues/51#issuecomment-522035784 My fastapi app is behind a reverse proxy that forwards all requests for /api/v1/* and rewrites them to /*. The application itself doesn’t know that but the FastAPI object which has openapi_prefix=“/api/v1”.

If I have an endpoint

@app.get("/hello")
def foo():
    return "hello"

and I query /api/v1/hello, it works fine. If I add an extra slash at the end, a redirect occurs but tries to send the user to /hello instead of /api/v1/hello. I’m not sure if it’s a bug or if I don’t use the app correctly. Maybe it would be simpler for me to make the fastapi app aware of the /api/v1 prefix and remove the rewrite rule? What do you think?

To Reproduce

Behind a reverse proxy forwarding the requests for /api/v1/* and rewriting those to /*

  1. Create a file with:
from fastapi import FastAPI

app = FastAPI(openapi_prefix="/api/v1")


@app.get("/hello")
def read_root():
    return {"Hello": "World"}
  1. Open the browser and call the endpoint /api/v1/hello/.
  2. It returns a 404 because there is a redirect to /hello.
  3. But I expected the redirect to be to /api/v1/hello.

Expected behavior

The redirection shouldn’t remove the openapi_prefix.

Environment

  • OS: Linux / Windows
  • FastAPI Version 0.52.0 from pip
  • python 3.7.4.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Gaasmanncommented, Jun 24, 2020

@tiangolo Hello. I just tested my app by replacing the openapi_prefix parameter by root_path on the FastAPI object: it works like a charm. I’ll close this issue. Thanks a lot!

2reactions
rkbeatsscommented, Jun 17, 2020

@tiangolo will start investigating this today bitmoji

Read more comments on GitHub >

github_iconTop Results From Across the Web

I have installed OpenAPI 3 using springdoc, but the URL is ...
Firstly, the below dependency has nothing to do with the issue you are mentioning. <dependency> <groupId>org.springdoc</groupId> ...
Read more >
Zalando RESTful API and Event Guidelines
Hint: A good way to explore OpenAPI 3.0/2.0 is to navigate through the OpenAPI specification mind map and use our Swagger Plugin for...
Read more >
Web service error codes (Microsoft Dataverse) - Power Apps
This topic lists the error codes you might encounter when you debug your code.
Read more >
aws-cdk/aws-apigateway module - AWS Documentation
In case of failures, the fields error and cause are returned as part of the response. Other metadata such as billing details, AWS...
Read more >
API Host and Base Path - Swagger
Sign In · Try Free. OAS 2 This page applies to OpenAPI Specification ver. ... REST APIs have a base URL to which...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found