Skip to content

Commit 8fe9f78

Browse files
committed
chore(python) update generator to 7.22
1 parent cfdcfc9 commit 8fe9f78

8 files changed

Lines changed: 96 additions & 48 deletions

File tree

languages/python/templates/__init__package.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/__init__package.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/__init__package.mustache for the original template }}
22
# coding: utf-8
33

44
# flake8: noqa

languages/python/templates/api.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/api.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/api.mustache for the original template }}
22
# coding: utf-8
33

44
{{>partial_header}}
5+
56
import warnings
67
from pydantic import validate_call, Field, StrictFloat, StrictStr, StrictInt
78
from typing import Any, Dict, List, Optional, Tuple, Union

languages/python/templates/api_client.mustache

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/api_client.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/api_client.mustache for the original template }}
22
# coding: utf-8
33

44
{{>partial_header}}
55

6+
67
import datetime
78
from dateutil.parser import parse
89
from enum import Enum
@@ -66,6 +67,7 @@ class ApiClient:
6667
'date': datetime.date,
6768
'datetime': datetime.datetime,
6869
'decimal': decimal.Decimal,
70+
'UUID': uuid.UUID,
6971
'object': object,
7072
}
7173
_pool = None
@@ -320,7 +322,7 @@ class ApiClient:
320322
response_text = None
321323
return_data = None
322324
try:
323-
if response_type == "bytearray":
325+
if response_type in ("bytearray", "bytes"):
324326
return_data = response_data.data
325327
elif response_type == "file":
326328
return_data = self.__deserialize_file(response_data)
@@ -385,28 +387,24 @@ class ApiClient:
385387
return obj.isoformat()
386388
elif isinstance(obj, decimal.Decimal):
387389
return str(obj)
388-
389390
elif isinstance(obj, dict):
390-
obj_dict = obj
391+
return {
392+
key: self.sanitize_for_serialization(val)
393+
for key, val in obj.items()
394+
}
395+
396+
# Convert model obj to dict except
397+
# attributes `openapi_types`, `attribute_map`
398+
# and attributes which value is not None.
399+
# Convert attribute name to json key in
400+
# model definition for request.
401+
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')):
402+
obj_dict = obj.to_dict()
391403
else:
392-
# Convert model obj to dict except
393-
# attributes `openapi_types`, `attribute_map`
394-
# and attributes which value is not None.
395-
# Convert attribute name to json key in
396-
# model definition for request.
397-
if hasattr(obj, 'to_dict') and callable(getattr(obj, 'to_dict')): # noqa: B009
398-
obj_dict = obj.to_dict()
399-
else:
400-
obj_dict = obj.__dict__
404+
obj_dict = obj.__dict__
401405

402-
if isinstance(obj_dict, list):
403-
# here we handle instances that can either be a list or something else, and only became a real list by calling to_dict() # noqa: E501
404-
return self.sanitize_for_serialization(obj_dict)
406+
return self.sanitize_for_serialization(obj_dict)
405407

406-
return {
407-
key: self.sanitize_for_serialization(val)
408-
for key, val in obj_dict.items()
409-
}
410408

