Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush local filter and test cache when processing parent_instructions #552

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to MiniJinja are documented here.

## 2.1.2

- Flush filter and test cache when processing extended template.
This fixes a bug that caused the wrong filters to be used in some
cases. #551

## 2.1.1

- Added `indent` parameter to `tojson` filter. #546
Expand Down
7 changes: 6 additions & 1 deletion minijinja/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl<'env> Vm<'env> {
}};
}

// looks nicer nice way
// looks nicer this way
#[allow(clippy::while_let_loop)]
loop {
let instr = match state.instructions.get(pc) {
Expand All @@ -231,6 +231,11 @@ impl<'env> Vm<'env> {
};
out.end_capture(AutoEscape::None);
pc = 0;
// because we swap out the instructions we also need to unload all
// the filters and tests to ensure that we are not accidentally
// reusing the local_ids for completely different filters.
loaded_filters = [None; MAX_LOCALS];
loaded_tests = [None; MAX_LOCALS];
continue;
}
};
Expand Down
29 changes: 29 additions & 0 deletions minijinja/tests/test_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,32 @@ fn test_multiple_extended_includes_in_loop() {
let rv = env.get_template("main.txt").unwrap().render(()).unwrap();
assert_eq!(rv, "012");
}

/// See https://github.com/mitsuhiko/minijinja/issues/551
#[test]
fn test_filter_caching() {
let mut env = Environment::new();
env.add_template("parent.txt", "{{ 'Hello Foo' | lower }}")
.unwrap();
env.add_template(
"child.txt",
"{% extends 'parent.txt' %}{% set dummy = 'a' | upper %}",
)
.unwrap();
let rv = env.get_template("child.txt").unwrap().render(()).unwrap();
assert_eq!(rv, "hello foo");
}

/// See https://github.com/mitsuhiko/minijinja/issues/551
#[test]
fn test_test_caching() {
let mut env = Environment::new();
env.add_template("parent.txt", "{{ 42 is odd }}").unwrap();
env.add_template(
"child.txt",
"{% extends 'parent.txt' %}{% set dummy = 23 is even %}",
)
.unwrap();
let rv = env.get_template("child.txt").unwrap().render(()).unwrap();
assert_eq!(rv, "false");
}