From c6e38008362893eaddc4c0358cdaca01f84cfa01 Mon Sep 17 00:00:00 2001 From: William Bowen Date: Tue, 19 Jan 2021 12:15:34 -0700 Subject: [PATCH 1/2] Handle Unset Enums when deserializing --- .../custom_e2e/models/model_with_union_property.py | 4 ++-- .../my_test_api_client/models/model_with_union_property.py | 4 ++-- .../templates/property_templates/enum_property.pyi | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py index 960360cdb..ed8deec19 100644 --- a/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py @@ -46,7 +46,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: try: a_property = UNSET _a_property = data - if _a_property is not None: + if _a_property is not None and _a_property is not UNSET: a_property = AnEnum(_a_property) return a_property @@ -54,7 +54,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: pass a_property = UNSET _a_property = data - if _a_property is not None: + if _a_property is not None and _a_property is not UNSET: a_property = AnIntEnum(_a_property) return a_property diff --git a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py index 960360cdb..ed8deec19 100644 --- a/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py +++ b/end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py @@ -46,7 +46,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: try: a_property = UNSET _a_property = data - if _a_property is not None: + if _a_property is not None and _a_property is not UNSET: a_property = AnEnum(_a_property) return a_property @@ -54,7 +54,7 @@ def _parse_a_property(data: Any) -> Union[Unset, AnEnum, AnIntEnum]: pass a_property = UNSET _a_property = data - if _a_property is not None: + if _a_property is not None and _a_property is not UNSET: a_property = AnIntEnum(_a_property) return a_property diff --git a/openapi_python_client/templates/property_templates/enum_property.pyi b/openapi_python_client/templates/property_templates/enum_property.pyi index 4765a6fd5..1066fce10 100644 --- a/openapi_python_client/templates/property_templates/enum_property.pyi +++ b/openapi_python_client/templates/property_templates/enum_property.pyi @@ -4,7 +4,7 @@ {% else %} {{ property.python_name }} = {{ initial_value }} _{{ property.python_name }} = {{ source }} -if _{{ property.python_name }} is not None: +if _{{ property.python_name }} is not None and _{{ property.python_name }} is not UNSET: {{ property.python_name }} = {{ property.reference.class_name }}(_{{ property.python_name }}) {% endif %} {% endmacro %} From f1b3e4ec947456fa6e95150c7a00199abe328fdb Mon Sep 17 00:00:00 2001 From: William Bowen Date: Fri, 22 Jan 2021 09:04:59 -0700 Subject: [PATCH 2/2] Add update to CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a64107a..7b2372aca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 project info. - `none` will not create a project folder at all, only the inner package folder (which won't be inner anymore) - Attempt to detect and alert users if they are using an unsupported version of OpenAPI (#281). +- Fixes `Enum` deserialization when the value is `UNSET`. ### Changes