-
Notifications
You must be signed in to change notification settings - Fork 380
fix(http-client-python): api_version validation decorator reads correct config attribute name #11392
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
Merged
Merged
fix(http-client-python): api_version validation decorator reads correct config attribute name #11392
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9864c76
fix(http-client-python): api_version validation decorator reads corre…
l0lawrence d218e73
Drop incidental version f-string NameError change; keep only api_vers…
l0lawrence fe6f127
Merge branch 'main' into l0lawrence-fix-api-version-validation-attr
l0lawrence 7fde618
Tighten changelog wording
l0lawrence 0ad4284
Keep protected _config access on its own line so black can't detach t…
l0lawrence 34a487f
Read api_version via a direct config attribute accessor instead of ge…
l0lawrence 8236f8c
Revert to getattr-based config attribute lookup
l0lawrence cab1998
Bake api-version config attribute name into _validation.py
l0lawrence 3d6b98d
Inline protected _config access in _validation.py decorator
l0lawrence 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
7 changes: 7 additions & 0 deletions
7
.chronus/changes/fix-api-version-validation-attr-2026-7-24-11-30-0.md
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" | ||
| --- | ||
|
|
||
| Fix the generated `_validation.py` `@api_version_validation` decorator so it reads the correct client config attribute for the API version. It previously hardcoded `client._config.api_version`, but the attribute name is derived from the API-version parameter's `client_name`. For specs that name the versioning parameter something other than `apiVersion` (e.g. `self.version`), the lookup raised `AttributeError` that the decorator silently swallowed, disabling all API-version validation for those clients. The emitter now bakes the real attribute name into the generated decorator so it reads `config.<name>` directly. |
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
140 changes: 140 additions & 0 deletions
140
packages/http-client-python/tests/unit/test_api_version_validation.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,140 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for | ||
| # license information. | ||
| # -------------------------------------------------------------------------- | ||
| """Tests for the generated ``_validation.py`` ``@api_version_validation`` decorator. | ||
|
|
||
| Regression coverage for the bug where the generated ``_validation.py`` decorator | ||
| hardcoded ``client._config.api_version``. When the versioning parameter is named | ||
| something other than ``apiVersion`` (e.g. Azure Storage names it ``version`` via | ||
| ``@apiVersion @header("x-ms-version") version: string``), the config attribute is | ||
| ``self.version`` and NOT ``self.api_version``. The hardcoded lookup then raised | ||
| ``AttributeError`` which the decorator silently swallowed, disabling ALL | ||
| api-version validation for those clients. | ||
|
|
||
| The fix bakes the real config attribute name (the api-version parameter's | ||
| ``client_name``) into the rendered ``_validation.py`` so the decorator reads | ||
| ``config.<client_name>`` directly. | ||
| """ | ||
|
|
||
| import pytest | ||
| from jinja2 import Environment, PackageLoader | ||
|
|
||
| from pygen.codegen.models import Client, CodeModel | ||
| from pygen.codegen.models.parameter import ConfigParameter | ||
| from pygen.codegen.models.parameter_list import ClientGlobalParameterList | ||
| from pygen.codegen.models.primitive_types import StringType | ||
| from pygen.codegen.serializers.general_serializer import GeneralSerializer | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def code_model(): | ||
| return CodeModel( | ||
| { | ||
| "clients": [ | ||
| { | ||
| "name": "client", | ||
| "namespace": "blah", | ||
| "moduleName": "blah", | ||
| "parameters": [], | ||
| "url": "", | ||
| "operationGroups": [], | ||
| } | ||
| ], | ||
| "namespace": "namespace", | ||
| }, | ||
| options={ | ||
| "show-send-request": True, | ||
| "builders-visibility": "public", | ||
| "show-operations": True, | ||
| "models-mode": "dpg", | ||
| "version-tolerant": True, | ||
| "flavor": "azure", | ||
| }, | ||
| ) | ||
|
|
||
|
|
||
| def _env() -> Environment: | ||
| return Environment( | ||
| loader=PackageLoader("pygen.codegen", "templates"), | ||
| keep_trailing_newline=True, | ||
| line_statement_prefix="##", | ||
| line_comment_prefix="###", | ||
| trim_blocks=True, | ||
| lstrip_blocks=True, | ||
| ) | ||
|
|
||
|
|
||
| def _api_version_config_parameter(code_model, client_name: str) -> ConfigParameter: | ||
| return ConfigParameter( | ||
| yaml_data={ | ||
| "clientName": client_name, | ||
| "optional": True, | ||
| "location": "query", | ||
| "wireName": "api-version", | ||
| "implementation": "Client", | ||
| "isApiVersion": True, | ||
| "description": "The API version.", | ||
| }, | ||
| code_model=code_model, | ||
| type=StringType({"type": "string"}, code_model), | ||
| ) | ||
|
|
||
|
|
||
| def _client_with_api_version(code_model, client_name): | ||
| client = Client( | ||
| { | ||
| "name": "client", | ||
| "namespace": "blah", | ||
| "moduleName": "blah", | ||
| "parameters": [], | ||
| "url": "", | ||
| "operationGroups": [], | ||
| }, | ||
| code_model, | ||
| parameters=ClientGlobalParameterList({}, code_model, parameters=[]), | ||
| ) | ||
| if client_name is not None: | ||
| client.config.parameters.parameters.append(_api_version_config_parameter(code_model, client_name)) | ||
| code_model.clients = [client] | ||
| return client | ||
|
|
||
|
|
||
| def _render(code_model) -> str: | ||
| return GeneralSerializer(code_model=code_model, env=_env()).serialize_validation_file() | ||
|
|
||
|
|
||
| def test_reads_version_attribute_for_non_conventional_param(code_model): | ||
| # Storage-like: the versioning parameter is named ``version`` -> config attr | ||
| # is ``self.version``, so the decorator must read ``config.version`` directly. | ||
| _client_with_api_version(code_model, "version") | ||
|
|
||
| rendered = _render(code_model) | ||
|
|
||
| assert "client_api_version = client._config.version" in rendered | ||
| assert "config.api_version" not in rendered | ||
| assert "getattr(" not in rendered | ||
|
|
||
|
|
||
| def test_reads_api_version_attribute_for_conventional_param(code_model): | ||
| _client_with_api_version(code_model, "api_version") | ||
|
|
||
| rendered = _render(code_model) | ||
|
|
||
| assert "client_api_version = client._config.api_version" in rendered | ||
|
|
||
|
|
||
| def test_defaults_to_api_version_when_no_api_version_param(code_model): | ||
| _client_with_api_version(code_model, None) | ||
|
|
||
| rendered = _render(code_model) | ||
|
|
||
| assert "client_api_version = client._config.api_version" in rendered | ||
|
|
||
|
|
||
| def test_api_version_config_attr_name_helper(code_model): | ||
| _client_with_api_version(code_model, "version") | ||
| serializer = GeneralSerializer(code_model=code_model, env=_env()) | ||
|
|
||
| assert serializer._api_version_config_attr_name() == "version" |
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.