Skip to content

Commit 2a102b7

Browse files
committed
[Python] Use pydantic_core.to_jsonable_python to convert non-standard data types
1 parent d605afe commit 2a102b7

File tree

383 files changed

+766
-766
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

383 files changed

+766
-766
lines changed

modules/openapi-generator/src/main/resources/python/model_generic.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import json
1111
{{/vendorExtensions.x-py-model-imports}}
1212
from typing import Optional, Set
1313
from typing_extensions import Self
14+
from pydantic_core import to_jsonable_python
1415

1516
{{#hasChildren}}
1617
{{#discriminator}}
@@ -127,8 +128,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
127128

128129
def to_json(self) -> str:
129130
"""Returns the JSON representation of the model using alias"""
130-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
131-
return json.dumps(self.to_dict())
131+
return json.dumps(to_jsonable_python(self.to_dict()))
132132

133133
@classmethod
134134
def from_json(cls, json_str: str) -> Optional[{{^hasChildren}}Self{{/hasChildren}}{{#hasChildren}}{{#discriminator}}Union[{{#mappedModels}}{{{modelName}}}{{^-last}}, {{/-last}}{{/mappedModels}}]{{/discriminator}}{{^discriminator}}Self{{/discriminator}}{{/hasChildren}}]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/bird.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Any, ClassVar, Dict, List, Optional
2323
from typing import Optional, Set
2424
from typing_extensions import Self
25+
from pydantic_core import to_jsonable_python
2526

2627
class Bird(BaseModel):
2728
"""
@@ -44,8 +45,7 @@ def to_str(self) -> str:
4445

4546
def to_json(self) -> str:
4647
"""Returns the JSON representation of the model using alias"""
47-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
48-
return json.dumps(self.to_dict())
48+
return json.dumps(to_jsonable_python(self.to_dict()))
4949

5050
@classmethod
5151
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/category.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Any, ClassVar, Dict, List, Optional
2323
from typing import Optional, Set
2424
from typing_extensions import Self
25+
from pydantic_core import to_jsonable_python
2526

2627
class Category(BaseModel):
2728
"""
@@ -44,8 +45,7 @@ def to_str(self) -> str:
4445

4546
def to_json(self) -> str:
4647
"""Returns the JSON representation of the model using alias"""
47-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
48-
return json.dumps(self.to_dict())
48+
return json.dumps(to_jsonable_python(self.to_dict()))
4949

5050
@classmethod
5151
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/data_query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from openapi_client.models.query import Query
2525
from typing import Optional, Set
2626
from typing_extensions import Self
27+
from pydantic_core import to_jsonable_python
2728

2829
class DataQuery(Query):
2930
"""
@@ -47,8 +48,7 @@ def to_str(self) -> str:
4748

4849
def to_json(self) -> str:
4950
"""Returns the JSON representation of the model using alias"""
50-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
51-
return json.dumps(self.to_dict())
51+
return json.dumps(to_jsonable_python(self.to_dict()))
5252

5353
@classmethod
5454
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/default_value.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from openapi_client.models.string_enum_ref import StringEnumRef
2424
from typing import Optional, Set
2525
from typing_extensions import Self
26+
from pydantic_core import to_jsonable_python
2627

2728
class DefaultValue(BaseModel):
2829
"""
@@ -62,8 +63,7 @@ def to_str(self) -> str:
6263

6364
def to_json(self) -> str:
6465
"""Returns the JSON representation of the model using alias"""
65-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
66-
return json.dumps(self.to_dict())
66+
return json.dumps(to_jsonable_python(self.to_dict()))
6767

6868
@classmethod
6969
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/number_properties_only.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from typing_extensions import Annotated
2424
from typing import Optional, Set
2525
from typing_extensions import Self
26+
from pydantic_core import to_jsonable_python
2627

2728
class NumberPropertiesOnly(BaseModel):
2829
"""
@@ -46,8 +47,7 @@ def to_str(self) -> str:
4647

4748
def to_json(self) -> str:
4849
"""Returns the JSON representation of the model using alias"""
49-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
50-
return json.dumps(self.to_dict())
50+
return json.dumps(to_jsonable_python(self.to_dict()))
5151

5252
@classmethod
5353
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/pet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from openapi_client.models.tag import Tag
2525
from typing import Optional, Set
2626
from typing_extensions import Self
27+
from pydantic_core import to_jsonable_python
2728

2829
class Pet(BaseModel):
2930
"""
@@ -60,8 +61,7 @@ def to_str(self) -> str:
6061

6162
def to_json(self) -> str:
6263
"""Returns the JSON representation of the model using alias"""
63-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
64-
return json.dumps(self.to_dict())
64+
return json.dumps(to_jsonable_python(self.to_dict()))
6565

6666
@classmethod
6767
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Any, ClassVar, Dict, List, Optional
2323
from typing import Optional, Set
2424
from typing_extensions import Self
25+
from pydantic_core import to_jsonable_python
2526

2627
class Query(BaseModel):
2728
"""
@@ -55,8 +56,7 @@ def to_str(self) -> str:
5556

5657
def to_json(self) -> str:
5758
"""Returns the JSON representation of the model using alias"""
58-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
59-
return json.dumps(self.to_dict())
59+
return json.dumps(to_jsonable_python(self.to_dict()))
6060

6161
@classmethod
6262
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/tag.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Any, ClassVar, Dict, List, Optional
2323
from typing import Optional, Set
2424
from typing_extensions import Self
25+
from pydantic_core import to_jsonable_python
2526

2627
class Tag(BaseModel):
2728
"""
@@ -44,8 +45,7 @@ def to_str(self) -> str:
4445

4546
def to_json(self) -> str:
4647
"""Returns the JSON representation of the model using alias"""
47-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
48-
return json.dumps(self.to_dict())
48+
return json.dumps(to_jsonable_python(self.to_dict()))
4949

5050
@classmethod
5151
def from_json(cls, json_str: str) -> Optional[Self]:

samples/client/echo_api/python-disallowAdditionalPropertiesIfNotPresent/openapi_client/models/test_form_object_multipart_request_marker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Any, ClassVar, Dict, List, Optional
2323
from typing import Optional, Set
2424
from typing_extensions import Self
25+
from pydantic_core import to_jsonable_python
2526

2627
class TestFormObjectMultipartRequestMarker(BaseModel):
2728
"""
@@ -43,8 +44,7 @@ def to_str(self) -> str:
4344

4445
def to_json(self) -> str:
4546
"""Returns the JSON representation of the model using alias"""
46-
# TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
47-
return json.dumps(self.to_dict())
47+
return json.dumps(to_jsonable_python(self.to_dict()))
4848

4949
@classmethod
5050
def from_json(cls, json_str: str) -> Optional[Self]:

0 commit comments

Comments
 (0)