Skip to content

fix: Treat None as UNSET in query params for model properties, etc. #421

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
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 @@ -38,11 +38,14 @@ def _get_kwargs(
cookies: Dict[str, Any] = client.get_cookies()

json_not_required_not_nullable_datetime_prop: Union[Unset, str] = UNSET
if not isinstance(not_required_not_nullable_datetime_prop, Unset):
if (
not isinstance(not_required_not_nullable_datetime_prop, Unset)
and not_required_not_nullable_datetime_prop is not None
):
json_not_required_not_nullable_datetime_prop = not_required_not_nullable_datetime_prop.isoformat()

json_not_required_nullable_datetime_prop: Union[Unset, None, str] = UNSET
if not isinstance(not_required_nullable_datetime_prop, Unset):
if not isinstance(not_required_nullable_datetime_prop, Unset) and not_required_nullable_datetime_prop is not None:
json_not_required_nullable_datetime_prop = (
not_required_nullable_datetime_prop.isoformat() if not_required_nullable_datetime_prop else None
)
Expand All @@ -54,25 +57,25 @@ def _get_kwargs(
)

json_date_prop: Union[Unset, str] = UNSET
if not isinstance(date_prop, Unset):
if not isinstance(date_prop, Unset) and date_prop is not None:
json_date_prop = date_prop.isoformat()

json_list_prop: Union[Unset, List[str]] = UNSET
if not isinstance(list_prop, Unset):
if not isinstance(list_prop, Unset) and list_prop is not None:
json_list_prop = []
for list_prop_item_data in list_prop:
list_prop_item = list_prop_item_data.value

json_list_prop.append(list_prop_item)

json_union_prop: Union[Unset, float, str]
if isinstance(union_prop, Unset):
if isinstance(union_prop, Unset) or union_prop is None:
json_union_prop = UNSET
else:
json_union_prop = union_prop

json_union_prop_with_ref: Union[Unset, float, str]
if isinstance(union_prop_with_ref, Unset):
if isinstance(union_prop_with_ref, Unset) or union_prop_with_ref is None:
json_union_prop_with_ref = UNSET
elif isinstance(union_prop_with_ref, AnEnum):
json_union_prop_with_ref = UNSET
Expand All @@ -83,17 +86,17 @@ def _get_kwargs(
json_union_prop_with_ref = union_prop_with_ref

json_enum_prop: Union[Unset, str] = UNSET
if not isinstance(enum_prop, Unset):
if not isinstance(enum_prop, Unset) and enum_prop is not None:
json_enum_prop = enum_prop.value

json_model_prop: Union[Unset, Dict[str, Any]] = UNSET
if not isinstance(model_prop, Unset):
if not isinstance(model_prop, Unset) and model_prop is not None:
json_model_prop = model_prop.to_dict()

json_required_model_prop = required_model_prop.to_dict()

json_nullable_model_prop: Union[Unset, None, Dict[str, Any]] = UNSET
if not isinstance(nullable_model_prop, Unset):
if not isinstance(nullable_model_prop, Unset) and nullable_model_prop is not None:
json_nullable_model_prop = nullable_model_prop.to_dict() if nullable_model_prop else None

json_nullable_required_model_prop = nullable_required_model_prop.to_dict() if nullable_required_model_prop else None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _get_kwargs(
cookies: Dict[str, Any] = client.get_cookies()

json_query_param: Union[Unset, List[str]] = UNSET
if not isinstance(query_param, Unset):
if not isinstance(query_param, Unset) and query_param is not None:
json_query_param = query_param

params: Dict[str, Any] = {
Expand Down
2 changes: 1 addition & 1 deletion openapi_python_client/templates/endpoint_macros.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if {{ parameter.python_name }} is not UNSET:
{% set destination = "json_" + property.python_name %}
{% if property.template %}
{% from "property_templates/" + property.template import transform %}
{{ transform(property, property.python_name, destination) }}
{{ transform(property, property.python_name, destination, query_param=True) }}
{% endif %}
{% endfor %}
params: Dict[str, Any] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ isoparse({{ source }}).date()

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{% if property.required %}
{{ destination }} = {{ source }}.isoformat() {% if property.nullable %}if {{ source }} else None {%endif%}
{% else %}
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
if not isinstance({{ source }}, Unset){% if query_param %} and {{ source }} is not None{% endif %}:
{% if property.nullable %}
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ isoparse({{ source }})

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, str){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
Expand All @@ -19,7 +19,7 @@ isoparse({{ source }})
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
if not isinstance({{ source }}, Unset){% if query_param %} and {{ source }} is not None{% endif %}:
{% if property.nullable %}
{{ destination }} = {{ source }}.isoformat() if {{ source }} else None
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, {{ property.value_type.__name__ }}){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{% set transformed = source + ".value" %}
{% set type_string = property.get_type_string(json=True) %}
{% if stringify %}
Expand All @@ -25,7 +25,7 @@
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
if not isinstance({{ source }}, Unset){% if query_param %} and {{ source }} is not None{% endif %}:
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ File(

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, bytes){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{% if property.required %}
{% if property.nullable %}
{{ destination }} = {{ source }}.to_tuple() if {{ source }} else None
Expand All @@ -21,7 +21,7 @@ File(
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
if not isinstance({{ source }}, Unset){% if query_param %} and {{ source }} is not None{% endif %}:
{% if property.nullable %}
{{ destination }} = {{ source }}.to_tuple() if {{ source }} else None
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ for {{ inner_source }} in {{ source }}:

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{% set inner_property = property.inner_property %}
{% if stringify %}
{% set type_string = "Union[Unset, Tuple[None, str, str]]" %}
Expand All @@ -58,7 +58,7 @@ else:
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
if not isinstance({{ source }}, Unset){% if query_param %} and {{ source }} is not None{% endif %}:
{% if property.nullable %}
if {{ source }} is None:
{{ destination }} = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, dict){% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False, transform_method="to_dict") %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, transform_method="to_dict", query_param=False) %}
{% set transformed = source + "." + transform_method + "()" %}
{% if stringify %}
{% set transformed = "(None, json.dumps(" + transformed + "), 'application/json')" %}
Expand All @@ -26,7 +26,7 @@
{% endif %}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
if not isinstance({{ source }}, Unset){% if query_param %} and {{ source }} is not None{% endif %}:
{% if property.nullable %}
{{ destination }} = {{ transformed }} if {{ source }} else None
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

{% macro check_type_for_construct(property, source) %}{{ source }} is None{% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{{ destination }} = None
{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def _parse_{{ property.python_name }}(data: object) -> {{ property.get_type_stri
{# For now we assume there will be no unions of unions #}
{% macro check_type_for_construct(property, source) %}True{% endmacro %}

{% macro transform(property, source, destination, declare_type=True, stringify=False) %}
{% macro transform(property, source, destination, declare_type=True, stringify=False, query_param=False) %}
{% if not property.required or property.nullable %}
{{ destination }}{% if declare_type %}: {{ property.get_type_string(json=True) }}{% endif %}

{% if not property.required %}
if isinstance({{ source }}, Unset):
if isinstance({{ source }}, Unset){% if query_param %} or {{ source }} is None{% endif %}:
{{ destination }} = UNSET
{% endif %}
{% endif %}
Expand Down