-
-
Notifications
You must be signed in to change notification settings - Fork 100
feat: validate async endpoints #849
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from collections.abc import AsyncIterator | ||
| from http import HTTPStatus | ||
| from typing import Any | ||
|
|
||
|
|
@@ -9,6 +10,7 @@ | |
| from dmr.exceptions import EndpointMetadataError | ||
| from dmr.options_mixins import AsyncMetaMixin, MetaMixin | ||
| from dmr.plugins.pydantic import PydanticSerializer | ||
| from dmr.plugins.pydantic.serializer import PydanticEndpointOptimizer | ||
|
|
||
|
|
||
| def test_controller_either_sync_or_async() -> None: | ||
|
|
@@ -174,3 +176,25 @@ def handle_error( | |
| exc: Exception, | ||
| ) -> Any: | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| def test_endpoint_rejects_async_gen() -> None: | ||
| """Ensure endpoints cannot be async generators.""" | ||
|
|
||
| class _NoOpOptimizer(PydanticEndpointOptimizer): | ||
| @override | ||
| @classmethod | ||
| def optimize_endpoint(cls, metadata: Any) -> None: # noqa: WPS324 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, why do you need this type?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If i use controller like this with pytest.raises(
EndpointMetadataError,
match='is an async generator',
):
class _BadController(Controller[PydanticSerializer]):
async def get(self) -> AsyncIterator[int]:
yield 1 # pragma: no coverI have pydantic error =(
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it! Please, then also test that |
||
| return None # noqa: WPS324 | ||
|
|
||
| class _NoOpPydanticSerializer(PydanticSerializer): | ||
| optimizer = _NoOpOptimizer | ||
|
|
||
| with pytest.raises( | ||
| EndpointMetadataError, | ||
| match='is an async generator', | ||
| ): | ||
|
|
||
| class _BadController(Controller[_NoOpPydanticSerializer]): | ||
| async def get(self) -> AsyncIterator[int]: | ||
| yield 1 # pragma: no cover | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are at it: let's all check for sync generators. Because they also won't work.