Skip to content

Register template filters on the fallback Jinja2 environment - #1243

Open
Sanjays2402 wants to merge 1 commit into
geopython:masterfrom
Sanjays2402:fix/j2-fallback-filters-1236
Open

Register template filters on the fallback Jinja2 environment#1243
Sanjays2402 wants to merge 1 commit into
geopython:masterfrom
Sanjays2402:fix/j2-fallback-filters-1236

Conversation

@Sanjays2402

Copy link
Copy Markdown

Overview

render_j2_template() registers the to_json filter on the Environment it
creates, but when a template is missing from a custom server.templates.path
the fallback Environment for the built-in templates is created without it. Any
built-in template using {{ ... | to_json }} (items.html, item.html,
stac_items.html) then fails with
TemplateAssertionError: No filter named 'to_json', so a partial custom
templates directory returns HTTP 500 instead of falling back cleanly.

This registers the filter and global on the fallback environment too.

Related Issue / Discussion

Closes #1236.

Additional Information

New tests/unittests/test_ogc_api_util.py covers both the custom-template
override and the fallback path; the fallback test fails with the exact reported
TemplateAssertionError without this change and passes with it. tests/unittests
is green (146 passed) and flake8 is clean on both changed files.

This change was prepared with AI assistance; the regression test was run locally
and fails without the fix.

Contributions and Licensing

(as per https://github.com/geopython/pycsw/blob/master/CONTRIBUTING.rst#contributions-and-licensing)

  • I'd like to contribute bugfix to pycsw. I confirm that my contributions to pycsw will be compatible with the pycsw license guidelines at the time of contribution.
  • I have already previously agreed to the pycsw Contributions and Licensing Guidelines

When server.templates.path is configured, render_j2_template() creates a
custom Environment and registers the to_json filter on it. If a requested
template is not present in that directory, a second Environment is built
for the built-in templates, but no filters were registered on it, so any
built-in template using {{ ... | to_json }} (items.html, item.html,
stac_items.html) failed with TemplateAssertionError: No filter named
'to_json'.

Register the filter and global on the fallback environment as well, so a
partial custom templates directory transparently falls back to the
built-in templates.

Adds tests/unittests/test_ogc_api_util.py covering both the custom
template override and the fallback path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

render_j2_template fallback environment missing custom filters (to_json) when custom templates.path is set

1 participant