Skip to content

Commit eece180

Browse files
committed
fix: Handle Unset Enums when deserializing (openapi-generators#306)
1 parent 64c31ce commit eece180

File tree

4 files changed

+40
-16
lines changed

4 files changed

+40
-16
lines changed

CHANGELOG.md

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,64 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.8.0 - Unreleased
9+
10+
### Additions
11+
12+
- New `--meta` command line option for specifying what type of metadata should be generated:
13+
- `poetry` is the default value, same behavior you're used to in previous versions
14+
- `setup` will generate a pyproject.toml with no Poetry information, and instead create a `setup.py` with the
15+
project info.
16+
- `none` will not create a project folder at all, only the inner package folder (which won't be inner anymore)
17+
- Attempt to detect and alert users if they are using an unsupported version of OpenAPI (#281).
18+
- Fixes `Enum` deserialization when the value is `UNSET`.
19+
20+
### Changes
21+
22+
- Lowered the minimum version of `python-dateutil` to 2.8.0 for improved compatibility (#298 & #299). Thanks @bowenwr!
23+
- The `from_dict` method on generated models is now a `@classmethod` instead of `@staticmethod` (#215 & #292). Thanks @forest-benchling!
24+
825
## 0.7.3 - 2020-12-21
26+
927
### Fixes
28+
1029
- Spacing and extra returns for Union types of `additionalProperties` (#266 & #268). Thanks @joshzana & @packyg!
1130
- Title of inline schemas will no longer be missing characters (#271 & #274). Thanks @kalzoo!
12-
- Handling of nulls (Nones) when parsing or constructing dates (#267). Thanks @fyhertz!
31+
- Handling of nulls (Nones) when parsing or constructing dates (#267). Thanks @fyhertz!
1332

1433
## 0.7.2 - 2020-12-08
34+
1535
### Fixes
36+
1637
- A bug in handling optional properties that are themselves models (introduced in 0.7.1) (#262). Thanks @packyg!
1738

1839
## 0.7.1 - 2020-12-08
40+
1941
### Additions
42+
2043
- Support for additionalProperties attribute in OpenAPI schemas and "free-form" objects by adding an `additional_properties` attribute to generated models. **COMPATIBILITY NOTE**: this will prevent any model property with a name that would be coerced to "additional_properties" in the generated client from generating properly (#218 & #252). Thanks @packyg!
2144

2245
### Fixes
46+
2347
- Enums will once again work with query parameters (#259). Thanks @packyg!
2448
- Generated Poetry metadata in pyproject.toml will properly indicate Python 3.6 compatibility (#258). Thanks @bowenwr!
2549

2650
## 0.7.0 - 2020-11-25
2751

2852
### Breaking Changes
2953

30-
- Any request/response field that is not `required` and wasn't specified is now set to `UNSET` instead of `None`.
54+
- Any request/response field that is not `required` and wasn't specified is now set to `UNSET` instead of `None`.
3155
- Values that are `UNSET` will not be sent along in API calls
32-
- Schemas defined with `type=object` will now be converted into classes, just like if they were created as ref components.
33-
The previous behavior was a combination of skipping and using generic Dicts for these schemas.
34-
- Response schema handling was unified with input schema handling, meaning that responses will behave differently than before.
35-
Specifically, instead of the content-type deciding what the generated Python type is, the schema itself will.
36-
- As a result of this, endpoints that used to return `bytes` when content-type was application/octet-stream will now return a `File` object if the type of the data is "binary", just like if you were submitting that type instead of receiving it.
56+
- Schemas defined with `type=object` will now be converted into classes, just like if they were created as ref components.
57+
The previous behavior was a combination of skipping and using generic Dicts for these schemas.
58+
- Response schema handling was unified with input schema handling, meaning that responses will behave differently than before.
59+
Specifically, instead of the content-type deciding what the generated Python type is, the schema itself will.
60+
- As a result of this, endpoints that used to return `bytes` when content-type was application/octet-stream will now return a `File` object if the type of the data is "binary", just like if you were submitting that type instead of receiving it.
3761
- Instead of skipping input properties with no type, enum, anyOf, or oneOf declared, the property will be declared as `None`.
38-
- Class (models and Enums) names will now contain the name of their parent element (if any). For example, a property
39-
declared in an endpoint will be named like {endpoint_name}_{previous_class_name}. Classes will no longer be
40-
deduplicated by appending a number to the end of the generated name, so if two names conflict with this new naming
41-
scheme, there will be an error instead.
62+
- Class (models and Enums) names will now contain the name of their parent element (if any). For example, a property
63+
declared in an endpoint will be named like {endpoint*name}*{previous_class_name}. Classes will no longer be
64+
deduplicated by appending a number to the end of the generated name, so if two names conflict with this new naming
65+
scheme, there will be an error instead.
4266

4367
### Additions
4468

end_to_end_tests/golden-record-custom/custom_e2e/models/model_with_union_property.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum
4747
raise TypeError()
4848
a_property = UNSET
4949
_a_property = data
50-
if _a_property is not None:
50+
if _a_property is not None and _a_property is not UNSET:
5151
a_property = AnEnum(_a_property)
5252

5353
return a_property
@@ -57,7 +57,7 @@ def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum
5757
raise TypeError()
5858
a_property = UNSET
5959
_a_property = data
60-
if _a_property is not None:
60+
if _a_property is not None and _a_property is not UNSET:
6161
a_property = AnIntEnum(_a_property)
6262

6363
return a_property

end_to_end_tests/golden-record/my_test_api_client/models/model_with_union_property.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum
4747
raise TypeError()
4848
a_property = UNSET
4949
_a_property = data
50-
if _a_property is not None:
50+
if _a_property is not None and _a_property is not UNSET:
5151
a_property = AnEnum(_a_property)
5252

5353
return a_property
@@ -57,7 +57,7 @@ def _parse_a_property(data: Union[Unset, int]) -> Union[Unset, AnEnum, AnIntEnum
5757
raise TypeError()
5858
a_property = UNSET
5959
_a_property = data
60-
if _a_property is not None:
60+
if _a_property is not None and _a_property is not UNSET:
6161
a_property = AnIntEnum(_a_property)
6262

6363
return a_property

openapi_python_client/templates/property_templates/enum_property.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{% else %}
55
{{ property.python_name }} = {{ initial_value }}
66
_{{ property.python_name }} = {{ source }}
7-
if _{{ property.python_name }} is not None:
7+
if _{{ property.python_name }} is not None and _{{ property.python_name }} is not UNSET:
88
{{ property.python_name }} = {{ property.reference.class_name }}(_{{ property.python_name }})
99
{% endif %}
1010
{% endmacro %}

0 commit comments

Comments
 (0)