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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Fix support for path parameters in WebSockets #3879

Merged
merged 7 commits into from Sep 1, 2022

Conversation

davidbrochart
Copy link
Contributor

Because a path can look like /files/{file_path:path}, we need to strip the type (e.g. :path) of the parameter.

Copy link

@adam-tokarski adam-tokarski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Care to add some tests for that?

@davidbrochart
Copy link
Contributor Author

Care to add some tests for that?

Sure, let me know what you think.

Copy link

@adam-tokarski adam-tokarski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, but personally I'd

  • add more tests for not so happy paths, e.g. "/path/{param::}" or "/path:smth/{param}"
  • introduce kind of parametrized (if available here), but it would not change too much

@davidbrochart
Copy link
Contributor Author

  • add more tests for not so happy paths, e.g. "/path/{param::}" or "/path:smth/{param}"

Done.

  • introduce kind of parametrized (if available here), but it would not change too much

parametrized is not used in FastAPI yet, and it seems a bit overkill to me, so I didn't use it.

@davidbrochart davidbrochart force-pushed the fix_path_params branch 3 times, most recently from 6216d8d to 23bf8ee Compare October 8, 2021 07:00
@davidbrochart
Copy link
Contributor Author

@adam-tokarski do you think this can be merged?

@adam-tokarski
Copy link

@adam-tokarski do you think this can be merged?

If there are not any edge cases for which it could break anything I would say - yes, why not.

@davidbrochart
Copy link
Contributor Author

I don't see any edge cases. What's the next step to have this PR in?

@ghandic
Copy link
Contributor

ghandic commented Oct 14, 2021

The only person that can merge is @tiangolo he'll take a look at it soon

@davidbrochart
Copy link
Contributor Author

@tiangolo do you think this can go in? We are currently monkey-patching the change in Jupyter.

@codecov
Copy link

codecov bot commented May 16, 2022

Codecov Report

Merging #3879 (aa48787) into master (0bb8920) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #3879   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          535       535           
  Lines        13825     13838   +13     
=========================================
+ Hits         13825     13838   +13     
Impacted Files Coverage 螖
fastapi/routing.py 100.00% <100.00%> (酶)
tests/test_ws_router.py 100.00% <100.00%> (酶)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@github-actions
Copy link
Contributor

馃摑 Docs preview for commit 6552462 at: https://628205635239266587b6e591--fastapi.netlify.app

@davidbrochart
Copy link
Contributor Author

The Jupyter server still needs to monkey-patch FastAPI, is there any chance this change could go in?

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 31, 2022

What's this for?

@davidbrochart
Copy link
Contributor Author

Thanks for chiming in @Kludex!
This is to support parameter types in WebSocket endpoints, for instance:

@app.websocket("/files/{file_path:path}")
async def websocket_endpoint(websocket: WebSocket, file_path):
    ...

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 31, 2022

I'll check later, but if you can answer "why this is not needed on the HTTP methods?" would be helpful.

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 31, 2022

Also, just to set the expectations... I don't have merge rights. 馃憖

@davidbrochart
Copy link
Contributor Author

Also, just to set the expectations... I don't have merge rights. eyes

No worries, it's already great to have your feedback.

I'll check later, but if you can answer "why this is not needed on the HTTP methods?" would be helpful.

Your question made me revisit the issue, and it turns out that there is a better fix.
The APIWebSocketRoute was not treated the same way as the APIRoute, as far as their dependant path is concerned. Passing self.path_format for the former, as it is done for the latter, fixes the issue in a much nicer way.

@github-actions
Copy link
Contributor

馃摑 Docs preview for commit f1afa0d at: https://630f2f2d4b042e7bbc6f2c70--fastapi.netlify.app

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 31, 2022

Now it makes more sense. 馃憤

A test checking that query and path params can be passed as expected might be a good idea. 馃憖

@davidbrochart
Copy link
Contributor Author

A test checking that query and path params can be passed as expected might be a good idea.

馃憤
Without the fix, the test fails as expected:

tests/test_ws_router.py::test_router_with_params FAILED                                                                                       [100%]

===================================================================== FAILURES ======================================================================
______________________________________________________________ test_router_with_params ______________________________________________________________

    def test_router_with_params():
        client = TestClient(app)
>       with client.websocket_connect("/router/path/to/file?queryparam=a_query_param") as websocket:

tests/test_ws_router.py:120: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../../mambaforge/envs/fastapi/lib/python3.10/site-packages/starlette/testclient.py:316: in __enter__
    self._raise_on_close(message)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <starlette.testclient.WebSocketTestSession object at 0x7fba1ed72ef0>, message = {'code': 1008, 'reason': '', 'type': 'websocket.close'}

    def _raise_on_close(self, message: Message) -> None:
        if message["type"] == "websocket.close":
>           raise WebSocketDisconnect(
                message.get("code", 1000), message.get("reason", "")
            )
E           starlette.websockets.WebSocketDisconnect: (1008, '')

../../../mambaforge/envs/fastapi/lib/python3.10/site-packages/starlette/testclient.py:357: WebSocketDisconnect

@github-actions
Copy link
Contributor

馃摑 Docs preview for commit 7c7c76e at: https://630f5c1603e32307504c07f3--fastapi.netlify.app

@davidbrochart davidbrochart changed the title Fix path param names Fix WebSocket path parameters Aug 31, 2022
@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 31, 2022

This makes sense. 馃憤

Thanks @davidbrochart 馃檹

@tiangolo tiangolo changed the title Fix WebSocket path parameters 馃悰 Fix support for path parameters in WebSockets Sep 1, 2022
@tiangolo tiangolo merged commit d8b6aa6 into tiangolo:master Sep 1, 2022
@tiangolo
Copy link
Owner

tiangolo commented Sep 1, 2022

Awesome, thank you @davidbrochart! 馃

It's so exciting you are using FastAPI in some part of Jupyter!

Also thanks everyone for the help here! 馃殌

This will be available in the next release, in some hours (the current is 0.81.0). 馃帀

@davidbrochart davidbrochart deleted the fix_path_params branch September 1, 2022 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants