Skip to content

Commit

Permalink
Fix template error when iterating None value and fix params documenta…
Browse files Browse the repository at this point in the history
…tion (#31078)

* Check if form value is iterable

* Update doc params list to array

* Add test render, prevent value mutaion in js
  • Loading branch information
herlambang authored May 6, 2023
1 parent 49cc213 commit b8b18bd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion airflow/www/static/js/trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function updateJSONconf() {
values[values.length] = lines[j].trim();
}
}
params[keyName] = values;
params[keyName] = values.length === 0 ? params[keyName] : values;
} else if (elements[i].value.length === 0) {
params[keyName] = null;
} else if (
Expand Down
8 changes: 5 additions & 3 deletions airflow/www/templates/airflow/trigger.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@
{% elif form_details.schema and "array" in form_details.schema.type %}
<textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="array" rows="6"
{%- if not "null" in form_details.schema.type %} required=""{% endif -%}>
{%- for txt in form_details.value -%}
{{ txt }}{{ "\n" }}
{%- endfor -%}
{%- if form_details.value is sequence %}
{%- for txt in form_details.value -%}
{{ txt }}{{ "\n" }}
{%- endfor -%}
{% endif -%}
</textarea>
{% elif form_details.schema and "object" in form_details.schema.type %}
<textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="object" rows="6"
Expand Down
2 changes: 1 addition & 1 deletion docs/apache-airflow/core-concepts/params.rst
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ The following features are supported in the Trigger UI Form:
You can add the parameters ``minimum`` and ``maximum`` to restrict number range accepted.
- ``boolean``: Generates a toggle button to be used as ``True`` or ``False``.
- ``date``, ``datetime`` and ``time``: Generate date and/or time picker
- ``list``: Generates a HTML multi line text field, every line edited will be made into a string array as value
- ``array``: Generates a HTML multi line text field, every line edited will be made into a string array as value
- ``object``: Generates a JSON entry field
- Note: Per default if you specify a type, a field will be made required with input - because of JSON validation.
If you want to have a field value being added optional only, you must allow JSON schema validation allowing null values via:
Expand Down
26 changes: 26 additions & 0 deletions tests/www/views/test_views_trigger_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,29 @@ def test_viewer_cant_trigger_dag(app):
resp = client.get(url, follow_redirects=True)
response_data = resp.data.decode()
assert "Access is Denied" in response_data


def test_trigger_dag_params_array_value_none_render(admin_client, dag_maker, session, app, monkeypatch):
"""
Test that textarea in Trigger DAG UI is pre-populated
with param value None and type ["null", "array"] set in DAG.
"""
expected_conf = {"dag_param": None}
expected_dag_conf = json.dumps(expected_conf, indent=4).replace('"', "&#34;")
DAG_ID = "params_dag"
param = Param(
None,
type=["null", "array"],
minItems=0,
)
with monkeypatch.context() as m:
with dag_maker(dag_id=DAG_ID, serialized=True, session=session, params={"dag_param": param}):
EmptyOperator(task_id="task1")

m.setattr(app, "dag_bag", dag_maker.dagbag)
resp = admin_client.get(f"trigger?dag_id={DAG_ID}")

check_content_in_response(
f'<textarea style="display: none;" id="json_start" name="json_start">{expected_dag_conf}</textarea>',
resp,
)

0 comments on commit b8b18bd

Please sign in to comment.