Skip to content

Commit 4cfc8ef

Browse files
authored
Add tests for enum names with dots in python cilents (#21374)
* add tests for enum names with dot * remove file * apply same fix to python pydantic v1 * update test
1 parent 9d70de4 commit 4cfc8ef

File tree

43 files changed

+1087
-33
lines changed

Some content is hidden

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

43 files changed

+1087
-33
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,8 @@ public void setMapNumberTo(String mapNumberTo) {
15931593
}
15941594

15951595
public String toEnumVariableName(String name, String datatype) {
1596+
name = name.replace(".", "_DOT_");
1597+
15961598
if ("int".equals(datatype)) {
15971599
return "NUMBER_" + name.replace("-", "MINUS_");
15981600
}

modules/openapi-generator/src/test/resources/3_0/enum_float.yaml

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

modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,3 +2898,15 @@ components:
28982898
- B
28992899
- C
29002900
default: B
2901+
PonySizes:
2902+
type: object
2903+
properties:
2904+
type:
2905+
$ref: '#/components/schemas/Type'
2906+
Type:
2907+
type: float
2908+
enum:
2909+
- 2.0
2910+
- 1.0
2911+
- 0.5
2912+
- 0.25

samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ docs/ParentWithOptionalDict.md
8989
docs/Pet.md
9090
docs/PetApi.md
9191
docs/Pig.md
92+
docs/PonySizes.md
9293
docs/PoopCleaning.md
9394
docs/PrimitiveString.md
9495
docs/PropertyMap.md
@@ -113,6 +114,7 @@ docs/TestInlineFreeformAdditionalPropertiesRequest.md
113114
docs/TestModelWithEnumDefault.md
114115
docs/TestObjectForMultipartRequestsRequestMarker.md
115116
docs/Tiger.md
117+
docs/Type.md
116118
docs/UnnamedDictWithAdditionalModelListProperties.md
117119
docs/UnnamedDictWithAdditionalStringListProperties.md
118120
docs/UploadFileWithAdditionalPropertiesRequestObject.md
@@ -215,6 +217,7 @@ petstore_api/models/parent.py
215217
petstore_api/models/parent_with_optional_dict.py
216218
petstore_api/models/pet.py
217219
petstore_api/models/pig.py
220+
petstore_api/models/pony_sizes.py
218221
petstore_api/models/poop_cleaning.py
219222
petstore_api/models/primitive_string.py
220223
petstore_api/models/property_map.py
@@ -238,6 +241,7 @@ petstore_api/models/test_inline_freeform_additional_properties_request.py
238241
petstore_api/models/test_model_with_enum_default.py
239242
petstore_api/models/test_object_for_multipart_requests_request_marker.py
240243
petstore_api/models/tiger.py
244+
petstore_api/models/type.py
241245
petstore_api/models/unnamed_dict_with_additional_model_list_properties.py
242246
petstore_api/models/unnamed_dict_with_additional_string_list_properties.py
243247
petstore_api/models/upload_file_with_additional_properties_request_object.py

samples/openapi3/client/petstore/python-aiohttp/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ Class | Method | HTTP request | Description
231231
- [ParentWithOptionalDict](docs/ParentWithOptionalDict.md)
232232
- [Pet](docs/Pet.md)
233233
- [Pig](docs/Pig.md)
234+
- [PonySizes](docs/PonySizes.md)
234235
- [PoopCleaning](docs/PoopCleaning.md)
235236
- [PrimitiveString](docs/PrimitiveString.md)
236237
- [PropertyMap](docs/PropertyMap.md)
@@ -254,6 +255,7 @@ Class | Method | HTTP request | Description
254255
- [TestModelWithEnumDefault](docs/TestModelWithEnumDefault.md)
255256
- [TestObjectForMultipartRequestsRequestMarker](docs/TestObjectForMultipartRequestsRequestMarker.md)
256257
- [Tiger](docs/Tiger.md)
258+
- [Type](docs/Type.md)
257259
- [UnnamedDictWithAdditionalModelListProperties](docs/UnnamedDictWithAdditionalModelListProperties.md)
258260
- [UnnamedDictWithAdditionalStringListProperties](docs/UnnamedDictWithAdditionalStringListProperties.md)
259261
- [UploadFileWithAdditionalPropertiesRequestObject](docs/UploadFileWithAdditionalPropertiesRequestObject.md)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# PonySizes
2+
3+
4+
## Properties
5+
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**type** | [**Type**](Type.md) | | [optional]
9+
10+
## Example
11+
12+
```python
13+
from petstore_api.models.pony_sizes import PonySizes
14+
15+
# TODO update the JSON string below
16+
json = "{}"
17+
# create an instance of PonySizes from a JSON string
18+
pony_sizes_instance = PonySizes.from_json(json)
19+
# print the JSON string representation of the object
20+
print(PonySizes.to_json())
21+
22+
# convert the object into a dict
23+
pony_sizes_dict = pony_sizes_instance.to_dict()
24+
# create an instance of PonySizes from a dict
25+
pony_sizes_from_dict = PonySizes.from_dict(pony_sizes_dict)
26+
```
27+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
28+
29+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Type
2+
3+
4+
## Enum
5+
6+
* `NUMBER_2_DOT_0` (value: `2.0`)
7+
8+
* `NUMBER_1_DOT_0` (value: `1.0`)
9+
10+
* `NUMBER_0_DOT_5` (value: `0.5`)
11+
12+
* `NUMBER_0_DOT_25` (value: `0.25`)
13+
14+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
15+
16+

samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"ParentWithOptionalDict",
117117
"Pet",
118118
"Pig",
119+
"PonySizes",
119120
"PoopCleaning",
120121
"PrimitiveString",
121122
"PropertyMap",
@@ -139,6 +140,7 @@
139140
"TestModelWithEnumDefault",
140141
"TestObjectForMultipartRequestsRequestMarker",
141142
"Tiger",
143+
"Type",
142144
"UnnamedDictWithAdditionalModelListProperties",
143145
"UnnamedDictWithAdditionalStringListProperties",
144146
"UploadFileWithAdditionalPropertiesRequestObject",
@@ -249,6 +251,7 @@
249251
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict as ParentWithOptionalDict
250252
from petstore_api.models.pet import Pet as Pet
251253
from petstore_api.models.pig import Pig as Pig
254+
from petstore_api.models.pony_sizes import PonySizes as PonySizes
252255
from petstore_api.models.poop_cleaning import PoopCleaning as PoopCleaning
253256
from petstore_api.models.primitive_string import PrimitiveString as PrimitiveString
254257
from petstore_api.models.property_map import PropertyMap as PropertyMap
@@ -272,6 +275,7 @@
272275
from petstore_api.models.test_model_with_enum_default import TestModelWithEnumDefault as TestModelWithEnumDefault
273276
from petstore_api.models.test_object_for_multipart_requests_request_marker import TestObjectForMultipartRequestsRequestMarker as TestObjectForMultipartRequestsRequestMarker
274277
from petstore_api.models.tiger import Tiger as Tiger
278+
from petstore_api.models.type import Type as Type
275279
from petstore_api.models.unnamed_dict_with_additional_model_list_properties import UnnamedDictWithAdditionalModelListProperties as UnnamedDictWithAdditionalModelListProperties
276280
from petstore_api.models.unnamed_dict_with_additional_string_list_properties import UnnamedDictWithAdditionalStringListProperties as UnnamedDictWithAdditionalStringListProperties
277281
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject as UploadFileWithAdditionalPropertiesRequestObject

samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
9595
from petstore_api.models.pet import Pet
9696
from petstore_api.models.pig import Pig
97+
from petstore_api.models.pony_sizes import PonySizes
9798
from petstore_api.models.poop_cleaning import PoopCleaning
9899
from petstore_api.models.primitive_string import PrimitiveString
99100
from petstore_api.models.property_map import PropertyMap
@@ -117,6 +118,7 @@
117118
from petstore_api.models.test_model_with_enum_default import TestModelWithEnumDefault
118119
from petstore_api.models.test_object_for_multipart_requests_request_marker import TestObjectForMultipartRequestsRequestMarker
119120
from petstore_api.models.tiger import Tiger
121+
from petstore_api.models.type import Type
120122
from petstore_api.models.unnamed_dict_with_additional_model_list_properties import UnnamedDictWithAdditionalModelListProperties
121123
from petstore_api.models.unnamed_dict_with_additional_string_list_properties import UnnamedDictWithAdditionalStringListProperties
122124
from petstore_api.models.upload_file_with_additional_properties_request_object import UploadFileWithAdditionalPropertiesRequestObject
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# coding: utf-8
2+
3+
"""
4+
OpenAPI Petstore
5+
6+
This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
7+
8+
The version of the OpenAPI document: 1.0.0
9+
Generated by OpenAPI Generator (https://openapi-generator.tech)
10+
11+
Do not edit the class manually.
12+
""" # noqa: E501
13+
14+
15+
from __future__ import annotations
16+
import pprint
17+
import re # noqa: F401
18+
import json
19+
20+
from pydantic import BaseModel, ConfigDict
21+
from typing import Any, ClassVar, Dict, List, Optional
22+
from petstore_api.models.type import Type
23+
from typing import Optional, Set
24+
from typing_extensions import Self
25+
26+
class PonySizes(BaseModel):
27+
"""
28+
PonySizes
29+
""" # noqa: E501
30+
type: Optional[Type] = None
31+
__properties: ClassVar[List[str]] = ["type"]
32+
33+
model_config = ConfigDict(
34+
populate_by_name=True,
35+
validate_assignment=True,
36+
protected_namespaces=(),
37+
)
38+
39+
40+
def to_str(self) -> str:
41+
"""Returns the string representation of the model using alias"""
42+
return pprint.pformat(self.model_dump(by_alias=True))
43+
44+
def to_json(self) -> str:
45+
"""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())
48+
49+
@classmethod
50+
def from_json(cls, json_str: str) -> Optional[Self]:
51+
"""Create an instance of PonySizes from a JSON string"""
52+
return cls.from_dict(json.loads(json_str))
53+
54+
def to_dict(self) -> Dict[str, Any]:
55+
"""Return the dictionary representation of the model using alias.
56+
57+
This has the following differences from calling pydantic's
58+
`self.model_dump(by_alias=True)`:
59+
60+
* `None` is only added to the output dict for nullable fields that
61+
were set at model initialization. Other fields with value `None`
62+
are ignored.
63+
"""
64+
excluded_fields: Set[str] = set([
65+
])
66+
67+
_dict = self.model_dump(
68+
by_alias=True,
69+
exclude=excluded_fields,
70+
exclude_none=True,
71+
)
72+
return _dict
73+
74+
@classmethod
75+
def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
76+
"""Create an instance of PonySizes from a dict"""
77+
if obj is None:
78+
return None
79+
80+
if not isinstance(obj, dict):
81+
return cls.model_validate(obj)
82+
83+
_obj = cls.model_validate({
84+
"type": obj.get("type")
85+
})
86+
return _obj
87+
88+

0 commit comments

Comments
 (0)