forked from loafle/openapi-generator-original
[python] Add tests for simple object with additional properties (#16218)
* add tests for simple object without additional properties * update logic
This commit is contained in:
parent
d9fc039dcf
commit
d41fe12c1f
@ -1239,12 +1239,12 @@ public class PythonClientCodegen extends AbstractPythonCodegen implements Codege
|
||||
codegenProperties = model.vars;
|
||||
}
|
||||
|
||||
// if model_generic.mustache is used
|
||||
if (model.oneOf.isEmpty() && !model.isEnum) {
|
||||
if (!this.disallowAdditionalPropertiesIfNotPresent) {
|
||||
typingImports.add("Dict");
|
||||
typingImports.add("Any");
|
||||
}
|
||||
// if model_generic.mustache is used and support additionalProperties
|
||||
if (model.oneOf.isEmpty() && model.anyOf.isEmpty()
|
||||
&& !model.isEnum
|
||||
&& !this.disallowAdditionalPropertiesIfNotPresent) {
|
||||
typingImports.add("Dict");
|
||||
typingImports.add("Any");
|
||||
}
|
||||
|
||||
//loop through properties/schemas to set up typing, pydantic
|
||||
|
@ -2238,3 +2238,11 @@ components:
|
||||
type_:
|
||||
type: string
|
||||
type: object
|
||||
ObjectToTestAdditionalProperties:
|
||||
description: Minimal object
|
||||
type: object
|
||||
properties:
|
||||
property:
|
||||
description: Property
|
||||
type: boolean
|
||||
default: false
|
||||
|
@ -54,6 +54,7 @@ docs/Name.md
|
||||
docs/NullableClass.md
|
||||
docs/NullableProperty.md
|
||||
docs/NumberOnly.md
|
||||
docs/ObjectToTestAdditionalProperties.md
|
||||
docs/ObjectWithDeprecatedFields.md
|
||||
docs/OneOfEnumString.md
|
||||
docs/Order.md
|
||||
@ -143,6 +144,7 @@ petstore_api/models/name.py
|
||||
petstore_api/models/nullable_class.py
|
||||
petstore_api/models/nullable_property.py
|
||||
petstore_api/models/number_only.py
|
||||
petstore_api/models/object_to_test_additional_properties.py
|
||||
petstore_api/models/object_with_deprecated_fields.py
|
||||
petstore_api/models/one_of_enum_string.py
|
||||
petstore_api/models/order.py
|
||||
|
@ -179,6 +179,7 @@ Class | Method | HTTP request | Description
|
||||
- [NullableClass](docs/NullableClass.md)
|
||||
- [NullableProperty](docs/NullableProperty.md)
|
||||
- [NumberOnly](docs/NumberOnly.md)
|
||||
- [ObjectToTestAdditionalProperties](docs/ObjectToTestAdditionalProperties.md)
|
||||
- [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
|
||||
- [OneOfEnumString](docs/OneOfEnumString.md)
|
||||
- [Order](docs/Order.md)
|
||||
|
@ -0,0 +1,29 @@
|
||||
# ObjectToTestAdditionalProperties
|
||||
|
||||
Minimal object
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**var_property** | **bool** | Property | [optional] [default to False]
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.object_to_test_additional_properties import ObjectToTestAdditionalProperties
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of ObjectToTestAdditionalProperties from a JSON string
|
||||
object_to_test_additional_properties_instance = ObjectToTestAdditionalProperties.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print ObjectToTestAdditionalProperties.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
object_to_test_additional_properties_dict = object_to_test_additional_properties_instance.to_dict()
|
||||
# create an instance of ObjectToTestAdditionalProperties from a dict
|
||||
object_to_test_additional_properties_form_dict = object_to_test_additional_properties.from_dict(object_to_test_additional_properties_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)
|
||||
|
||||
|
@ -85,6 +85,7 @@ from petstore_api.models.name import Name
|
||||
from petstore_api.models.nullable_class import NullableClass
|
||||
from petstore_api.models.nullable_property import NullableProperty
|
||||
from petstore_api.models.number_only import NumberOnly
|
||||
from petstore_api.models.object_to_test_additional_properties import ObjectToTestAdditionalProperties
|
||||
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
|
||||
from petstore_api.models.one_of_enum_string import OneOfEnumString
|
||||
from petstore_api.models.order import Order
|
||||
|
@ -61,6 +61,7 @@ from petstore_api.models.name import Name
|
||||
from petstore_api.models.nullable_class import NullableClass
|
||||
from petstore_api.models.nullable_property import NullableProperty
|
||||
from petstore_api.models.number_only import NumberOnly
|
||||
from petstore_api.models.object_to_test_additional_properties import ObjectToTestAdditionalProperties
|
||||
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
|
||||
from petstore_api.models.one_of_enum_string import OneOfEnumString
|
||||
from petstore_api.models.order import Order
|
||||
|
@ -0,0 +1,70 @@
|
||||
# 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, StrictBool
|
||||
|
||||
class ObjectToTestAdditionalProperties(BaseModel):
|
||||
"""
|
||||
Minimal object
|
||||
"""
|
||||
var_property: Optional[StrictBool] = Field(False, alias="property", description="Property")
|
||||
__properties = ["property"]
|
||||
|
||||
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) -> ObjectToTestAdditionalProperties:
|
||||
"""Create an instance of ObjectToTestAdditionalProperties 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) -> ObjectToTestAdditionalProperties:
|
||||
"""Create an instance of ObjectToTestAdditionalProperties from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return ObjectToTestAdditionalProperties.parse_obj(obj)
|
||||
|
||||
_obj = ObjectToTestAdditionalProperties.parse_obj({
|
||||
"var_property": obj.get("property") if obj.get("property") is not None else False
|
||||
})
|
||||
return _obj
|
||||
|
@ -0,0 +1,54 @@
|
||||
# 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.object_to_test_additional_properties import ObjectToTestAdditionalProperties # noqa: E501
|
||||
from petstore_api.rest import ApiException
|
||||
|
||||
class TestObjectToTestAdditionalProperties(unittest.TestCase):
|
||||
"""ObjectToTestAdditionalProperties unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional):
|
||||
"""Test ObjectToTestAdditionalProperties
|
||||
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 `ObjectToTestAdditionalProperties`
|
||||
"""
|
||||
model = petstore_api.models.object_to_test_additional_properties.ObjectToTestAdditionalProperties() # noqa: E501
|
||||
if include_optional :
|
||||
return ObjectToTestAdditionalProperties(
|
||||
var_property = True
|
||||
)
|
||||
else :
|
||||
return ObjectToTestAdditionalProperties(
|
||||
)
|
||||
"""
|
||||
|
||||
def testObjectToTestAdditionalProperties(self):
|
||||
"""Test ObjectToTestAdditionalProperties"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -54,6 +54,7 @@ docs/Name.md
|
||||
docs/NullableClass.md
|
||||
docs/NullableProperty.md
|
||||
docs/NumberOnly.md
|
||||
docs/ObjectToTestAdditionalProperties.md
|
||||
docs/ObjectWithDeprecatedFields.md
|
||||
docs/OneOfEnumString.md
|
||||
docs/Order.md
|
||||
@ -143,6 +144,7 @@ petstore_api/models/name.py
|
||||
petstore_api/models/nullable_class.py
|
||||
petstore_api/models/nullable_property.py
|
||||
petstore_api/models/number_only.py
|
||||
petstore_api/models/object_to_test_additional_properties.py
|
||||
petstore_api/models/object_with_deprecated_fields.py
|
||||
petstore_api/models/one_of_enum_string.py
|
||||
petstore_api/models/order.py
|
||||
|
@ -179,6 +179,7 @@ Class | Method | HTTP request | Description
|
||||
- [NullableClass](docs/NullableClass.md)
|
||||
- [NullableProperty](docs/NullableProperty.md)
|
||||
- [NumberOnly](docs/NumberOnly.md)
|
||||
- [ObjectToTestAdditionalProperties](docs/ObjectToTestAdditionalProperties.md)
|
||||
- [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md)
|
||||
- [OneOfEnumString](docs/OneOfEnumString.md)
|
||||
- [Order](docs/Order.md)
|
||||
|
@ -0,0 +1,29 @@
|
||||
# ObjectToTestAdditionalProperties
|
||||
|
||||
Minimal object
|
||||
|
||||
## Properties
|
||||
Name | Type | Description | Notes
|
||||
------------ | ------------- | ------------- | -------------
|
||||
**var_property** | **bool** | Property | [optional] [default to False]
|
||||
|
||||
## Example
|
||||
|
||||
```python
|
||||
from petstore_api.models.object_to_test_additional_properties import ObjectToTestAdditionalProperties
|
||||
|
||||
# TODO update the JSON string below
|
||||
json = "{}"
|
||||
# create an instance of ObjectToTestAdditionalProperties from a JSON string
|
||||
object_to_test_additional_properties_instance = ObjectToTestAdditionalProperties.from_json(json)
|
||||
# print the JSON string representation of the object
|
||||
print ObjectToTestAdditionalProperties.to_json()
|
||||
|
||||
# convert the object into a dict
|
||||
object_to_test_additional_properties_dict = object_to_test_additional_properties_instance.to_dict()
|
||||
# create an instance of ObjectToTestAdditionalProperties from a dict
|
||||
object_to_test_additional_properties_form_dict = object_to_test_additional_properties.from_dict(object_to_test_additional_properties_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)
|
||||
|
||||
|
@ -85,6 +85,7 @@ from petstore_api.models.name import Name
|
||||
from petstore_api.models.nullable_class import NullableClass
|
||||
from petstore_api.models.nullable_property import NullableProperty
|
||||
from petstore_api.models.number_only import NumberOnly
|
||||
from petstore_api.models.object_to_test_additional_properties import ObjectToTestAdditionalProperties
|
||||
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
|
||||
from petstore_api.models.one_of_enum_string import OneOfEnumString
|
||||
from petstore_api.models.order import Order
|
||||
|
@ -61,6 +61,7 @@ from petstore_api.models.name import Name
|
||||
from petstore_api.models.nullable_class import NullableClass
|
||||
from petstore_api.models.nullable_property import NullableProperty
|
||||
from petstore_api.models.number_only import NumberOnly
|
||||
from petstore_api.models.object_to_test_additional_properties import ObjectToTestAdditionalProperties
|
||||
from petstore_api.models.object_with_deprecated_fields import ObjectWithDeprecatedFields
|
||||
from petstore_api.models.one_of_enum_string import OneOfEnumString
|
||||
from petstore_api.models.order import Order
|
||||
|
@ -18,7 +18,7 @@ import json
|
||||
import pprint
|
||||
import re # noqa: F401
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import List, Optional
|
||||
from pydantic import BaseModel, Field, StrictStr, ValidationError, conint, conlist, constr, validator
|
||||
from typing import Union, Any, List, TYPE_CHECKING
|
||||
from pydantic import StrictStr, Field
|
||||
|
@ -18,7 +18,7 @@ import json
|
||||
import pprint
|
||||
import re # noqa: F401
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Optional
|
||||
from pydantic import BaseModel, Field, StrictStr, ValidationError, validator
|
||||
from petstore_api.models.basque_pig import BasquePig
|
||||
from petstore_api.models.danish_pig import DanishPig
|
||||
|
@ -0,0 +1,82 @@
|
||||
# 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 Any, Dict, Optional
|
||||
from pydantic import BaseModel, Field, StrictBool
|
||||
|
||||
class ObjectToTestAdditionalProperties(BaseModel):
|
||||
"""
|
||||
Minimal object
|
||||
"""
|
||||
var_property: Optional[StrictBool] = Field(False, alias="property", description="Property")
|
||||
additional_properties: Dict[str, Any] = {}
|
||||
__properties = ["property"]
|
||||
|
||||
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) -> ObjectToTestAdditionalProperties:
|
||||
"""Create an instance of ObjectToTestAdditionalProperties 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) -> ObjectToTestAdditionalProperties:
|
||||
"""Create an instance of ObjectToTestAdditionalProperties from a dict"""
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if not isinstance(obj, dict):
|
||||
return ObjectToTestAdditionalProperties.parse_obj(obj)
|
||||
|
||||
_obj = ObjectToTestAdditionalProperties.parse_obj({
|
||||
"var_property": obj.get("property") if obj.get("property") is not None else False
|
||||
})
|
||||
# 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
|
||||
|
@ -0,0 +1,54 @@
|
||||
# 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.object_to_test_additional_properties import ObjectToTestAdditionalProperties # noqa: E501
|
||||
from petstore_api.rest import ApiException
|
||||
|
||||
class TestObjectToTestAdditionalProperties(unittest.TestCase):
|
||||
"""ObjectToTestAdditionalProperties unit test stubs"""
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def make_instance(self, include_optional):
|
||||
"""Test ObjectToTestAdditionalProperties
|
||||
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 `ObjectToTestAdditionalProperties`
|
||||
"""
|
||||
model = petstore_api.models.object_to_test_additional_properties.ObjectToTestAdditionalProperties() # noqa: E501
|
||||
if include_optional :
|
||||
return ObjectToTestAdditionalProperties(
|
||||
var_property = True
|
||||
)
|
||||
else :
|
||||
return ObjectToTestAdditionalProperties(
|
||||
)
|
||||
"""
|
||||
|
||||
def testObjectToTestAdditionalProperties(self):
|
||||
"""Test ObjectToTestAdditionalProperties"""
|
||||
# inst_req_only = self.make_instance(include_optional=False)
|
||||
# inst_req_and_optional = self.make_instance(include_optional=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
@ -501,3 +501,10 @@ class ModelTests(unittest.TestCase):
|
||||
self.assertEqual(a.to_json(), '{"another_property": [[{"id": 123, "name": "tag name"}]]}')
|
||||
a2 = petstore_api.ArrayOfArrayOfModel.from_dict(a.to_dict())
|
||||
self.assertEqual(a.to_dict(), a2.to_dict())
|
||||
|
||||
def test_object_with_additional_properties(self):
|
||||
a = petstore_api.ObjectToTestAdditionalProperties()
|
||||
a.additional_properties = { "abc": 123 }
|
||||
# should not throw the following errors:
|
||||
# pydantic.errors.ConfigError: field "additional_properties" not yet prepared so type is still a ForwardRef, you might need to call ObjectToTestAdditionalProperties.update_forward_refs().
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user