Skip to content

Commit d1e926a

Browse files
azure-sdkmsyyc
andauthored
[multiapi combiner] fix api-version validation (Azure#30880)
* fix api-version validation * review * fix * fix --------- Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
1 parent fca29d4 commit d1e926a

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

tools/azure-sdk-tools/packaging_tools/multiapi_combiner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ def decorator(self) -> str:
185185
# all of the api versions of the operation group
186186
# because the operation group handles first round of validation
187187
if self._need_method_api_version_check:
188-
retval.append(f" api_versions={self.api_versions},")
188+
retval.append(f" method_valid_on={self.api_versions},")
189189
if self._need_params_api_version_check:
190-
retval.append(" params={")
190+
retval.append(" params_valid_on={")
191191
retval.extend([f' "{p.name}": {p.api_versions},' for p in self.parameters if p.need_decorator])
192192
retval.append(" }")
193193
retval.append(" )")
@@ -259,7 +259,7 @@ def need_decorator(self) -> bool:
259259

260260
@property
261261
def decorator(self) -> str:
262-
return "\n".join(["@api_version_validation(", f" api_versions={self.api_versions}", ")"])
262+
return "\n".join(["@api_version_validation(", f" method_valid_on={self.api_versions}", ")"])
263263

264264
def combine_operations(self) -> None:
265265
api_versions = [v for v in self.code_model.sorted_api_versions if v in self.api_versions]
Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,48 @@
1-
2-
31
# --------------------------------------------------------------------------
42
# Copyright (c) Microsoft Corporation. All rights reserved.
53
# Licensed under the MIT License. See License.txt in the project root for license information.
64
# Code generated by Microsoft (R) Python Code Generator.
75
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
86
# --------------------------------------------------------------------------
9-
from typing import Optional, Dict, List
7+
import functools
108

119

12-
class api_version_validation:
13-
def __init__(
14-
self,
15-
*,
16-
api_versions: Optional[List[str]] = None,
17-
params: Optional[Dict[str, List[str]]] = None,
18-
):
19-
self.api_versions = api_versions or []
20-
self.params = params or {}
10+
def api_version_validation(**kwargs):
11+
params_valid_on = kwargs.pop("params_valid_on", {})
12+
method_valid_on = kwargs.pop("method_valid_on", {})
2113

22-
def __call__(self, func):
23-
api_versions = self.api_versions
24-
params = self.params
25-
def wrapper(self, *args, **kwargs):
14+
def decorator(func):
15+
@functools.wraps(func)
16+
def wrapper(*args, **kwargs):
2617
func_name = func.__name__
27-
if hasattr(self, "_get_api_version"):
28-
client_api_version = self._get_api_version(func_name)
29-
else:
30-
client_api_version = self._api_version
31-
if api_versions and client_api_version not in api_versions:
18+
try:
19+
client_api_version = client._get_api_version(func_name) # pylint: disable=protected-access
20+
except AttributeError:
21+
client_api_version = client._api_version
22+
23+
if client_api_version not in method_valid_on:
3224
raise ValueError(
3325
f"'{func_name}' is not available in API version "
34-
f"{client_api_version}. Pass service API version {api_versions[0]} or newer to your client."
26+
f"{client_api_version}. All valid API version are {', '.join(method_valid_on)}."
3527
)
36-
unsupported = [
37-
parameter
38-
for parameter, api_versions in params.items()
28+
29+
unsupported = {
30+
parameter: ", ".join(api_versions)
31+
for parameter, api_versions in params_valid_on.items()
3932
if parameter in kwargs and client_api_version not in api_versions
40-
]
33+
}
4134
if unsupported:
4235
raise ValueError(
4336
"".join(
4437
[
4538
f"'{param}' is not available in API version {client_api_version}. "
46-
f"Use service API version {params[param][0]} or newer.\n"
47-
for param, version in unsupported
39+
f"All valid API version are {versions} \n"
40+
for param, versions in unsupported.items()
4841
]
4942
)
5043
)
51-
return func(self, *args, **kwargs)
44+
return func(*args, **kwargs)
5245

5346
return wrapper
47+
48+
return decorator

0 commit comments

Comments
 (0)