Add support setting Null for nullable optional args

For primitive types (not arrays and not dicts or similar) when resource
attribute can be explicitly nullable (while also being optional) it is
necessary to allow CLI user to set it to null. The only practical way
found till now is to add additional argument: `device_id / no_device_id`
which can be used to set explicit null. Neither relying on specific
value ("", "null") nor using default value when no value is sustainable
looking at the variations of APIs.

Change-Id: I6d549ffcc5c51a9c3ed849c590777b5ba4f6d721
This commit is contained in:
Artem Goncharov 2025-05-13 13:59:45 +02:00
parent 7811367bea
commit 85c09cd60e
2 changed files with 11 additions and 0 deletions
codegenerator/templates/rust_cli

@ -98,6 +98,12 @@ struct {{ type.name }} {
{{ macros.docstring(field.description, indent=4) }}
{{ field.clap_macros_ext(is_group=type.is_group) }}
{{ field.local_name }}: {{ field.type_hint }},
{%- if field.is_optional and field.is_nullable and field.data_type.__class__.__name__ in ["String", "Boolean", "Number", "Integer"] %}
/// Set explicit NULL for the {{ field.local_name }}
#[arg(help_heading = "Body parameters", long, action = clap::ArgAction::SetTrue, conflicts_with = "{{ field.local_name }}")]
no_{{ field.local_name }}: bool,
{%- endif %}
{%- endfor %}
{%- endif %}
}

@ -21,6 +21,11 @@
if let Some(val) = &args.{{ v.local_name }} {
{{ macros.set_request_data_from_input(type_manager, builder_name, v, "val") }}
}
{%- if v.is_nullable and v.data_type.__class__.__name__ in ["String", "Boolean", "Number", "Integer"] %}
else if args.no_{{ v.local_name }} {
{{ builder_name }}.{{ v.local_name }}(None);
}
{%- endif %}
{%- elif v.data_type.format is defined and v.data_type.format == "password" %}
if let Some(val) = &args.{{ v.local_name }} {
{# val.clone() is necessary due to SecretBox not implementing From<&String> #}