Repository navigation
[http-client-python] Serialize named model query parameters #12624
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
l0lawrence
wants to merge
2
commits into
microsoft:main
Choose a base branch
from
l0lawrence:l0lawrence-python-core-support-fix
base: main
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.
+110
−5
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: fix | ||
| packages: | ||
| - "@typespec/http-client-python" | ||
| --- | ||
|
|
||
| Serialize named model query parameters using form-style expansion, including exploded properties and query continuation routes. |
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
42 changes: 42 additions & 0 deletions
42
packages/http-client-python/tests/unit/test_query_model_serialization.py
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,42 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
| from jinja2 import Environment, PackageLoader | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session", autouse=True) | ||
| def testserver(): | ||
| yield | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def serializer(): | ||
| env = Environment(loader=PackageLoader("pygen.codegen", "templates")) | ||
| source = env.get_template("serialization.py.jinja2").render( | ||
| code_model=SimpleNamespace(license_header="", core_library="corehttp") | ||
| ) | ||
| namespace = {} | ||
| exec(compile(source, "<serialization>", "exec"), namespace) | ||
| return namespace["Serializer"]() | ||
|
|
||
|
|
||
| def test_query_model_quotes_components_and_preserves_falsy_values(serializer): | ||
| result = serializer.query_model( | ||
| "filter", | ||
| {"wire,name": "a,b & c", "enabled": False, "count": 0, "empty": "", "omitted": None}, | ||
| ) | ||
| assert result == {"wire%2Cname": "a%2Cb%20%26%20c", "enabled": "false", "count": "0", "empty": ""} | ||
|
|
||
|
|
||
| def test_query_model_skip_url_encoding(serializer): | ||
| assert serializer.query_model("filter", {"wire,name": "a,b"}, skip_quote=True) == {"wire,name": "a,b"} | ||
|
|
||
|
|
||
| def test_query_model_rejects_missing_required_model(serializer): | ||
| with pytest.raises(ValueError, match="filter"): | ||
| serializer.query_model("filter", None) |
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.
[P2] Omit standard expansion when the model has no defined properties
This branch assigns
_params["param"] = ""whenquery_model()returns{}, producing?param=(or¶m=for continuation) instead of omitting the parameter. This is reachable with a named model containing only optional properties, e.g.model Filters { field?: string; }, when the caller passesFilters(); an all-Nonemapping has the same result. I reproduced both cases using the serializer methods from this revision.RFC 6570 section 2.3 treats an associative value with zero members, or only undefined members, as undefined: https://www.rfc-editor.org/rfc/rfc6570.html#section-2.3. Servers can distinguish an absent parameter from an explicitly empty value. Exploded expansion already omits it correctly via
.update({}).Could we serialize the pairs once and only assign the standard query parameter when that dictionary is nonempty? Please add regressions for empty/all-
Noneproperties in standard expansion and continuation, while preserving a defined empty-string property such as{"field": ""}.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.
I think up comment belongs to corner case to feel free to accept or not.