add python name mapping support (#16120)

This commit is contained in:
William Cheng
2023-07-20 17:30:01 +08:00
committed by GitHub
parent 35ca486e1c
commit 7dee666826
17 changed files with 364 additions and 1 deletions

View File

@@ -7,3 +7,6 @@ additionalProperties:
useOneOfDiscriminatorLookup: "true"
disallowAdditionalPropertiesIfNotPresent: false
mapNumberTo: StrictFloat
nameMappings:
_type: underscore_type
type_: type_with_underscore

View File

@@ -185,6 +185,11 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
@Override
public String toVarName(String name) {
// obtain the name from nameMapping directly if provided
if (nameMapping.containsKey(name)) {
return nameMapping.get(name);
}
// sanitize name
name = sanitizeName(name); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
@@ -218,6 +223,11 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co
@Override
public String toParamName(String name) {
// obtain the name from nameMapping directly if provided
if (nameMapping.containsKey(name)) {
return nameMapping.get(name);
}
// to avoid conflicts with 'callback' parameter for async call
if ("callback".equals(name)) {
return "param_callback";

View File

@@ -2229,4 +2229,12 @@ components:
type: array
items:
$ref: "#/components/schemas/Tag"
PropertyNameCollision:
properties:
_type:
type: string
type:
type: string
type_:
type: string
type: object

View File

@@ -68,6 +68,7 @@ docs/ParentWithOptionalDict.md
docs/Pet.md
docs/PetApi.md
docs/Pig.md
docs/PropertyNameCollision.md
docs/ReadOnlyFirst.md
docs/SecondRef.md
docs/SelfReferenceModel.md
@@ -155,6 +156,7 @@ petstore_api/models/parent.py
petstore_api/models/parent_with_optional_dict.py
petstore_api/models/pet.py
petstore_api/models/pig.py
petstore_api/models/property_name_collision.py
petstore_api/models/read_only_first.py
petstore_api/models/second_ref.py
petstore_api/models/self_reference_model.py

View File

@@ -192,6 +192,7 @@ Class | Method | HTTP request | Description
- [ParentWithOptionalDict](docs/ParentWithOptionalDict.md)
- [Pet](docs/Pet.md)
- [Pig](docs/Pig.md)
- [PropertyNameCollision](docs/PropertyNameCollision.md)
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [SecondRef](docs/SecondRef.md)
- [SelfReferenceModel](docs/SelfReferenceModel.md)

View File

@@ -0,0 +1,30 @@
# PropertyNameCollision
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**type** | **str** | | [optional]
**type** | **str** | | [optional]
**type_** | **str** | | [optional]
## Example
```python
from petstore_api.models.property_name_collision import PropertyNameCollision
# TODO update the JSON string below
json = "{}"
# create an instance of PropertyNameCollision from a JSON string
property_name_collision_instance = PropertyNameCollision.from_json(json)
# print the JSON string representation of the object
print PropertyNameCollision.to_json()
# convert the object into a dict
property_name_collision_dict = property_name_collision_instance.to_dict()
# create an instance of PropertyNameCollision from a dict
property_name_collision_form_dict = property_name_collision.from_dict(property_name_collision_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -98,6 +98,7 @@ from petstore_api.models.parent import Parent
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
from petstore_api.models.pet import Pet
from petstore_api.models.pig import Pig
from petstore_api.models.property_name_collision import PropertyNameCollision
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.second_ref import SecondRef
from petstore_api.models.self_reference_model import SelfReferenceModel

View File

@@ -74,6 +74,7 @@ from petstore_api.models.parent import Parent
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
from petstore_api.models.pet import Pet
from petstore_api.models.pig import Pig
from petstore_api.models.property_name_collision import PropertyNameCollision
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.second_ref import SecondRef
from petstore_api.models.self_reference_model import SelfReferenceModel

View File

@@ -0,0 +1,74 @@
# coding: utf-8
"""
OpenAPI Petstore
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
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
from __future__ import annotations
import pprint
import re # noqa: F401
import json
from typing import Optional
from pydantic import BaseModel, Field, StrictStr
class PropertyNameCollision(BaseModel):
"""
PropertyNameCollision
"""
type: Optional[StrictStr] = Field(None, alias="_type")
type: Optional[StrictStr] = None
type_: Optional[StrictStr] = None
__properties = ["_type", "type", "type_"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.dict(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
return json.dumps(self.to_dict())
@classmethod
def from_json(cls, json_str: str) -> PropertyNameCollision:
"""Create an instance of PropertyNameCollision from a JSON string"""
return cls.from_dict(json.loads(json_str))
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
exclude={
},
exclude_none=True)
return _dict
@classmethod
def from_dict(cls, obj: dict) -> PropertyNameCollision:
"""Create an instance of PropertyNameCollision from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return PropertyNameCollision.parse_obj(obj)
_obj = PropertyNameCollision.parse_obj({
"type": obj.get("_type"),
"type": obj.get("type"),
"type_": obj.get("type_")
})
return _obj

View File

@@ -0,0 +1,56 @@
# coding: utf-8
"""
OpenAPI Petstore
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
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
import unittest
import datetime
import petstore_api
from petstore_api.models.property_name_collision import PropertyNameCollision # noqa: E501
from petstore_api.rest import ApiException
class TestPropertyNameCollision(unittest.TestCase):
"""PropertyNameCollision unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def make_instance(self, include_optional):
"""Test PropertyNameCollision
include_option is a boolean, when False only required
params are included, when True both required and
optional params are included """
# uncomment below to create an instance of `PropertyNameCollision`
"""
model = petstore_api.models.property_name_collision.PropertyNameCollision() # noqa: E501
if include_optional :
return PropertyNameCollision(
type = '',
type = '',
type_ = ''
)
else :
return PropertyNameCollision(
)
"""
def testPropertyNameCollision(self):
"""Test PropertyNameCollision"""
# inst_req_only = self.make_instance(include_optional=False)
# inst_req_and_optional = self.make_instance(include_optional=True)
if __name__ == '__main__':
unittest.main()

View File

@@ -68,6 +68,7 @@ docs/ParentWithOptionalDict.md
docs/Pet.md
docs/PetApi.md
docs/Pig.md
docs/PropertyNameCollision.md
docs/ReadOnlyFirst.md
docs/SecondRef.md
docs/SelfReferenceModel.md
@@ -155,6 +156,7 @@ petstore_api/models/parent.py
petstore_api/models/parent_with_optional_dict.py
petstore_api/models/pet.py
petstore_api/models/pig.py
petstore_api/models/property_name_collision.py
petstore_api/models/read_only_first.py
petstore_api/models/second_ref.py
petstore_api/models/self_reference_model.py

View File

@@ -192,6 +192,7 @@ Class | Method | HTTP request | Description
- [ParentWithOptionalDict](docs/ParentWithOptionalDict.md)
- [Pet](docs/Pet.md)
- [Pig](docs/Pig.md)
- [PropertyNameCollision](docs/PropertyNameCollision.md)
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [SecondRef](docs/SecondRef.md)
- [SelfReferenceModel](docs/SelfReferenceModel.md)

View File

@@ -0,0 +1,30 @@
# PropertyNameCollision
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**underscore_type** | **str** | | [optional]
**type** | **str** | | [optional]
**type_with_underscore** | **str** | | [optional]
## Example
```python
from petstore_api.models.property_name_collision import PropertyNameCollision
# TODO update the JSON string below
json = "{}"
# create an instance of PropertyNameCollision from a JSON string
property_name_collision_instance = PropertyNameCollision.from_json(json)
# print the JSON string representation of the object
print PropertyNameCollision.to_json()
# convert the object into a dict
property_name_collision_dict = property_name_collision_instance.to_dict()
# create an instance of PropertyNameCollision from a dict
property_name_collision_form_dict = property_name_collision.from_dict(property_name_collision_dict)
```
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@@ -98,6 +98,7 @@ from petstore_api.models.parent import Parent
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
from petstore_api.models.pet import Pet
from petstore_api.models.pig import Pig
from petstore_api.models.property_name_collision import PropertyNameCollision
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.second_ref import SecondRef
from petstore_api.models.self_reference_model import SelfReferenceModel

View File

@@ -74,6 +74,7 @@ from petstore_api.models.parent import Parent
from petstore_api.models.parent_with_optional_dict import ParentWithOptionalDict
from petstore_api.models.pet import Pet
from petstore_api.models.pig import Pig
from petstore_api.models.property_name_collision import PropertyNameCollision
from petstore_api.models.read_only_first import ReadOnlyFirst
from petstore_api.models.second_ref import SecondRef
from petstore_api.models.self_reference_model import SelfReferenceModel

View File

@@ -0,0 +1,86 @@
# coding: utf-8
"""
OpenAPI Petstore
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
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
from __future__ import annotations
import pprint
import re # noqa: F401
import json
from typing import Optional
from pydantic import BaseModel, Field, StrictStr
class PropertyNameCollision(BaseModel):
"""
PropertyNameCollision
"""
underscore_type: Optional[StrictStr] = Field(None, alias="_type")
type: Optional[StrictStr] = None
type_with_underscore: Optional[StrictStr] = Field(None, alias="type_")
additional_properties: Dict[str, Any] = {}
__properties = ["_type", "type", "type_"]
class Config:
"""Pydantic configuration"""
allow_population_by_field_name = True
validate_assignment = True
def to_str(self) -> str:
"""Returns the string representation of the model using alias"""
return pprint.pformat(self.dict(by_alias=True))
def to_json(self) -> str:
"""Returns the JSON representation of the model using alias"""
return json.dumps(self.to_dict())
@classmethod
def from_json(cls, json_str: str) -> PropertyNameCollision:
"""Create an instance of PropertyNameCollision from a JSON string"""
return cls.from_dict(json.loads(json_str))
def to_dict(self):
"""Returns the dictionary representation of the model using alias"""
_dict = self.dict(by_alias=True,
exclude={
"additional_properties"
},
exclude_none=True)
# puts key-value pairs in additional_properties in the top level
if self.additional_properties is not None:
for _key, _value in self.additional_properties.items():
_dict[_key] = _value
return _dict
@classmethod
def from_dict(cls, obj: dict) -> PropertyNameCollision:
"""Create an instance of PropertyNameCollision from a dict"""
if obj is None:
return None
if not isinstance(obj, dict):
return PropertyNameCollision.parse_obj(obj)
_obj = PropertyNameCollision.parse_obj({
"underscore_type": obj.get("_type"),
"type": obj.get("type"),
"type_with_underscore": obj.get("type_")
})
# store additional fields in additional_properties
for _key in obj.keys():
if _key not in cls.__properties:
_obj.additional_properties[_key] = obj.get(_key)
return _obj

View File

@@ -0,0 +1,56 @@
# coding: utf-8
"""
OpenAPI Petstore
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
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)
Do not edit the class manually.
"""
import unittest
import datetime
import petstore_api
from petstore_api.models.property_name_collision import PropertyNameCollision # noqa: E501
from petstore_api.rest import ApiException
class TestPropertyNameCollision(unittest.TestCase):
"""PropertyNameCollision unit test stubs"""
def setUp(self):
pass
def tearDown(self):
pass
def make_instance(self, include_optional):
"""Test PropertyNameCollision
include_option is a boolean, when False only required
params are included, when True both required and
optional params are included """
# uncomment below to create an instance of `PropertyNameCollision`
"""
model = petstore_api.models.property_name_collision.PropertyNameCollision() # noqa: E501
if include_optional :
return PropertyNameCollision(
underscoreType = '',
type = '',
typeWithUnderscore = ''
)
else :
return PropertyNameCollision(
)
"""
def testPropertyNameCollision(self):
"""Test PropertyNameCollision"""
# inst_req_only = self.make_instance(include_optional=False)
# inst_req_and_optional = self.make_instance(include_optional=True)
if __name__ == '__main__':
unittest.main()