Skip to content

Commit 7dee666

Browse files
authored
add python name mapping support (#16120)
1 parent 35ca486 commit 7dee666

File tree

17 files changed

+364
-1
lines changed

17 files changed

+364
-1
lines changed

bin/configs/python.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ additionalProperties:
77
useOneOfDiscriminatorLookup: "true"
88
disallowAdditionalPropertiesIfNotPresent: false
99
mapNumberTo: StrictFloat
10+
nameMappings:
11+
_type: underscore_type
12+
type_: type_with_underscore

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ public String toDefaultValue(Schema p) {
185185

186186
@Override
187187
public String toVarName(String name) {
188+
// obtain the name from nameMapping directly if provided
189+
if (nameMapping.containsKey(name)) {
190+
return nameMapping.get(name);
191+
}
192+
188193
// sanitize name
189194
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
190195

@@ -218,6 +223,11 @@ public String toRegularExpression(String pattern) {
218223

219224
@Override
220225
public String toParamName(String name) {
226+
// obtain the name from nameMapping directly if provided
227+
if (nameMapping.containsKey(name)) {
228+
return nameMapping.get(name);
229+
}
230+
221231
// to avoid conflicts with 'callback' parameter for async call
222232
if ("callback".equals(name)) {
223233
return "param_callback";

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,4 +2229,12 @@ components:
22292229
type: array
22302230
items:
22312231
$ref: "#/components/schemas/Tag"
2232-
2232+
PropertyNameCollision:
2233+
properties:
2234+
_type:
2235+
type: string
2236+
type:
2237+
type: string
2238+
type_:
2239+
type: string
2240+
type: object

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ docs/ParentWithOptionalDict.md
6868
docs/Pet.md
6969
docs/PetApi.md
7070
docs/Pig.md
71+
docs/PropertyNameCollision.md
7172
docs/ReadOnlyFirst.md
7273
docs/SecondRef.md
7374
docs/SelfReferenceModel.md
@@ -155,6 +156,7 @@ petstore_api/models/parent.py
155156
petstore_api/models/parent_with_optional_dict.py
156157
petstore_api/models/pet.py
157158
petstore_api/models/pig.py
159+
petstore_api/models/property_name_collision.py
158160
petstore_api/models/read_only_first.py
159161
petstore_api/models/second_ref.py
160162
petstore_api/models/self_reference_model.py

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Class | Method | HTTP request | Description
192192
- [ParentWithOptionalDict](docs/ParentWithOptionalDict.md)
193193
- [Pet](docs/Pet.md)
194194
- [Pig](docs/Pig.md)
195+
- [PropertyNameCollision](docs/PropertyNameCollision.md)
195196
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
196197
- [SecondRef](docs/SecondRef.md)
197198
- [SelfReferenceModel](docs/SelfReferenceModel.md)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# PropertyNameCollision
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**type** | **str** | | [optional]
8+
**type** | **str** | | [optional]
9+
**type_** | **str** | | [optional]
10+
11+
## Example
12+
13+
```python
14+
from petstore_api.models.property_name_collision import PropertyNameCollision
15+
16+
# TODO update the JSON string below
17+
json = "{}"
18+
# create an instance of PropertyNameCollision from a JSON string
19+
property_name_collision_instance = PropertyNameCollision.from_json(json)
20+
# print the JSON string representation of the object
21+
print PropertyNameCollision.to_json()
22+
23+
# convert the object into a dict
24+
property_name_collision_dict = property_name_collision_instance.to_dict()
25+
# create an instance of PropertyNameCollision from a dict
26+
property_name_collision_form_dict = property_name_collision.from_dict(property_name_collision_dict)
27+
```
28+
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)
29+
30+

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
9999
from petstore_api.models.pet import Pet
100100
from petstore_api.models.pig import Pig
101+
from petstore_api.models.property_name_collision import PropertyNameCollision
101102
from petstore_api.models.read_only_first import ReadOnlyFirst
102103
from petstore_api.models.second_ref import SecondRef
103104
from petstore_api.models.self_reference_model import SelfReferenceModel

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
7575
from petstore_api.models.pet import Pet
7676
from petstore_api.models.pig import Pig
77+
from petstore_api.models.property_name_collision import PropertyNameCollision
7778
from petstore_api.models.read_only_first import ReadOnlyFirst
7879
from petstore_api.models.second_ref import SecondRef
7980
from petstore_api.models.self_reference_model import SelfReferenceModel
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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: \" \\ # noqa: E501
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+
"""
13+
14+
15+
from __future__ import annotations
16+
import pprint
17+
import re # noqa: F401
18+
import json
19+
20+
21+
from typing import Optional
22+
from pydantic import BaseModel, Field, StrictStr
23+
24+
class PropertyNameCollision(BaseModel):
25+
"""
26+
PropertyNameCollision
27+
"""
28+
type: Optional[StrictStr] = Field(None, alias="_type")
29+
type: Optional[StrictStr] = None
30+
type_: Optional[StrictStr] = None
31+
__properties = ["_type", "type", "type_"]
32+
33+
class Config:
34+
"""Pydantic configuration"""
35+
allow_population_by_field_name = True
36+
validate_assignment = True
37+
38+
def to_str(self) -> str:
39+
"""Returns the string representation of the model using alias"""
40+
return pprint.pformat(self.dict(by_alias=True))
41+
42+
def to_json(self) -> str:
43+
"""Returns the JSON representation of the model using alias"""
44+
return json.dumps(self.to_dict())
45+
46+
@classmethod
47+
def from_json(cls, json_str: str) -> PropertyNameCollision:
48+
"""Create an instance of PropertyNameCollision from a JSON string"""
49+
return cls.from_dict(json.loads(json_str))
50+
51+
def to_dict(self):
52+
"""Returns the dictionary representation of the model using alias"""
53+
_dict = self.dict(by_alias=True,
54+
exclude={
55+
},
56+
exclude_none=True)
57+
return _dict
58+
59+
@classmethod
60+
def from_dict(cls, obj: dict) -> PropertyNameCollision:
61+
"""Create an instance of PropertyNameCollision from a dict"""
62+
if obj is None:
63+
return None
64+
65+
if not isinstance(obj, dict):
66+
return PropertyNameCollision.parse_obj(obj)
67+
68+
_obj = PropertyNameCollision.parse_obj({
69+
"type": obj.get("_type"),
70+
"type": obj.get("type"),
71+
"type_": obj.get("type_")
72+
})
73+
return _obj
74+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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: \" \\ # noqa: E501
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+
"""
13+
14+
15+
import unittest
16+
import datetime
17+
18+
import petstore_api
19+
from petstore_api.models.property_name_collision import PropertyNameCollision # noqa: E501
20+
from petstore_api.rest import ApiException
21+
22+
class TestPropertyNameCollision(unittest.TestCase):
23+
"""PropertyNameCollision unit test stubs"""
24+
25+
def setUp(self):
26+
pass
27+
28+
def tearDown(self):
29+
pass
30+
31+
def make_instance(self, include_optional):
32+
"""Test PropertyNameCollision
33+
include_option is a boolean, when False only required
34+
params are included, when True both required and
35+
optional params are included """
36+
# uncomment below to create an instance of `PropertyNameCollision`
37+
"""
38+
model = petstore_api.models.property_name_collision.PropertyNameCollision() # noqa: E501
39+
if include_optional :
40+
return PropertyNameCollision(
41+
type = '',
42+
type = '',
43+
type_ = ''
44+
)
45+
else :
46+
return PropertyNameCollision(
47+
)
48+
"""
49+
50+
def testPropertyNameCollision(self):
51+
"""Test PropertyNameCollision"""
52+
# inst_req_only = self.make_instance(include_optional=False)
53+
# inst_req_and_optional = self.make_instance(include_optional=True)
54+
55+
if __name__ == '__main__':
56+
unittest.main()

0 commit comments

Comments
 (0)