Skip to content

Commit 5ec432c

Browse files
msyyciscai-msft
andauthored

File tree

11 files changed

+36
-13
lines changed

11 files changed

+36
-13
lines changed

.chronus/changes/python-versioningValidation-2025-5-20-12-14-3.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/http-client-python/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log - @typespec/http-client-python
22

3+
## 0.12.3
4+
5+
### Bug Fixes
6+
7+
- [#7705](https://github.com/microsoft/typespec/pull/7705) Validate api versions by looking at ordering of api versions from spec
8+
- [#7696](https://github.com/microsoft/typespec/pull/7696) Add support for `validate-versioning` flag, so users can toggle whether they get api versioning validation
9+
10+
311
## 0.12.2
412

513
### Bump dependencies

packages/http-client-python/emitter/src/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ function emitHttpOperation(
293293
discriminator: "basic",
294294
isOverload: false,
295295
overloads: [],
296-
apiVersions: [],
296+
apiVersions: method.apiVersions,
297297
wantTracing: true,
298298
exposeStreamKeyword: true,
299299
crossLanguageDefinitionId: method?.crossLanguageDefinitionId,

packages/http-client-python/emitter/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ function emitProperty(
249249
optional: property.optional,
250250
description: property.summary ? property.summary : property.doc,
251251
addedOn: getAddedOn(context, property),
252+
apiVersions: property.apiVersions,
252253
visibility: visibilityMapping(property.visibility),
253254
isDiscriminator: property.discriminator,
254255
flatten: property.flatten,

packages/http-client-python/emitter/src/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ type ParamBase = {
148148
isApiVersion: boolean;
149149
type: Record<string, any>;
150150
isContinuationToken: boolean;
151+
apiVersions: string[];
151152
};
152153

153154
export function getAddedOn<TServiceOperation extends SdkServiceOperation>(
@@ -228,6 +229,7 @@ export function emitParamBase<TServiceOperation extends SdkServiceOperation>(
228229
isApiVersion: parameter.isApiVersionParam,
229230
isContinuationToken: isContinuationToken(parameter, method),
230231
type,
232+
apiVersions: parameter.apiVersions,
231233
};
232234
}
233235

packages/http-client-python/generator/pygen/codegen/models/parameter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def __init__(
8181
self.grouper: bool = self.yaml_data.get("grouper", False)
8282
self.check_client_input: bool = self.yaml_data.get("checkClientInput", False)
8383
self.added_on: Optional[str] = self.yaml_data.get("addedOn")
84+
self.api_versions: Optional[List[str]] = self.yaml_data.get("apiVersions", [])
8485
self.is_api_version: bool = self.yaml_data.get("isApiVersion", False)
8586
self.in_overload: bool = self.yaml_data.get("inOverload", False)
8687
self.default_to_unset_sentinel: bool = self.yaml_data.get("defaultToUnsetSentinel", False)

packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ def _api_version_validation(self, builder: OperationType) -> str:
607607
if params_added_on:
608608
retval.append(f" params_added_on={dict(params_added_on)},")
609609
if retval:
610+
if builder.api_versions:
611+
retval.append(f" api_versions_list={builder.api_versions},")
610612
retval_str = "\n".join(retval)
611613
return f"@api_version_validation(\n{retval_str}\n)"
612614
return ""

packages/http-client-python/generator/pygen/codegen/templates/validation.py.jinja2

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import functools
66
def api_version_validation(**kwargs):
77
params_added_on = kwargs.pop("params_added_on", {})
88
method_added_on = kwargs.pop("method_added_on", "")
9+
api_versions_list = kwargs.pop("api_versions_list", [])
10+
11+
def _index_with_default(value: str, default: int = -1) -> int:
12+
"""Get the index of value in lst, or return default if not found.
13+
14+
:param value: The value to search for in the api_versions_list.
15+
:type value: str
16+
:param default: The default value to return if the value is not found.
17+
:type default: int
18+
:return: The index of the value in the list, or the default value if not found.
19+
:rtype: int
20+
"""
21+
try:
22+
return api_versions_list.index(value)
23+
except ValueError:
24+
return default
925

1026
def decorator(func):
1127
@functools.wraps(func)
@@ -17,7 +33,7 @@ def api_version_validation(**kwargs):
1733
except AttributeError:
1834
return func(*args, **kwargs)
1935

20-
if method_added_on > client_api_version:
36+
if _index_with_default(method_added_on) > _index_with_default(client_api_version):
2137
raise ValueError(
2238
f"'{func.__name__}' is not available in API version "
2339
f"{client_api_version}. Pass service API version {method_added_on} or newer to your client."
@@ -27,7 +43,7 @@ def api_version_validation(**kwargs):
2743
parameter: api_version
2844
for api_version, parameters in params_added_on.items()
2945
for parameter in parameters
30-
if parameter in kwargs and api_version > client_api_version
46+
if parameter in kwargs and _index_with_default(api_version) > _index_with_default(client_api_version)
3147
}
3248
if unsupported:
3349
raise ValueError("".join([

packages/http-client-python/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/http-client-python/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@typespec/http-client-python",
3-
"version": "0.12.2",
3+
"version": "0.12.3",
44
"author": "Microsoft Corporation",
55
"description": "TypeSpec emitter for Python SDKs",
66
"homepage": "https://typespec.io",

0 commit comments

Comments
 (0)