411409
def deserialize(self, response_text: str, response_type: str, content_type: Optional[str]):
412410
"""Deserializes response into an object.
@@ -488,6 +486,8 @@ class ApiClient:
488486
return self.__deserialize_datetime(data)
489487
elif klass is decimal.Decimal:
490488
return decimal.Decimal(data)
489+
elif klass is uuid.UUID:
490+
return uuid.UUID(data)
491491
elif issubclass(klass, Enum):
492492
return self.__deserialize_enum(data, klass)
493493
else:

languages/python/templates/exceptions.mustache

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/exceptions.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/exceptions.mustache for the original template }}
22
# coding: utf-8
33

44
{{>partial_header}}
@@ -96,9 +96,9 @@ class ApiKeyError(OpenApiException, KeyError):
9696
class ApiException(OpenApiException):
9797

9898
def __init__(
99-
self,
100-
status=None,
101-
reason=None,
99+
self,
100+
status=None,
101+
reason=None,
102102
http_resp=None,
103103
*,
104104
body: Optional[str] = None,
@@ -124,10 +124,10 @@ class ApiException(OpenApiException):
124124

125125
@classmethod
126126
def from_response(
127-
cls,
128-
*,
129-
http_resp,
130-
body: Optional[str],
127+
cls,
128+
*,
129+
http_resp,
130+
body: Optional[str],
131131
data: Optional[Any],
132132
) -> Self:
133133
if http_resp.status == 400:

languages/python/templates/model_generic.mustache

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/model_generic.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/model_generic.mustache for the original template }}
22
from __future__ import annotations
33
import pprint
44
import re # noqa: F401
@@ -15,6 +15,7 @@ from pydantic import field_validator
1515
{{! TEMPLATE CUSTOMIZATION - END - import for workaround below }}
1616
from typing import Optional, Set
1717
from typing_extensions import Self
18+
from pydantic_core import to_jsonable_python
1819

1920
{{#hasChildren}}
2021
{{#discriminator}}
@@ -56,6 +57,9 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
5657

5758
{{/isNullable}}
5859
{{/required}}
60+
if not isinstance(value, str):
61+
value = str(value)
62+
5963
if not re.match(r"{{{.}}}", value{{#vendorExtensions.x-modifiers}} ,re.{{{.}}}{{/vendorExtensions.x-modifiers}}):
6064
raise ValueError(r"must validate the regular expression {{{vendorExtensions.x-pattern}}}")
6165
return value
@@ -114,7 +118,8 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
114118
{{/vars}}
115119

116120
model_config = ConfigDict(
117-
populate_by_name=True,
121+
validate_by_name=True,
122+
validate_by_alias=True,
118123
validate_assignment=True,
119124
protected_namespaces=(),
120125
)
@@ -147,8 +152,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
147152

148153
def to_json(self) -> str:
149154
"""Returns the JSON representation of the model using alias"""
150-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
151-
return json.dumps(self.to_dict())
155+
return json.dumps(to_jsonable_python(self.to_dict()))
152156

153157
@classmethod
154158
def from_json(cls, json_str: str) -> Optional[{{^hasChildren}}Self{{/hasChildren}}{{#hasChildren}}{{#discriminator}}Union[{{#mappedModels}}{{{modelName}}}{{^-last}}, {{/-last}}{{/mappedModels}}]{{/discriminator}}{{^discriminator}}Self{{/discriminator}}{{/hasChildren}}]:
@@ -201,7 +205,21 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
201205
_dict['{{{baseName}}}'] = _items
202206
{{/items.items.isPrimitiveType}}
203207
{{/items.isArray}}
208+
{{#items.isMap}}
209+
{{^items.items.isPrimitiveType}}
210+
# override the default output from pydantic by calling `to_dict()` of each item in {{{name}}} (list of dict)
211+
_items = []
212+
if self.{{{name}}}:
213+
for _item_{{{name}}} in self.{{{name}}}:
214+
if _item_{{{name}}}:
215+
_items.append(
216+
{_inner_key: _inner_value.to_dict() for _inner_key, _inner_value in _item_{{{name}}}.items()}
217+
)
218+
_dict['{{{baseName}}}'] = _items
219+
{{/items.items.isPrimitiveType}}
220+
{{/items.isMap}}
204221
{{^items.isArray}}
222+
{{^items.isMap}}
205223
{{^items.isPrimitiveType}}
206224
{{^items.isEnumOrRef}}
207225
# override the default output from pydantic by calling `to_dict()` of each item in {{{name}}} (list)
@@ -213,6 +231,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
213231
_dict['{{{baseName}}}'] = _items
214232
{{/items.isEnumOrRef}}
215233
{{/items.isPrimitiveType}}
234+
{{/items.isMap}}
216235
{{/items.isArray}}
217236
{{/isArray}}
218237
{{#isMap}}
@@ -229,6 +248,20 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
229248
_dict['{{{baseName}}}'] = _field_dict_of_array
230249
{{/items.items.isPrimitiveType}}
231250
{{/items.isArray}}
251+
{{#items.isMap}}
252+
{{^items.items.isPrimitiveType}}
253+
# override the default output from pydantic by calling `to_dict()` of each value in {{{name}}} (dict of dict)
254+
_field_dict_of_dict = {}
255+
if self.{{{name}}}:
256+
for _key_{{{name}}}, _value_{{{name}}} in self.{{{name}}}.items():
257+
if _value_{{{name}}} is not None:
258+
_field_dict_of_dict[_key_{{{name}}}] = {
259+
_key: _value.to_dict() for _key, _value in _value_{{{name}}}.items()
260+
}
261+
_dict['{{{baseName}}}'] = _field_dict_of_dict
262+
{{/items.items.isPrimitiveType}}
263+
{{/items.isMap}}
264+
{{^items.isMap}}
232265
{{^items.isArray}}
233266
{{^items.isPrimitiveType}}
234267
{{^items.isEnumOrRef}}
@@ -242,6 +275,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
242275
{{/items.isEnumOrRef}}
243276
{{/items.isPrimitiveType}}
244277
{{/items.isArray}}
278+
{{/items.isMap}}
245279
{{/isMap}}
246280
{{/isContainer}}
247281
{{^isContainer}}
@@ -312,6 +346,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
312346
{{#allVars}}
313347
{{#isContainer}}
314348
{{#isArray}}
349+
{{#items.isContainer}}
315350
{{#items.isArray}}
316351
{{#items.items.isPrimitiveType}}
317352
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
@@ -323,7 +358,19 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
323358
] if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
324359
{{/items.items.isPrimitiveType}}
325360
{{/items.isArray}}
326-
{{^items.isArray}}
361+
{{#items.isMap}}
362+
{{#items.items.isPrimitiveType}}
363+
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
364+
{{/items.items.isPrimitiveType}}
365+
{{^items.items.isPrimitiveType}}
366+
"{{{baseName}}}": [
367+
{_inner_key: {{{items.items.dataType}}}.from_dict(_inner_value) for _inner_key, _inner_value in _item.items()}
368+
for _item in obj["{{{baseName}}}"]
369+
] if obj.get("{{{baseName}}}") is not None else None{{^-last}},{{/-last}}
370+
{{/items.items.isPrimitiveType}}
371+
{{/items.isMap}}
372+
{{/items.isContainer}}
373+
{{^items.isContainer}}
327374
{{^items.isPrimitiveType}}
328375
{{#items.isEnumOrRef}}
329376
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
@@ -335,7 +382,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
335382
{{#items.isPrimitiveType}}
336383
"{{{baseName}}}": obj.get("{{{baseName}}}"){{^-last}},{{/-last}}
337384
{{/items.isPrimitiveType}}
338-
{{/items.isArray}}
385+
{{/items.isContainer}}
339386
{{/isArray}}
340387
{{#isMap}}
341388
{{^items.isPrimitiveType}}
@@ -350,20 +397,18 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
350397
if _v is not None
351398
else None
352399
)
353-
for _k, _v in obj.get("{{{baseName}}}").items()
400+
for _k, _v in obj["{{{baseName}}}"].items()
354401
)
355402
if obj.get("{{{baseName}}}") is not None
356403
else None{{^-last}},{{/-last}}
357404
{{/items.isMap}}
358405
{{#items.isArray}}
359-
"{{{baseName}}}": dict(
360-
(_k,
361-
[{{{items.items.dataType}}}.from_dict(_item) for _item in _v]
362-
if _v is not None
363-
else None
364-
)
365-
for _k, _v in obj.get("{{{baseName}}}", {}).items()
366-
){{^-last}},{{/-last}}
406+
"{{{baseName}}}": {
407+
_k: [{{{items.items.dataType}}}.from_dict(_item) for _item in _v] if _v is not None else None
408+
for _k, _v in obj["{{{baseName}}}"].items()
409+
}
410+
if obj.get("{{{baseName}}}") is not None
411+
else None{{^-last}},{{/-last}}
367412
{{/items.isArray}}
368413
{{/items.isContainer}}
369414
{{^items.isContainer}}

languages/python/templates/model_oneof.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/model_oneof.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/model_oneof.mustache for the original template }}
22
from __future__ import annotations
33
import json
44
import pprint

languages/python/templates/rest.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.20.0/modules/openapi-generator/src/main/resources/python/rest.mustache for the original template }}
1+
{{! This template was customized. See https://github.com/OpenAPITools/openapi-generator/blob/v7.22.0/modules/openapi-generator/src/main/resources/python/rest.mustache for the original template }}
22
# coding: utf-8
33

44
{{>partial_header}}
55

6+
67
import io
78
import json
89
import re
10+
import ssl
911

1012
{{! TEMPLATE CUSTOMIZATION - BEGIN - requests instead of urllib3 }}
1113
import requests

scripts/generate-sdk/generate-sdk.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ go)
6161
python)
6262
# When the GENERATOR_VERSION changes, migrate also the templates in templates/python
6363
# Renovate: datasource=github-tags depName=OpenAPITools/openapi-generator versioning=semver
64-
GENERATOR_VERSION="v7.20.0"
64+
GENERATOR_VERSION="v7.22.0"
6565
;;
6666
java)
6767
# When the GENERATOR_VERSION changes, migrate also the templates in templates/java

0 commit comments

Comments
 (0)