sirVir opened a new issue, #34394: URL: https://github.com/apache/airflow/issues/34394
### Apache Airflow version 2.7.1 ### What happened According to [Airflow Documentation on Airflow Variables in Templates](https://airflow.apache.org/docs/apache-airflow/stable/templates-ref.html#airflow-variables-in-templates) there are two ways of accessing the JSON variables in templates: - using the direct `{{ var.json.my_dict_var.key1 }}` - using a getter with a default fallback value `{{{{ var.json.get('my.dict.var', {'key1': 'val1'}) }}` However, only the first approach works for nested variables, as demonstrated in the example. Or is it me not understanding this documentation? Alternatively it could get updated to make it clearer. ### What you think should happen instead _No response_ ### How to reproduce The following test demonstrates the issue: ``` from datetime import datetime, timezone import pytest from airflow import DAG from airflow.models.baseoperator import BaseOperator from airflow.models.dag import DAG from airflow.utils import db from airflow.utils.state import DagRunState from airflow.utils.types import DagRunType from pytest import MonkeyPatch from airflow.models import Variable class TemplatedBaseOperator(BaseOperator): template_fields = BaseOperator.template_fields + ( "templated_direct", "templated_getter") def __init__( self, *args, **kwargs, ): self.templated_direct = "{{ var.json.somekey.somecontent }}" self.templated_getter = "{{ var.json.get('somekey.somecontent', false) }}" super().__init__( *args, **kwargs, ) @pytest.fixture() def reset_db(): db.resetdb() yield @pytest.fixture def dag() -> DAG: with DAG( dag_id="templating_dag", schedule="@daily", start_date=datetime(2023, 1, 1, tzinfo=timezone.utc), render_template_as_native_obj=True, ) as dag: TemplatedBaseOperator( task_id="templating_task" ) return dag def test_templating(dag: DAG, reset_db: None, monkeypatch: MonkeyPatch): """Test if the templated values get intialized from environment variables when rendered""" # setting env variables monkeypatch.setenv( "AIRFLOW_VAR_SOMEKEY", '{"somecontent": true}', ) dagrun = dag.create_dagrun( state=DagRunState.RUNNING, execution_date=datetime(2023, 1, 1, tzinfo=timezone.utc), data_interval=(datetime(2023, 1, 1, tzinfo=timezone.utc), datetime(2023, 1, 7, tzinfo=timezone.utc)), start_date=datetime(2023, 1, 7, tzinfo=timezone.utc), run_type=DagRunType.MANUAL, ) ti = dagrun.get_task_instance(task_id="templating_task") ti.task = dag.get_task(task_id="templating_task") rendered_template = ti.render_templates() assert {'somecontent': True} == Variable.get("somekey", deserialize_json=True) assert getattr(rendered_template, "templated_direct") == True # the following test is failing, getting default "False" instead of Variable 'True' assert getattr(rendered_template, "templated_getter") == True ``` ### Operating System Ubuntu 22.04.3 LTS ### Versions of Apache Airflow Providers _No response_ ### Deployment Virtualenv installation ### Deployment details _No response_ ### Anything else _No response_ ### Are you willing to submit PR? - [X] Yes I am willing to submit a PR! ### Code of Conduct - [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
