Skip to content

Commit abf5a0c

Browse files
authored
fix(native): Jinja - enable autoescape for .jinja (old naming) files (#7243)
1 parent 2674614 commit abf5a0c

File tree

8 files changed

+75
-68
lines changed

8 files changed

+75
-68
lines changed

packages/cubejs-backend-native/src/template/entry.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ impl JinjaEngine {
9898
Err(err)
9999
},
100100
);
101+
engine.set_auto_escape_callback(|_name: &str| mj::AutoEscape::Json);
101102

102103
Ok(Self { inner: engine })
103104
}

packages/cubejs-backend-native/test/__snapshots__/jinja.test.ts.snap

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ exports[`Jinja (new api) render 01.yml.jinja: 01.yml.jinja 1`] = `
66
sql: >
77
SELECT
88
order_id,
9-
SUM(CASE WHEN payment_method = 'bank_transfer' THEN amount END) AS bank_transfer_amount,
10-
SUM(CASE WHEN payment_method = 'credit_card' THEN amount END) AS credit_card_amount,
11-
SUM(CASE WHEN payment_method = 'gift_card' THEN amount END) AS gift_card_amount,
9+
SUM(CASE WHEN payment_method = 'TRANSFER' THEN amount END) AS bank_transfer_amount,
10+
SUM(CASE WHEN payment_method = 'CREDIT' THEN amount END) AS credit_card_amount,
11+
SUM(CASE WHEN payment_method = 'GIFT' THEN amount END) AS gift_card_amount,
1212
SUM(amount) AS total_amount
1313
FROM app_data.payments
1414
GROUP BY 1
@@ -17,9 +17,9 @@ exports[`Jinja (new api) render 01.yml.jinja: 01.yml.jinja 1`] = `
1717
sql: >
1818
SELECT
1919
order_id,
20-
SUM(CASE WHEN payment_method = 'bank_transfer' THEN amount END) AS bank_transfer_amount,
21-
SUM(CASE WHEN payment_method = 'credit_card' THEN amount END) AS credit_card_amount,
22-
SUM(CASE WHEN payment_method = 'gift_card' THEN amount END) AS gift_card_amount
20+
SUM(CASE WHEN payment_method = 'TRANSFER' THEN amount END) AS bank_transfer_amount,
21+
SUM(CASE WHEN payment_method = 'CREDIT' THEN amount END) AS credit_card_amount,
22+
SUM(CASE WHEN payment_method = 'GIFT' THEN amount END) AS gift_card_amount
2323
FROM app_data.payments
2424
GROUP BY 1"
2525
`;
@@ -66,7 +66,7 @@ exports[`Jinja (new api) render 04.yml.jinja: 04.yml.jinja 1`] = `
6666
"
6767
6868
cubes:
69-
- name: cube_04_base_events
69+
- name: cube_04_\\"base_events\\"
7070
sql: >
7171
SELECT *
7272
FROM public.events
@@ -81,7 +81,7 @@ cubes:
8181
type: time
8282
8383
- name: cube_04_product_purchases
84-
extends: base_events
84+
extends: \\"base_events\\"
8585
sql_table: public.events
8686
8787
dimensions:
@@ -90,7 +90,7 @@ cubes:
9090
type: time
9191
9292
- name: cube_04_page_views
93-
extends: base_events
93+
extends: \\"base_events\\"
9494
sql_table: public.events
9595
9696
dimensions:
@@ -108,21 +108,21 @@ cubes:
108108
sql_table: public.orders
109109
110110
measures:
111-
- name: day
111+
- name: \\"day\\"
112112
type: count_distinct
113113
sql: user_id
114114
rolling_window:
115115
trailing: 1 day
116116
offset: start
117117
118-
- name: mau
118+
- name: \\"mau\\"
119119
type: count_distinct
120120
sql: user_id
121121
rolling_window:
122122
trailing: 30 day
123123
offset: start
124124
125-
- name: wau
125+
- name: \\"wau\\"
126126
type: count_distinct
127127
sql: user_id
128128
rolling_window:
@@ -137,22 +137,22 @@ exports[`Jinja (new api) render 06.yml.jinja: 06.yml.jinja 1`] = `
137137
sql_table: public.orders
138138
139139
dimensions:
140-
- name: id
141-
sql: id
142-
type: number
140+
- name: \\"id\\"
141+
sql: \\"id\\"
142+
type: \\"number\\"
143143
primary_key: true
144144
145-
- name: status
146-
sql: status
147-
type: string
145+
- name: \\"status\\"
146+
sql: \\"status\\"
147+
type: \\"string\\"
148148
149-
- name: created_at
150-
sql: created_at
151-
type: time
149+
- name: \\"created_at\\"
150+
sql: \\"created_at\\"
151+
type: \\"time\\"
152152
153-
- name: completed_at
154-
sql: completed_at
155-
type: time
153+
- name: \\"completed_at\\"
154+
sql: \\"completed_at\\"
155+
type: \\"time\\"
156156
"
157157
`;
158158

