Skip to content

Commit

Permalink
Qute: fix handling of missing properties in strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Nov 25, 2024
1 parent 1266b57 commit dd01531
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean isFailure() {

public T get() {
if (exception != null) {
// Throw an exception if completed exceptionally
// Always wrap the original exception if completed exceptionally
throw new TemplateException(exception);
}
return result;
Expand Down Expand Up @@ -285,8 +285,12 @@ public CompletionStage<T> whenComplete(BiConsumer<? super T, ? super Throwable>
Objects.requireNonNull(action).accept(result, exception);
} catch (Throwable e) {
if (exception == null) {
// "if this stage completed normally but the supplied action throws an exception,
// then the returned stage completes exceptionally with the supplied action's exception"
return new CompletedStage<>(null, e);
}
// if this stage completed exceptionally and the supplied action throws an exception,
// then the returned stage completes exceptionally with this stage's exception
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private CompletionStage<Object> resolveNamespace(EvalContext context, Resolution
// Continue to the next part of the expression
return resolveReference(false, r, parts, resolutionContext, expression, 1);
} else if (strictRendering) {
throw propertyNotFound(r, expression);
return CompletedStage.failure(propertyNotFound(r, expression));
}
return Results.notFound(context);
}
Expand Down Expand Up @@ -199,9 +199,9 @@ private CompletionStage<Object> resolve(EvalContextImpl evalContext, Iterator<Va
notFound = Results.NotFound.from(evalContext);
}
}
// If in strict mode then just throw an exception
// If in strict mode then just fail
if (strictRendering && isLastPart) {
throw propertyNotFound(notFound, expression);
return CompletedStage.failure(propertyNotFound(notFound, expression));
}
return CompletedStage.of(notFound);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.quarkus.qute;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.Map;

import org.junit.jupiter.api.Test;

/**
* Tests based on the description from
* <a href="https://github.com/quarkusio/quarkus/issues/44674">https://github.com/quarkusio/quarkus/issues/44674</a>.
*/
public class LetTimeoutTest {

Engine engine = Engine.builder().addDefaults().build();

private static final Map<String, Object> DATA = Map.of(
"a", Map.of("b", Map.of()));

@Test
void withDataFactoryMethod() {
TemplateInstance instance = engine.parse("""
{#let b = a.b}
{c}
{/let}
""").data(DATA);

assertThatThrownBy(instance::render)
.isInstanceOf(TemplateException.class)
.hasRootCauseMessage("Rendering error: Key \"c\" not found in the map with keys [a] in expression {c}");
}

@Test
void withInstanceThenDataForEachEntry() {
TemplateInstance instance = engine.parse("""
{#let b=a.b}
{c}
{/let}
""").instance();
for (var e : DATA.entrySet()) {
instance.data(e.getKey(), e.getValue());
}
assertThatThrownBy(instance::render)
.isInstanceOf(TemplateException.class)
.hasRootCauseMessage(
"Rendering error: Key \"c\" not found in the template data map with keys [a] in expression {c}");
}

@Test
void withSet_withInstanceThenDataForEachEntry() {
TemplateInstance instance = engine.parse("""
{#set b = a.b}
{c}
{/set}
""").instance();
for (var e : DATA.entrySet()) {
instance.data(e.getKey(), e.getValue());
}
assertThatThrownBy(instance::render)
.isInstanceOf(TemplateException.class)
.hasRootCauseMessage(
"Rendering error: Key \"c\" not found in the template data map with keys [a] in expression {c}");
}

@Test
void withLetWithoutEndTagwithInstanceThenDataForEachEntry() {
TemplateInstance instance = engine.parse("""
{#let b = a.b}
{c}
""").instance();
for (var e : DATA.entrySet()) {
instance.data(e.getKey(), e.getValue());
}
assertThatThrownBy(instance::render)
.isInstanceOf(TemplateException.class)
.hasRootCauseMessage(
"Rendering error: Key \"c\" not found in the template data map with keys [a] in expression {c}");
}
}

0 comments on commit dd01531

Please sign in to comment.