fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

Automatic OPTIONS request with route schema

Open samuelcolvin opened this issue 4 years ago • 13 comments

First check

Yes to all (great list of checks btw, I might copy them to pydantic 👍 )

Thank you

@tiangolo FastAPI is great. This is the first time I've used it in production and I'm really impressed by both the range of features and particularly the documentation. I've written and deployed web apps using django, flask, aiohttp, starlette and now fastapi. FastAPI is the only python web framework I've seen to come close to django in terms of quality of documentation, it's definitely now my go-to web framework.

Thank you for the amazing project, I feel really proud to have contributed to it indirectly.

Feature Request

The only thing I've seen missing from fastpi is support for informative OPTIONS requests for all/most/some endpoints, automatically included based in the route's definition.

I effectively want the following:

class LoginForm(BaseModel):
    email: EmailStr
    password: SecretStr

@app.post('/login/')
async def login(login_form: LoginForm):
   ...

@app.options('/login/', include_in_schema=False)
async def login_options():
    return LoginForm.schema(by_alias=True)

Without having to define the second endpoint. This is so my react frontend can build forms without having to duplicate the field definitions.

I guess it might have to be slightly more complex to include parameters, query args and body model(s).

I know I can get the information I need from /openapi.json, but that involves more processing in js and more data downloaded.

In the past when I've implemented a feature like this myself, I had to deal with OPTIONS requests associated with preflight checks separately, but that's relatively easy by looking for the Access-Control-Request-Method header.

I guess if this feature was to be approved it would have to be optional, default off. I guess through a keyword arg on FastAPI, include_router and each route.

samuelcolvin avatar Sep 03 '20 13:09 samuelcolvin

According to the specifications of OPTIONS method, i'm not sure if it is supposed to do what you want

If I understands correctly, you want to have a shortcut to the schema of some endpoints, while OPTIONS describing the whole path.

I think it'll be better to have such a shortcut by <METHOD> http:\example.net\docs<path> for the <METHOD> endpoint

Vlatterran avatar Apr 20 '23 10:04 Vlatterran

No, I want the scheme for that URL, which is exactly what OPTIONS requests are for.

samuelcolvin avatar Apr 20 '23 12:04 samuelcolvin

I just ran into this unexpectedly when I saw a bunch of HTTP 405 responses using Swagger UI. It looks like Swagger UI assumes that every endpoint supports OPTIONS. It doesn't seem to impede the ability to send requests in the UI, but I assume it's used for something, otherwise it wouldn't be there. I was surprised that HTTP OPTIONS wasn't supported by default for FastAPI routes, at minimum returning an empty response with the standard ALLOW header.

I also noticed that HEAD similarly was not supported by default, which can be useful for debugging and scripting. This again was surprising to me.

So here's my +1 for enabling both OPTIONS and HEAD by default in FastAPI routes.

gwerbin avatar May 09 '23 18:05 gwerbin

Hello please check draft of PR for this issue https://github.com/tiangolo/fastapi/pull/10057

whysage avatar Aug 10 '23 15:08 whysage

Hi, updated PR (fixed linters and tests, add default value False for add_auto_options_route) please review general approach https://github.com/tiangolo/fastapi/pull/10861

Tests will be added.

whysage avatar Dec 30 '23 11:12 whysage

Tests were added. Please review. https://github.com/tiangolo/fastapi/pull/10861

@tiangolo @samuelcolvin

whysage avatar Jan 14 '24 09:01 whysage

Hi, any comments on pull request?

whysage avatar Jan 24 '24 17:01 whysage

A kind reminder about review of PR https://github.com/tiangolo/fastapi/pull/10861 @tiangolo @samuelcolvin @gwerbin @Vlatterran

whysage avatar Feb 18 '24 14:02 whysage

Any updates on review https://github.com/tiangolo/fastapi/pull/10861 ? @tiangolo @samuelcolvin @gwerbin @Vlatterran

whysage avatar Mar 08 '24 07:03 whysage

Sorry @whysage, I haven't had much free time. I will try to block out some time for it on Sunday night. Note that I'm not a repo maintainer so you will still need approval from someone else.

gwerbin avatar Mar 08 '24 18:03 gwerbin

@gwerbin thanks!

whysage avatar Mar 09 '24 12:03 whysage

Hi, please review https://github.com/tiangolo/fastapi/pull/10861 @tiangolo @samuelcolvin @gwerbin @Vlatterran

whysage avatar Mar 19 '24 16:03 whysage

@whysage thanks for the interest, I'll review it once I get there, I have many other things to review and do.

Please do not ping us as it adds additional overhead and work reviewing the pings, and that's an effort I/we could have invested in reviewing PRs, so that we could get to yours faster.

If you want to accelerate the process, you could help with everything else, answering questions, reviewing other PRs, etc: https://fastapi.tiangolo.com/help-fastapi/#help-maintain-fastapi

tiangolo avatar Mar 19 '24 16:03 tiangolo