Repository navigation
fix(fastapi): bind null Kafka records inside nullable model batches #3390
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
Open
Lancetnik
wants to merge
4
commits into
stack/tombstone-route-batch
from
stack/tombstone-fastapi-batch
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6989a9a
fix(fastapi): bind null Kafka records inside nullable model batches
Lancetnik aa5bfc3
Bind FastAPI tombstone batches per body field
Lancetnik b77d2d3
Merge branch 'fix-stack/coverage-3389' into fix-stack/coverage-3390
Lancetnik d6dfd68
Merge branch 'fix-stack/coverage-3389' into fix-stack/coverage-3390
Lancetnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import inspect | ||
| from collections.abc import Callable | ||
| from collections.abc import Callable, Sequence | ||
| from dataclasses import dataclass, replace | ||
| from functools import partial | ||
| from types import UnionType | ||
|
|
@@ -17,6 +17,8 @@ | |
| from fastapi.dependencies.models import Dependant | ||
| from fastapi.requests import Request | ||
|
|
||
| from faststream.message import StreamMessage | ||
|
|
||
| major, minor, patch, *_ = FASTAPI_VERSION.split(".") | ||
|
|
||
| _FASTAPI_MAJOR, _FASTAPI_MINOR = int(major), int(minor) | ||
|
|
@@ -45,8 +47,10 @@ | |
|
|
||
| __all__ = ( | ||
| "RequestValidationError", | ||
| "TombstoneBody", | ||
| "bind_tombstone_body", | ||
| "create_response_field", | ||
| "get_tombstone_body", | ||
| "raise_fastapi_validation_error", | ||
| "solve_faststream_dependency", | ||
| ) | ||
|
|
@@ -153,9 +157,32 @@ async def solve_faststream_dependency( | |
| ) | ||
|
|
||
|
|
||
| @dataclass(slots=True) | ||
| class TombstoneBody: | ||
| batch: list[Any] | None = None | ||
|
|
||
|
|
||
| def get_tombstone_body( | ||
| message: "StreamMessage[Any]", decoded: Any | ||
| ) -> TombstoneBody | None: | ||
| if not message.uses_default_decoder: | ||
| return None | ||
| if not message.has_body and _is_empty_body(decoded): | ||
| return TombstoneBody() | ||
|
|
||
| missing = message.batch_body_missing | ||
| if isinstance(decoded, list) and len(decoded) == len(missing) and any(missing): | ||
| return TombstoneBody([ | ||
| None if absent and _is_empty_body(item) else item | ||
| for item, absent in zip(decoded, missing, strict=True) | ||
| ]) | ||
| return None | ||
|
|
||
|
|
||
| def bind_tombstone_body( | ||
| dependant: "Dependant", | ||
| dependency_overrides_provider: Any, | ||
| tombstone: TombstoneBody, | ||
| ) -> tuple["Dependant", "dict[tuple[Any, ...], Any] | None"]: | ||
| """Apply the tombstone binding while retaining the original dependency keys.""" | ||
| overrides = getattr(dependency_overrides_provider, "dependency_overrides", {}) | ||
|
|
@@ -164,7 +191,7 @@ def bind_tombstone_body( | |
| if overrides and not hasattr(dependant, "cache_key") | ||
| else None | ||
| ) | ||
| return _bind_tombstone_fields(dependant, overrides, cache), cache | ||
| return _bind_tombstone_fields(dependant, overrides, cache, tombstone.batch), cache | ||
|
|
||
|
|
||
| _DEPENDANT_PARAMETERS = inspect.signature(get_dependant).parameters | ||
|
|
@@ -174,14 +201,15 @@ def _bind_tombstone_fields( | |
| dependant: "Dependant", | ||
| overrides: dict[Any, Any], | ||
| cache: "_OverrideDependencyCache | None", | ||
| batch: list[Any] | None, | ||
| ) -> "Dependant": | ||
| dependencies = [] | ||
| for original in dependant.dependencies: | ||
| dependency = original | ||
| override = overrides.get(original.call) | ||
| if override is not None: | ||
| dependency = _get_overridden_dependant(original, override) | ||
| adapted = _bind_tombstone_fields(dependency, overrides, cache) | ||
| adapted = _bind_tombstone_fields(dependency, overrides, cache, batch) | ||
| if cache is not None: | ||
| assert adapted.call is not None | ||
| adapted.call = cache.bind(adapted.call, original) | ||
|
|
@@ -190,9 +218,12 @@ def _bind_tombstone_fields( | |
| legacy_dependant.cache_key = cache_key | ||
| dependencies.append(adapted) | ||
|
|
||
| accepts_tombstone = ( | ||
| _is_optional_model_body if batch is None else _accepts_nullable_model_items | ||
| ) | ||
| body_params = [ | ||
| cast("ModelField", _TombstoneBodyField(field)) | ||
| if _is_optional_model_body(field) | ||
| cast("ModelField", _TombstoneBodyField(field, batch)) | ||
| if accepts_tombstone(field) | ||
| else field | ||
| for field in dependant.body_params | ||
| ] | ||
|
|
@@ -296,17 +327,20 @@ def _is_generator(call: Any) -> bool: | |
|
|
||
|
|
||
| class _TombstoneBodyField: | ||
| __slots__ = ("_field",) | ||
| __slots__ = ("_batch", "_field") | ||
|
|
||
| def __init__(self, field: "ModelField") -> None: | ||
| def __init__(self, field: "ModelField", batch: list[Any] | None) -> None: | ||
| self._field = field | ||
| self._batch = batch | ||
|
|
||
| def __getattr__(self, name: str) -> Any: | ||
| return getattr(self._field, name) | ||
|
|
||
| def validate( | ||
| self, value: Any, values: dict[str, Any], *, loc: tuple[int | str, ...] | ||
| ) -> tuple[Any, Any]: | ||
| if self._batch is not None: | ||
| return self._field.validate(self._batch, values, loc=loc) | ||
| if getattr(self._field.field_info, "validate_default", False): | ||
| return self._field.validate(None, values, loc=loc) | ||
| return None, [] | ||
|
|
@@ -328,10 +362,38 @@ def _is_optional_model_body(field: "ModelField") -> bool: | |
| ) | ||
|
|
||
|
|
||
| def _is_empty_body(value: Any) -> bool: | ||
| return value is None or (isinstance(value, (bytes, str)) and not value) | ||
|
|
||
|
|
||
| def _accepts_nullable_model_items(field: "ModelField") -> bool: | ||
| annotation = ( | ||
| field.field_info.annotation if FASTAPI_V2 else getattr(field, "outer_type_", None) | ||
| ) | ||
| annotation = _unwrap_annotated(annotation) | ||
| if get_origin(annotation) in {Union, UnionType}: | ||
| types = tuple(arg for arg in get_args(annotation) if arg is not type(None)) | ||
| if len(types) != 1: | ||
| return False | ||
| annotation = _unwrap_annotated(types[0]) | ||
| if get_origin(annotation) not in {list, Sequence}: | ||
| return False | ||
| items = get_args(annotation) | ||
| if not items: | ||
| return False | ||
| item = _unwrap_annotated(items[0]) | ||
| return type(None) in get_args(item) and _is_model_annotation(item) | ||
|
Comment on lines
+369
to
+385
Collaborator
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. |
||
|
|
||
|
|
||
| def _unwrap_annotated(annotation: Any) -> Any: | ||
| if get_origin(annotation) is Annotated: | ||
| return get_args(annotation)[0] | ||
| return annotation | ||
|
|
||
|
|
||
| def _is_model_annotation(annotation: Any) -> bool: | ||
| annotation = _unwrap_annotated(annotation) | ||
| origin = get_origin(annotation) | ||
| if origin is Annotated: | ||
| return _is_model_annotation(get_args(annotation)[0]) | ||
| if origin in {Union, UnionType}: | ||
| return all( | ||
| _is_model_annotation(arg) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Rearranging the decoded
batchinmiddlewareviolates thelist[Model | None]binding. Ifconsume_scopeexpands the list fromawait msg.decode(), the tombstones mask remains in its original order. On bothTestBrokergot a validation error onb""instead of[None, Model(...)]