Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Allow empty routed path (issue #414)#415

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tiangolo merged 1 commit intofastapi:masterfromvitalik:master
Aug 25, 2019
Merged

Allow empty routed path (issue #414)#415

tiangolo merged 1 commit intofastapi:masterfromvitalik:master
Aug 25, 2019

Conversation

@vitalik
Copy link
Contributor

With this patch it should be possible now to include empty route path
it also checks if both path and prefix are not empty
see#414 for more details

steinitzu reacted with rocket emoji
@vitalik
Copy link
ContributorAuthor

hi

do you think this will land to release ? (we are just trying to understand our project strategy... )

@pablogamboa
Copy link
Contributor

This feature also interests me quite a lot! Perhaps not this exact implementation, but I think it would be very beneficial to have the flexibility of having routes without a trailing slash 👍

@euri10
Copy link
Contributor

euri10 commentedAug 8, 2019 via email

I don't like the idea of having FastAPI behaving differently thanStarlette, if this should land it should be upstream imho, or not at all.Le jeu. 8 août 2019 à 8:03 AM, Pablo Marti <notifications@github.com> aécrit :
This feature also interests me quite a lot! Perhaps not this exact implementation, but I think it would be very beneficial to have the flexibility of having routes without a trailing slash 👍 — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#415>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAINSPXXUIKMNIV5JMB66T3QDPAKLANCNFSM4IIOOMHQ> .
dmontagu reacted with thumbs up emoji

@pablogamboa
Copy link
Contributor

yeah you're right@euri10 the discussion should be moved there

@vitalik
Copy link
ContributorAuthor

I don't like the idea of having FastAPI behaving differently than Starlette, if this should land it should be upstream imho, or not at all. Le jeu. 8 août 2019 à 8:03 AM, Pablo Marti
Hi@euri10 ,@pablogamboa

Well you forgetting that Starlette's endpoints can handle multiple http methods (GET, POST, HEAD, PUT) with one function, while what nice about fastapi - each method handled in separate function...

So I would not inherit ALOT Starlette's' approach (well I actually question them for not allowing ending slashes as well)

Just take a look at this example API structure:

GET  /catsPOST /catsGET  /cats/{id}/childrenPOST /cats/{id}/childrenGET  /cats/{id}/tasksPOST /cats/{id}/tasksGET  /cats/{id}/activitiesPOST /cats/{id}/activitiesGET  /cats/{id}/foodPOST /cats/{id}/foodGET  /dogsPOST /dogsGET  /dogs/{id}/childrenPOST /dogs/{id}/childrenGET  /dogs/{id}/tasksPOST /dogs/{id}/tasksGET  /dogs/{id}/activitiesPOST /dogs/{id}/activitiesGET  /dogs/{id}/foodPOST /dogs/{id}/foodGET  /birdsPOST /birdsGET  /birds/{id}/childrenPOST /birds/{id}/childrenGET  /birds/{id}/tasksPOST /birds/{id}/tasksGET  /birds/{id}/activitiesPOST /birds/{id}/activitiesGET  /birds/{id}/foodPOST /birds/{id}/food

if fastapi allowed me to to use empty route - it would lead just 3 lines of code:

app.include_route(..., prefix='/cats')app.include_route(..., prefix='/dogs')app.include_route(..., prefix='/birds')

without empty path in route I will have to do the following:

app.get('/cats', cats_get)app.post('/cats', cats_post)app.include_route(..., prefix='/cats')app.get('/dogs', dogs_get)app.post('/dogs', dogs_post)app.include_route(..., prefix='/dogs')app.get('/birds', birds_get)app.post('/birds', birds_post)app.include_route(..., prefix='/birds')

not only it produces 3 times more code, but also my views will have weird functions cats_get, cats_post without decorators

how else I would make it nicer ?

@euri10
Copy link
Contributor

euri10 commentedAug 8, 2019 via email

You could use a router with prefix cats, one with prefix dogs, etc,Sorry for the formatting I'm on mobile, but in essence with the structureyou mentioned you could do what's described herehttps://fastapi.tiangolo.com/tutorial/bigger-applications/ dogsrouter = APIRouter() catsrouter = APIRouter()app.include_router(dogsrouter)app.include_router( catsrouter, prefix="/cats", )@catsrouter.get('/')@catsrouter.post('/')@catsrouter.get('/{id}/children')Le jeu. 8 août 2019 à 12:41 PM, Vitaliy Kucheryaviy <notifications@github.com> a écrit :
I don't like the idea of having FastAPI behaving differently than Starlette, if this should land it should be upstream imho, or not at all. Le jeu. 8 août 2019 à 8:03 AM, Pablo Marti Hi@euri10 <https://github.com/euri10> ,@pablogamboa <https://github.com/pablogamboa> Well you forgetting that Starlette's endpoints can handle multiple http methods (GET, POST, HEAD, PUT) with one function, while what nice about fastapi - each method handled in separate function... So I would not inherit ALOT Starlette's' approach (well I actually question them for not allowing ending slashes as well) Just take a look at this example API structure: GET /cats POST /cats GET /cats/{id}/children POST /cats/{id}/children GET /cats/{id}/tasks POST /cats/{id}/tasks GET /cats/{id}/activities POST /cats/{id}/activities GET /cats/{id}/food POST /cats/{id}/food GET /dogs POST /dogs GET /dogs/{id}/children POST /dogs/{id}/children GET /dogs/{id}/tasks POST /dogs/{id}/tasks GET /dogs/{id}/activities POST /dogs/{id}/activities GET /dogs/{id}/food POST /dogs/{id}/food GET /birds POST /birds GET /birds/{id}/children POST /birds/{id}/children GET /birds/{id}/tasks POST /birds/{id}/tasks GET /birds/{id}/activities POST /birds/{id}/activities GET /birds/{id}/food POST /birds/{id}/food if fastapi allowed me to to use empty route - it would lead just 3 lines of code: app.include_route(..., prefix='/cats') app.include_route(..., prefix='/dogs') app.include_route(..., prefix='/birds') without empty path in route I will have to do the following: app.get('/cats', cats_get) app.post('/cats', cats_post) app.include_route(..., prefix='/cats') app.get('/dogs', dogs_get) app.post('/dogs', dogs_post) app.include_route(..., prefix='/dogs') app.get('/birds', birds_get) app.post('/birds', birds_post) app.include_route(..., prefix='/birds') not only it produces 3 times more code, but also my views will have weird functions cats_get, cats_post without decorators how else I would make it nicer ? — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#415>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAINSPWBQGOGYE4NKOVEGXDQDQA4ZANCNFSM4IIOOMHQ> .

@vitalik
Copy link
ContributorAuthor

@euri10 well - it does not work - try it...

you cannot include these endpoints without ending slash

GET  /catsPOST /cats

but making it outside routed views makes code less modular/incapsulated

@euri10
Copy link
Contributor

euri10 commentedAug 8, 2019 via email

unless I'm mistaken Starlette redirects you with a 308 if you don't havethe trailing slashwell I may be missing something obvious you want, sorry if I dont get it :)
On Thu, Aug 8, 2019 at 4:19 PM Vitaliy Kucheryaviy ***@***.***> wrote:@euri10 <https://github.com/euri10> well - it does not work - try it... you cannot include these endpoints without ending slash GET /cats POST /cats but making it outside routed views makes code less modular/incapsulated — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#415>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AAINSPXTGG3J23GCWNVHSCTQDQTQDANCNFSM4IIOOMHQ> .

@steinitzu
Copy link
Contributor

Redirects don't solve this for me. I want my route to be documented asGET /cats notGET /cats/ as it's consistent with my other endpoints that don't have a trailing slash.

What's the reason for not allowing empty paths?

@dmontagu
Copy link
Collaborator

@steinitzu I'm assuming it's just a way to ensure you don't accidentally end up with paths that aren't separated by slashes. Either way, to@euri10's point, I think that the right place to ask this question would probably be onstarlette because of this:https://github.com/encode/starlette/blob/master/starlette/routing.py#L147

@vitalik
Copy link
ContributorAuthor

@dmontagu well starlette routing have nothing to do with fast-API app.get/post/patch.. these a re different code basses

I'm assuming it's just a way to ensure you don't accidentally end up with paths that aren't separated by slashes

my pull request is actually makes this check - if you use empty routed path and include - it will throw an error

@dmontagu
Copy link
Collaborator

@vitalik

well starlette routing have nothing to do with fast-API app.get/post/patch

I think that's a little extreme; clearly they are related --APIRoute is a subclass ofstarlette.routing.Route after all.

That said, it is a valid point thatinclude_router doesn't exist in starlette, so this concern wouldn't apply. GivenAPIRoute is typically added to an app viainclude_router I'm convinced this is a reasonable change. I personally would be fine with merging it.

Either way, it would certainly be less controversial if there was a way to implement this that didn't remove validation that would be typically be performed in the superclass.

@vitalik
Copy link
ContributorAuthor

I'm sorry - maybe I am extreme , sorry about that - but this patch is only removing one line with assert inside fastapi code (have nothing to do with starlette) that's it (plus extra check that both include an route are not empty)

if you can give me any other option how to make my code more modular/isolated withot this assert - I would be happy to use it

@vitalik
Copy link
ContributorAuthor

Hi@tiangolo
Can you share your concerns or plans about this PR ?

Thank you

@tiangolotiangolo merged commitf7f17fc intofastapi:masterAug 25, 2019
@tiangolo
Copy link
Member

Thank you@vitalik ! Merged! 🎉 🚀


I think I can agree with you all 😄

I agree I don't want to diverge from Starlette, but it's true that the router functionality and the "path operations" are already handled differently.

I think for a new developer to find a path operation with an empty route in an existing codebase could be confusing, and that in some cases it could be more "intuitive" to have paths that are a prefix of other paths always have a trailing slash.

But that's a bit subjective, depending on the point of view, in fact, GitHub itself has by default the contrary behavior, the one that would be supported by this feature (it'shttps://github.com/tiangolo/fastapi , nothttps://github.com/tiangolo/fastapi/).

I also see that this is a feature that seems to help several use cases (at least 3, just from this thread), and I think it might help especially with some migrations, to keep paths consistent with previous implementations. Also, the flexibility it adds doesn't break current behavior, so I think it's fine.

Thanks for your contribution@vitalik ! And thanks for the interest and discussion here everyone!


Now, although I ran the tests locally first (and they passed), as Travis seems to have not triggered, I see now there's a mypy error after merging 😞. So I'll fix it before a release.

dmontagu and steinitzu reacted with hooray emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

6 participants

@vitalik@pablogamboa@euri10@steinitzu@dmontagu@tiangolo

[8]ページ先頭

©2009-2025 Movatter.jp