Skip to content

Fix nullable date/datetime properties #267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AModel:
a_camel_date_time: Union[datetime.datetime, datetime.date]
a_date: datetime.date
required_not_nullable: str
a_nullable_date: Optional[datetime.date]
required_nullable: Optional[str]
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
attr_1_leading_digit: Union[Unset, str] = UNSET
Expand Down Expand Up @@ -47,6 +48,8 @@ def to_dict(self) -> Dict[str, Any]:

nested_list_of_enums.append(nested_list_of_enums_item)

a_nullable_date = self.a_nullable_date.isoformat() if self.a_nullable_date else None

attr_1_leading_digit = self.attr_1_leading_digit
required_nullable = self.required_nullable
not_required_nullable = self.not_required_nullable
Expand All @@ -59,6 +62,7 @@ def to_dict(self) -> Dict[str, Any]:
"aCamelDateTime": a_camel_date_time,
"a_date": a_date,
"required_not_nullable": required_not_nullable,
"a_nullable_date": a_nullable_date,
"required_nullable": required_nullable,
}
)
Expand Down Expand Up @@ -109,6 +113,9 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat

nested_list_of_enums.append(nested_list_of_enums_item)

a_nullable_date = d.pop("a_nullable_date")
a_nullable_date = isoparse(a_nullable_date).date() if a_nullable_date else None

attr_1_leading_digit = d.pop("1_leading_digit", UNSET)

required_nullable = d.pop("required_nullable")
Expand All @@ -123,6 +130,7 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
a_date=a_date,
required_not_nullable=required_not_nullable,
nested_list_of_enums=nested_list_of_enums,
a_nullable_date=a_nullable_date,
attr_1_leading_digit=attr_1_leading_digit,
required_nullable=required_nullable,
not_required_nullable=not_required_nullable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class AModel:
a_camel_date_time: Union[datetime.datetime, datetime.date]
a_date: datetime.date
required_not_nullable: str
a_nullable_date: Optional[datetime.date]
required_nullable: Optional[str]
nested_list_of_enums: Union[Unset, List[List[DifferentEnum]]] = UNSET
attr_1_leading_digit: Union[Unset, str] = UNSET
Expand Down Expand Up @@ -47,6 +48,8 @@ def to_dict(self) -> Dict[str, Any]:

nested_list_of_enums.append(nested_list_of_enums_item)

a_nullable_date = self.a_nullable_date.isoformat() if self.a_nullable_date else None

attr_1_leading_digit = self.attr_1_leading_digit
required_nullable = self.required_nullable
not_required_nullable = self.not_required_nullable
Expand All @@ -59,6 +62,7 @@ def to_dict(self) -> Dict[str, Any]:
"aCamelDateTime": a_camel_date_time,
"a_date": a_date,
"required_not_nullable": required_not_nullable,
"a_nullable_date": a_nullable_date,
"required_nullable": required_nullable,
}
)
Expand Down Expand Up @@ -109,6 +113,9 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat

nested_list_of_enums.append(nested_list_of_enums_item)

a_nullable_date = d.pop("a_nullable_date")
a_nullable_date = isoparse(a_nullable_date).date() if a_nullable_date else None

attr_1_leading_digit = d.pop("1_leading_digit", UNSET)

required_nullable = d.pop("required_nullable")
Expand All @@ -123,6 +130,7 @@ def _parse_a_camel_date_time(data: Any) -> Union[datetime.datetime, datetime.dat
a_date=a_date,
required_not_nullable=required_not_nullable,
nested_list_of_enums=nested_list_of_enums,
a_nullable_date=a_nullable_date,
attr_1_leading_digit=attr_1_leading_digit,
required_nullable=required_nullable,
not_required_nullable=not_required_nullable,
Expand Down
8 changes: 7 additions & 1 deletion end_to_end_tests/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@
"schemas": {
"AModel": {
"title": "AModel",
"required": ["an_enum_value", "aCamelDateTime", "a_date", "required_nullable", "required_not_nullable"],
"required": ["an_enum_value", "aCamelDateTime", "a_date", "a_nullable_date", "required_nullable", "required_not_nullable"],
"type": "object",
"properties": {
"an_enum_value": {
Expand Down Expand Up @@ -657,6 +657,12 @@
"type": "string",
"format": "date"
},
"a_nullable_date": {
"title": "A Nullable Date",
"type": "string",
"format": "date",
"nullable": true
},
"1_leading_digit": {
"title": "Leading Digit",
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{% macro construct(property, source, initial_value="None") %}
{% if property.required %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, couldn't we do this:

Suggested change
{% if property.required %}
{% if property.required and not property.nullable %}

and let the below else handle None?

{% if property.nullable %}
{{ property.python_name }} = {{ source }}
{{ property.python_name }} = isoparse({{ property.python_name }}).date() if {{ property.python_name }} else None
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes as they are here would cause type issues, I think, because the type of {{property.python_name}} changes from that of {{source}} (Optional[str]) to Optional[date]

You would need to put the {{source}} in a temp variable for this implementation:

Suggested change
{{ property.python_name }} = {{ source }}
{{ property.python_name }} = isoparse({{ property.python_name }}).date() if {{ property.python_name }} else None
_{{ property.python_name }} = {{ source }}
{{ property.python_name }} = isoparse(_{{ property.python_name }}).date() if {{ property.python_name }} else None

But I think @dbanty's suggestion would work and would be a bit simpler.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I will try to fix that today!

{% else %}
{{ property.python_name }} = isoparse({{ source }}).date()
{% endif %}
{% else %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{% macro construct(property, source, initial_value="None") %}
{% if property.required %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here I think

{% if property.nullable %}
{{ property.python_name }} = {{ source }}
{{ property.python_name }} = isoparse({{ property.python_name }}) if {{ property.python_name }} else None
{% else %}
{{ property.python_name }} = isoparse({{ source }})
{% endif %}
{% else %}
{{ property.python_name }} = {{ initial_value }}
_{{ property.python_name }} = {{ source }}
Expand Down