Skip to content

Commit 4603cc9

Browse files
wing328EspenHa
andauthored
Use Pydantic for json serialization in .to_json method (#23210)
* [Python] Use pydantic_core.to_jsonable_python to convert non-standard data types * [Python] Add test for UUID serialization * update python samples --------- Co-authored-by: Espen Haugsdal <espen.haugsdal@gmail.com>
1 parent 8ef4a48 commit 4603cc9

File tree

397 files changed

+819
-782
lines changed

Some content is hidden

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

397 files changed

+819
-782
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}}
@@ -128,8 +129,7 @@ class {{classname}}({{#parent}}{{{.}}}{{/parent}}{{^parent}}BaseModel{{/parent}}
128129

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

134134
@classmethod
135135
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
"""
@@ -45,8 +46,7 @@ def to_str(self) -> str:
4546

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

5151
@classmethod
5252
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
"""
@@ -45,8 +46,7 @@ def to_str(self) -> str:
4546

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

5151
@classmethod
5252
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
"""
@@ -48,8 +49,7 @@ def to_str(self) -> str:
4849

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

5454
@classmethod
5555
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
"""
@@ -63,8 +64,7 @@ def to_str(self) -> str:
6364

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

6969
@classmethod
7070
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
"""
@@ -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/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
"""
@@ -61,8 +62,7 @@ def to_str(self) -> str:
6162

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

6767
@classmethod
6868
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
"""
@@ -56,8 +57,7 @@ def to_str(self) -> str:
5657

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

6262
@classmethod
6363
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
"""
@@ -45,8 +46,7 @@ def to_str(self) -> str:
4546

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

5151
@classmethod
5252
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
"""
@@ -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]:

0 commit comments

Comments
 (0)