eschutho opened a new pull request, #45742:
URL: https://github.com/apache/superset/pull/45742
### SUMMARY
**Problem.** `SqlaTable._render_adhoc_expression_for_metadata_lookup`
renders Jinja in an adhoc column's `sqlExpression` before the metadata lookup,
but only caught `SupersetSyntaxErrorException`.
`BaseTemplateProcessor.process_template` re-raises a raw
`jinja2.exceptions.UndefinedError` when the undefined name isn't shaped like a
function call (e.g. `{{ foo.bar }}`). That error escaped
`adhoc_column_to_sqla`, was not handled by the chart-data API (which only maps
`QueryObjectValidationError` to a 400), and fell through to the catch-all
`Exception` handler as an opaque 500.
**Fix.** Catch `(TemplateError, SupersetSyntaxErrorException)` and raise
`QueryObjectValidationError` with the existing message text (unchanged).
`UndefinedError` subclasses `TemplateError`. This matches how sibling sites in
the same file already behave (the `except UndefinedError` / `except
(TemplateError, SupersetSyntaxErrorException)` handlers). Only this one site is
changed.
Same bug class as #42366 and #42401.
**Behavior change.** A template error in an adhoc column expression now
returns a 400 with a validation message instead of a raw 500. The catch is
additive; no previously handled path changes.
### BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
N/A
### TESTING INSTRUCTIONS
Added `test_adhoc_column_to_sqla_wraps_undefined_error_in_metadata_lookup`
in `tests/unit_tests/connectors/sqla/models_test.py`. It uses a real
`JinjaTemplateProcessor` and an adhoc column with `sqlExpression="{{ foo.bar
}}"`, and asserts `QueryObjectValidationError`. Before the fix it fails with
`jinja2.exceptions.UndefinedError: 'foo' is undefined`; after, it passes.
```
pytest tests/unit_tests/connectors/sqla/models_test.py -k
wraps_undefined_error_in_metadata
```
Manual: POST a chart-data request with an adhoc column whose `sqlExpression`
is `{{ foo.bar }}`; expect a 400 rather than a 500.
Notes on verification:
- That chart-data requests reach `SqlaTable.adhoc_column_to_sqla` (which
overrides the `ExploreMixin` one) was confirmed by reading the code, not by a
live request.
- `tests/unit_tests/connectors/sqla/models_test.py` has 6 unrelated failures
that also occur without this change (cross-catalog table quoting:
`test_get_sqla_table_*`, `test_quoted_name_prevents_double_quoting`).
### ADDITIONAL INFORMATION
- [ ] Has associated issue:
- [ ] Required feature flags:
- [ ] Changes UI
- [ ] Includes DB Migration (follow approval process in
[SIP-59](https://github.com/apache/superset/issues/13351))
- [ ] Migration is atomic, supports rollback & is backwards-compatible
- [ ] Confirm DB migration upgrade and downgrade tested
- [ ] Runtime estimates and downtime expectations provided
- [ ] Introduces new feature or API
- [ ] Removes existing feature or API
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]