diff --git a/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml b/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml index c7ea2b82a87e..570084a2d103 100644 --- a/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/python/petstore-with-fake-endpoints-models-for-testing.yaml @@ -2199,3 +2199,16 @@ components: - type: integer minimum: 10 - type: string + NullableProperty: + type: object + required: + - id + - name + properties: + id: + type: integer + format: int64 + name: + type: string + nullable: true + pattern: "^[A-Z].*" diff --git a/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES b/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES index e17417e7c4b0..ac54bd8cece6 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python-aiohttp/.openapi-generator/FILES @@ -52,6 +52,7 @@ docs/Model200Response.md docs/ModelReturn.md docs/Name.md docs/NullableClass.md +docs/NullableProperty.md docs/NumberOnly.md docs/ObjectWithDeprecatedFields.md docs/OneOfEnumString.md @@ -139,6 +140,7 @@ petstore_api/models/model200_response.py petstore_api/models/model_return.py 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_with_deprecated_fields.py petstore_api/models/one_of_enum_string.py diff --git a/samples/openapi3/client/petstore/python-aiohttp/README.md b/samples/openapi3/client/petstore/python-aiohttp/README.md index 202e673e213d..809e439d8c64 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/README.md +++ b/samples/openapi3/client/petstore/python-aiohttp/README.md @@ -177,6 +177,7 @@ Class | Method | HTTP request | Description - [ModelReturn](docs/ModelReturn.md) - [Name](docs/Name.md) - [NullableClass](docs/NullableClass.md) + - [NullableProperty](docs/NullableProperty.md) - [NumberOnly](docs/NumberOnly.md) - [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md) - [OneOfEnumString](docs/OneOfEnumString.md) diff --git a/samples/openapi3/client/petstore/python-aiohttp/docs/NullableProperty.md b/samples/openapi3/client/petstore/python-aiohttp/docs/NullableProperty.md new file mode 100644 index 000000000000..fd9cbbc51aaa --- /dev/null +++ b/samples/openapi3/client/petstore/python-aiohttp/docs/NullableProperty.md @@ -0,0 +1,29 @@ +# NullableProperty + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**id** | **int** | | +**name** | **str** | | + +## Example + +```python +from petstore_api.models.nullable_property import NullableProperty + +# TODO update the JSON string below +json = "{}" +# create an instance of NullableProperty from a JSON string +nullable_property_instance = NullableProperty.from_json(json) +# print the JSON string representation of the object +print NullableProperty.to_json() + +# convert the object into a dict +nullable_property_dict = nullable_property_instance.to_dict() +# create an instance of NullableProperty from a dict +nullable_property_form_dict = nullable_property.from_dict(nullable_property_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) + + diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py index d4bbb4133f7f..b1196659ff31 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/__init__.py @@ -83,6 +83,7 @@ from petstore_api.models.model200_response import Model200Response from petstore_api.models.model_return import ModelReturn 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_with_deprecated_fields import ObjectWithDeprecatedFields from petstore_api.models.one_of_enum_string import OneOfEnumString diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py index 8412641c0649..723ce3396114 100644 --- a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/__init__.py @@ -59,6 +59,7 @@ from petstore_api.models.model200_response import Model200Response from petstore_api.models.model_return import ModelReturn 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_with_deprecated_fields import ObjectWithDeprecatedFields from petstore_api.models.one_of_enum_string import OneOfEnumString diff --git a/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py new file mode 100644 index 000000000000..f8cc9a8aa01d --- /dev/null +++ b/samples/openapi3/client/petstore/python-aiohttp/petstore_api/models/nullable_property.py @@ -0,0 +1,87 @@ +# 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, StrictInt, constr, validator + +class NullableProperty(BaseModel): + """ + NullableProperty + """ + id: StrictInt = Field(...) + name: Optional[constr(strict=True)] = Field(...) + __properties = ["id", "name"] + + @validator('name') + def name_validate_regular_expression(cls, value): + """Validates the regular expression""" + if value is None: + return value + + if not re.match(r"^[A-Z].*", value): + raise ValueError(r"must validate the regular expression /^[A-Z].*/") + return value + + 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) -> NullableProperty: + """Create an instance of NullableProperty 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) + # set to None if name (nullable) is None + # and __fields_set__ contains the field + if self.name is None and "name" in self.__fields_set__: + _dict['name'] = None + + return _dict + + @classmethod + def from_dict(cls, obj: dict) -> NullableProperty: + """Create an instance of NullableProperty from a dict""" + if obj is None: + return None + + if not isinstance(obj, dict): + return NullableProperty.parse_obj(obj) + + _obj = NullableProperty.parse_obj({ + "id": obj.get("id"), + "name": obj.get("name") + }) + return _obj + diff --git a/samples/openapi3/client/petstore/python-aiohttp/test/test_nullable_property.py b/samples/openapi3/client/petstore/python-aiohttp/test/test_nullable_property.py new file mode 100644 index 000000000000..34129457b1d7 --- /dev/null +++ b/samples/openapi3/client/petstore/python-aiohttp/test/test_nullable_property.py @@ -0,0 +1,57 @@ +# 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.nullable_property import NullableProperty # noqa: E501 +from petstore_api.rest import ApiException + +class TestNullableProperty(unittest.TestCase): + """NullableProperty unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional): + """Test NullableProperty + 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 `NullableProperty` + """ + model = petstore_api.models.nullable_property.NullableProperty() # noqa: E501 + if include_optional : + return NullableProperty( + id = 56, + name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>' + ) + else : + return NullableProperty( + id = 56, + name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>', + ) + """ + + def testNullableProperty(self): + """Test NullableProperty""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main() diff --git a/samples/openapi3/client/petstore/python/.openapi-generator/FILES b/samples/openapi3/client/petstore/python/.openapi-generator/FILES index dcecb2103872..d64c74f254df 100755 --- a/samples/openapi3/client/petstore/python/.openapi-generator/FILES +++ b/samples/openapi3/client/petstore/python/.openapi-generator/FILES @@ -52,6 +52,7 @@ docs/Model200Response.md docs/ModelReturn.md docs/Name.md docs/NullableClass.md +docs/NullableProperty.md docs/NumberOnly.md docs/ObjectWithDeprecatedFields.md docs/OneOfEnumString.md @@ -139,6 +140,7 @@ petstore_api/models/model200_response.py petstore_api/models/model_return.py 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_with_deprecated_fields.py petstore_api/models/one_of_enum_string.py diff --git a/samples/openapi3/client/petstore/python/README.md b/samples/openapi3/client/petstore/python/README.md index 734e5a01124b..952f0acd1610 100755 --- a/samples/openapi3/client/petstore/python/README.md +++ b/samples/openapi3/client/petstore/python/README.md @@ -177,6 +177,7 @@ Class | Method | HTTP request | Description - [ModelReturn](docs/ModelReturn.md) - [Name](docs/Name.md) - [NullableClass](docs/NullableClass.md) + - [NullableProperty](docs/NullableProperty.md) - [NumberOnly](docs/NumberOnly.md) - [ObjectWithDeprecatedFields](docs/ObjectWithDeprecatedFields.md) - [OneOfEnumString](docs/OneOfEnumString.md) diff --git a/samples/openapi3/client/petstore/python/docs/NullableProperty.md b/samples/openapi3/client/petstore/python/docs/NullableProperty.md new file mode 100644 index 000000000000..fd9cbbc51aaa --- /dev/null +++ b/samples/openapi3/client/petstore/python/docs/NullableProperty.md @@ -0,0 +1,29 @@ +# NullableProperty + + +## Properties +Name | Type | Description | Notes +------------ | ------------- | ------------- | ------------- +**id** | **int** | | +**name** | **str** | | + +## Example + +```python +from petstore_api.models.nullable_property import NullableProperty + +# TODO update the JSON string below +json = "{}" +# create an instance of NullableProperty from a JSON string +nullable_property_instance = NullableProperty.from_json(json) +# print the JSON string representation of the object +print NullableProperty.to_json() + +# convert the object into a dict +nullable_property_dict = nullable_property_instance.to_dict() +# create an instance of NullableProperty from a dict +nullable_property_form_dict = nullable_property.from_dict(nullable_property_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) + + diff --git a/samples/openapi3/client/petstore/python/petstore_api/__init__.py b/samples/openapi3/client/petstore/python/petstore_api/__init__.py index d4bbb4133f7f..b1196659ff31 100755 --- a/samples/openapi3/client/petstore/python/petstore_api/__init__.py +++ b/samples/openapi3/client/petstore/python/petstore_api/__init__.py @@ -83,6 +83,7 @@ from petstore_api.models.model200_response import Model200Response from petstore_api.models.model_return import ModelReturn 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_with_deprecated_fields import ObjectWithDeprecatedFields from petstore_api.models.one_of_enum_string import OneOfEnumString diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py b/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py index 8412641c0649..723ce3396114 100644 --- a/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py +++ b/samples/openapi3/client/petstore/python/petstore_api/models/__init__.py @@ -59,6 +59,7 @@ from petstore_api.models.model200_response import Model200Response from petstore_api.models.model_return import ModelReturn 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_with_deprecated_fields import ObjectWithDeprecatedFields from petstore_api.models.one_of_enum_string import OneOfEnumString diff --git a/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py b/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py new file mode 100644 index 000000000000..10e359e7df67 --- /dev/null +++ b/samples/openapi3/client/petstore/python/petstore_api/models/nullable_property.py @@ -0,0 +1,99 @@ +# 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, StrictInt, constr, validator + +class NullableProperty(BaseModel): + """ + NullableProperty + """ + id: StrictInt = Field(...) + name: Optional[constr(strict=True)] = Field(...) + additional_properties: Dict[str, Any] = {} + __properties = ["id", "name"] + + @validator('name') + def name_validate_regular_expression(cls, value): + """Validates the regular expression""" + if value is None: + return value + + if not re.match(r"^[A-Z].*", value): + raise ValueError(r"must validate the regular expression /^[A-Z].*/") + return value + + 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) -> NullableProperty: + """Create an instance of NullableProperty 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 + + # set to None if name (nullable) is None + # and __fields_set__ contains the field + if self.name is None and "name" in self.__fields_set__: + _dict['name'] = None + + return _dict + + @classmethod + def from_dict(cls, obj: dict) -> NullableProperty: + """Create an instance of NullableProperty from a dict""" + if obj is None: + return None + + if not isinstance(obj, dict): + return NullableProperty.parse_obj(obj) + + _obj = NullableProperty.parse_obj({ + "id": obj.get("id"), + "name": obj.get("name") + }) + # 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 + diff --git a/samples/openapi3/client/petstore/python/test/test_nullable_property.py b/samples/openapi3/client/petstore/python/test/test_nullable_property.py new file mode 100644 index 000000000000..34129457b1d7 --- /dev/null +++ b/samples/openapi3/client/petstore/python/test/test_nullable_property.py @@ -0,0 +1,57 @@ +# 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.nullable_property import NullableProperty # noqa: E501 +from petstore_api.rest import ApiException + +class TestNullableProperty(unittest.TestCase): + """NullableProperty unit test stubs""" + + def setUp(self): + pass + + def tearDown(self): + pass + + def make_instance(self, include_optional): + """Test NullableProperty + 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 `NullableProperty` + """ + model = petstore_api.models.nullable_property.NullableProperty() # noqa: E501 + if include_optional : + return NullableProperty( + id = 56, + name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>' + ) + else : + return NullableProperty( + id = 56, + name = 'AUR,rZ#UM/?R,Fp^l6$ARjbhJk C>', + ) + """ + + def testNullableProperty(self): + """Test NullableProperty""" + # inst_req_only = self.make_instance(include_optional=False) + # inst_req_and_optional = self.make_instance(include_optional=True) + +if __name__ == '__main__': + unittest.main() diff --git a/samples/openapi3/client/petstore/python/tests/test_model.py b/samples/openapi3/client/petstore/python/tests/test_model.py index 3934cd2b41c8..e582bdafecac 100644 --- a/samples/openapi3/client/petstore/python/tests/test_model.py +++ b/samples/openapi3/client/petstore/python/tests/test_model.py @@ -468,6 +468,11 @@ class ModelTests(unittest.TestCase): self.assertEqual(a.name, "MINUS_EFG") self.assertEqual(a, "-efg") + def test_nullable_property_pattern(self): + a = petstore_api.NullableProperty(id=12, name=None) + self.assertEqual(a.id, 12) + self.assertEqual(a.name, None) + def test_int_or_string_oneof(self): a = petstore_api.IntOrString("-efg") self.assertEqual(a.actual_instance, "-efg")