@@ -162,16 +162,16 @@ exports[`Jinja (new api) render 07.yml.jinja: 07.yml.jinja 1`] = `
162162
sql: >
163163
SELECT
164164
id AS payment_id,
165-
(amount / 100)::NUMERIC(16, 2) AS amount_usd,
166-
((order_selling_price - order_cost_price) / order_cost_price) AS markup
165+
(\\"amount\\" / 100)::NUMERIC(16, 2) AS amount_usd,
166+
((\\"order_selling_price\\" - \\"order_cost_price\\") / \\"order_cost_price\\") AS markup
167167
FROM app_data.payments"
168168
`;
169169

170170
exports[`Jinja (new api) render 08.yml.jinja: 08.yml.jinja 1`] = `
171171
"{ cubes:
172172
- name: cube_08
173173
sql_table: public.orders
174-
data_source: postgres }"
174+
data_source: \\"postgres\\" }"
175175
`;
176176

177177
exports[`Jinja (new api) render arguments-test.yml.jinja: arguments-test.yml.jinja 1`] = `
@@ -180,34 +180,34 @@ exports[`Jinja (new api) render arguments-test.yml.jinja: arguments-test.yml.jin
180180
arg_sum_integers_int_float: 4.140000000000001
181181
arg_bool_true: 1
182182
arg_bool_false: 0
183-
arg_str: hello world
184-
arg_null: none
185-
arg_seq_1: [1, 2, 3, 4, 5]
186-
arg_seq_2: [5, 4, 3, 2, 1]
183+
arg_str: \\"hello world\\"
184+
arg_null: null
185+
arg_seq_1: [1,2,3,4,5]
186+
arg_seq_2: [5,4,3,2,1]
187187
arg_sum_tuple: 3
188188
arg_sum_map: 20"
189189
`;
190190

191191
exports[`Jinja (new api) render data-model.yml.jinja: data-model.yml.jinja 1`] = `
192192
"cubes:
193193
194-
- name: cube_from_api
194+
- name: \\"cube_from_api\\"
195195
measures:
196-
- name: count
197-
type: count
198-
- name: total
199-
type: sum
200-
sql: amount
196+
- name: \\"count\\"
197+
type: \\"count\\"
198+
- name: \\"total\\"
199+
type: \\"sum\\"
200+
sql: \\"amount\\"
201201
202-
- name: cube_from_api_with_dimensions
202+
- name: \\"cube_from_api_with_dimensions\\"
203203
measures:
204-
- name: active_users
205-
type: count_distinct
206-
sql: user_id
204+
- name: \\"active_users\\"
205+
type: \\"count_distinct\\"
206+
sql: \\"user_id\\"
207207
dimensions:
208-
- name: city
209-
type: string
210-
sql: city_column"
208+
- name: \\"city\\"
209+
type: \\"string\\"
210+
sql: \\"city_column\\""
211211
`;
212212

213213
exports[`Jinja (new api) render dump_context.yml.jinja: dump_context.yml.jinja 1`] = `
@@ -216,18 +216,18 @@ exports[`Jinja (new api) render dump_context.yml.jinja: dump_context.yml.jinja 1
216216
print:
217217
bool_true: true
218218
bool_false: false
219-
string: test string
219+
string: \\"test string\\"
220220
int: 1
221221
float: 3.1415
222-
array_int: [9, 8, 7, 6, 5, 0, 1, 2, 3, 4]
223-
array_bool: [true, false, false, true]
224-
null: none
225-
undefined: none
222+
array_int: [9,8,7,6,5,0,1,2,3,4]
223+
array_bool: [true,false,false,true]
224+
null: null
225+
undefined: null
226226
security_context:
227227
userId: 1
228228
env_var:
229-
exist: test
230-
unknown_fallback: value"
229+
exist: \\"test\\"
230+
unknown_fallback: \\"value\\""
231231
`;
232232

233233
exports[`Jinja (new api) render python.yml: python.yml 1`] = `
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{%- macro markup(column1, column2) -%}
22
(({{column1}} - {{column2}}) / {{column2}})
3-
{%- endmacro -%}
3+
{%- endmacro -%}
4+
5+
{% macro escape_single_quotes(expression) -%}
6+
{{ expression | replace("'", "''") | safe }}
7+
{%- endmacro %}

packages/cubejs-backend-native/test/templates/01.yml.jinja

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
{%- set payment_methods = [
2-
"bank_transfer",
3-
"credit_card",
4-
"gift_card"
5-
] -%}
1+
{%- import ".utils.jinja" as utils -%}
2+
3+
{%- set payment_methods = {
4+
"bank_transfer": "TRANSFER",
5+
"credit_card": "CREDIT",
6+
"gift_card": "GIFT"
7+
} -%}
68

79
cubes:
810
- name: cube_01_1
911
sql: >
1012
SELECT
1113
order_id,
12-
{%- for payment_method in payment_methods %}
13-
SUM(CASE WHEN payment_method = '{{payment_method}}' THEN amount END) AS {{payment_method}}_amount,
14+
{%- for method, title in payment_methods | items %}
15+
SUM(CASE WHEN payment_method = '{{ utils.escape_single_quotes(title) }}' THEN amount END) AS {{ method | safe }}_amount,
1416
{%- endfor %}
1517
SUM(amount) AS total_amount
1618
FROM app_data.payments
@@ -20,8 +22,8 @@ cubes:
2022
sql: >
2123
SELECT
2224
order_id,
23-
{%- for payment_method in payment_methods %}
24-
SUM(CASE WHEN payment_method = '{{payment_method}}' THEN amount END) AS {{payment_method}}_amount
25+
{%- for method, title in payment_methods | items %}
26+
SUM(CASE WHEN payment_method = '{{ utils.escape_single_quotes(title) }}' THEN amount END) AS {{ method | safe }}_amount
2527
{%- if not loop.last %},{% endif %}
2628
{%- endfor %}
2729
FROM app_data.payments

packages/cubejs-backend-native/test/templates/02.yml.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ cubes:
1111
sql: >
1212
SELECT
1313
{%- for prop in nested_properties %}
14-
{{ prop }}_prop.value AS {{ prop }}
14+
{{ prop | safe }}_prop.value AS {{ prop | safe }}
1515
{%- endfor %}
1616
FROM public.events
1717
{%- for prop in nested_properties %}
18-
LEFT JOIN UNNEST(properties) AS {{ prop }}_prop ON {{ prop }}_prop.key = '{{ prop }}'
18+
LEFT JOIN UNNEST(properties) AS {{ prop | safe }}_prop ON {{ prop | safe }}_prop.key = '{{ prop | safe }}'
1919
{%- endfor %}

packages/cubejs-backend-native/test/templates/03.yml.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ cubes:
66
{%- for country in countries %}
77
SELECT
88
*,
9-
'{{ country }}' as country
10-
FROM {{ country }}_orders
9+
'{{ country | safe }}' as country
10+
FROM {{ country | safe }}_orders
1111
{% if not loop.last %}
1212
UNION ALL
1313
{% endif %}

packages/cubejs-backend-native/test/templates/04.yml.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cubes:
1414
FROM public.events
1515
WHERE
1616
{%- for inner_table in tables %}
17-
{FILTER_PARAMS.cube_04_{{ inner_table }}.timestamp.filter('timestamp')}
17+
{FILTER_PARAMS.cube_04_{{ inner_table | safe }}.timestamp.filter('timestamp')}
1818
{%- if not loop.last %} AND {% endif %}
1919
{%- endfor %}
2020

@@ -25,7 +25,7 @@ cubes:
2525
{% endfor %}
2626

2727
{%- for table in tables[1:] %}
28-
- name: cube_04_{{ table }}
28+
- name: cube_04_{{ table | safe }}
2929
extends: {{ tables[0] }}
3030
sql_table: public.events
3131

packages/cubejs-backend-native/test/templates/05.yml.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ cubes:
1616
type: count_distinct
1717
sql: user_id
1818
rolling_window:
19-
trailing: {{ days }} day
19+
trailing: {{ days | safe }} day
2020
offset: start
2121
{% endfor %}

0 commit comments

Comments
 (0)