-
-
Notifications
You must be signed in to change notification settings - Fork 980
feat(request): add get_query_string_as_media() method #2548
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
MannXo
wants to merge
15
commits into
falconry:master
Choose a base branch
from
MannXo:feat/query-string-as-media
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+410
−0
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
fa4500a
feat(request): add get_query_string_as_media() method
MannXo f4d6059
test: improve coverage for get_query_string_as_media
MannXo e550111
fix(asgi): use sync deserializer for query string media
MannXo 951f4e0
remove caching from get_query_string_as_media
MannXo 5633332
refactor: remove query-string caching
MannXo 4e658c7
Merge branch 'master' into feat/query-string-as-media
MannXo 9dff053
Merge branch 'master' into feat/query-string-as-media
vytas7 23ed9f1
Merge branch 'master' into feat/query-string-as-media
MannXo 69df029
resolved PR comments.
MannXo adbe960
Merge branch 'master' into feat/query-string-as-media
vytas7 fc14ac6
resolved PR comments
MannXo d63bc23
Merge branch 'master' into feat/query-string-as-media
vytas7 e83caf1
Merge branch 'master' into feat/query-string-as-media
vytas7 b30f8dd
Merge branch 'master' into feat/query-string-as-media
vytas7 38879b6
Address PR review: add ValueError to docstring, improve test assertio…
MannXo 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Added a new :meth:`~falcon.Request.get_query_string_as_media` method to | ||
| :class:`~falcon.Request` and :class:`~falcon.asgi.Request` that deserializes | ||
| the entire query string as media content. This is useful for implementing | ||
| OpenAPI 3.2 ``Parameter Object`` with content, where the whole query string | ||
| is treated as serialized data (e.g., JSON). The method URL-decodes the query | ||
| string and deserializes it using the configured media handlers. |
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 |
|---|---|---|
|
|
@@ -1164,6 +1164,86 @@ def get_media(self, default_when_empty: UnsetOr[Any] = _UNSET) -> Any: | |
| this property. | ||
| """ | ||
|
|
||
| def get_query_string_as_media( | ||
| self, media_type: str | None = None, default_when_empty: UnsetOr[Any] = _UNSET | ||
| ) -> Any: | ||
| """Deserialize the query string as a media object. | ||
|
|
||
| This method URL-decodes the query string and then deserializes it | ||
| as a media object using the specified media type handler. This is | ||
| useful for implementing OpenAPI 3.2 `Parameter Object with content`_ | ||
| where the entire query string is treated as serialized content. | ||
|
|
||
| For example, if the query string is | ||
| ``%7B%22numbers%22%3A%5B1%2C2%5D%2C%22flag%22%3Anull%7D``, this | ||
| method will URL-decode it to ``{"numbers":[1,2],"flag":null}`` and | ||
| then deserialize it as JSON (assuming ``media_type`` is set to | ||
| ``'application/json'``):: | ||
|
|
||
| # Query string: ?%7B%22numbers%22%3A%5B1%2C2%5D%7D | ||
| data = req.get_query_string_as_media('application/json') | ||
| # data == {'numbers': [1, 2]} | ||
|
|
||
| See also :ref:`media` for more information regarding media handling. | ||
|
|
||
| Note: | ||
| When called on a request with an empty query string, Falcon will | ||
| let the media handler try to deserialize the empty string and will | ||
| return the value returned by the handler or propagate the exception | ||
| raised by it. To instead return a different value in case of an | ||
| exception by the handler, specify the argument ``default_when_empty``. | ||
|
|
||
| Args: | ||
| media_type: Media type to use for deserialization | ||
| (e.g., ``'application/json'``). If not specified, the | ||
| ``default_media_type`` from :class:`falcon.RequestOptions` | ||
| will be used (default ``'application/json'``). | ||
|
|
||
| Keyword Args: | ||
| default_when_empty: Fallback value to return when there is no | ||
| query string and the media handler raises an error. By default, | ||
| Falcon uses the value returned by the media handler or | ||
| propagates the raised exception, if any. | ||
|
|
||
| Returns: | ||
| object: The deserialized media representation of the query string. | ||
|
|
||
| Raises: | ||
| ValueError: No media handler is configured for the specified | ||
| media type. | ||
|
|
||
| .. _Parameter Object with content: | ||
| https://spec.openapis.org/oas/latest.html#parameter-object-examples | ||
|
|
||
| """ | ||
| if media_type is None: | ||
| media_type = self.options.default_media_type | ||
|
|
||
| handler, _, _ = self.options.media_handlers._resolve( | ||
| media_type, self.options.default_media_type, raise_not_found=False | ||
| ) | ||
| if handler is None: | ||
| raise ValueError( | ||
|
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. let's mention in the doc string that a value error is raised if no handler is found
Contributor
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. Added |
||
| f'No media handler is configured for {media_type!r}. ' | ||
| 'Please ensure the media type is registered in ' | ||
| 'RequestOptions.media_handlers.' | ||
| ) | ||
|
|
||
| # URL-decode the query string | ||
| decoded_query_string = util.uri.decode(self.query_string, unquote_plus=False) | ||
|
|
||
| # Encode once and reuse bytes for BytesIO and length to avoid | ||
| # double-encoding the string. | ||
| query_bytes = decoded_query_string.encode('utf-8') | ||
| query_stream = BytesIO(query_bytes) | ||
|
|
||
| try: | ||
| return handler.deserialize(query_stream, media_type, len(query_bytes)) | ||
| except errors.MediaNotFoundError: | ||
| if default_when_empty is not _UNSET: | ||
| return default_when_empty | ||
| raise | ||
|
|
||
| # ------------------------------------------------------------------------ | ||
| # Methods | ||
| # ------------------------------------------------------------------------ | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.