From 3688a986c52c89711313eac73a9ec1bb7b9b4149 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Fri, 31 Mar 2023 14:47:46 +0200 Subject: [PATCH] Qute - fix validation of expressions with the "cdi" namespace - if a bean that has iterable in its set of bean types is used as an iterable in a loop section then the hints are not processed correctly --- .../io/quarkus/qute/deployment/QuteProcessor.java | 13 +++++++++++-- .../inject/NamedBeanIterableReturnTypeTest.java | 10 +++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java index 0863b63b7e4e3..6735401995acf 100644 --- a/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java +++ b/extensions/qute/deployment/src/main/java/io/quarkus/qute/deployment/QuteProcessor.java @@ -1049,6 +1049,8 @@ static Match validateNestedExpressions(QuteConfig config, TemplateAnalysis templ BeanInfo bean = findBean(expression, index, incorrectExpressions, namedBeans); if (bean != null) { rootClazz = bean.getImplClazz(); + // Skip the first part - the name of the bean, e.g. for {inject:foo.name} we start validation with "name" + match.setValues(rootClazz, bean.getProviderType()); } else { // Bean not found return putResult(match, results, expression); @@ -1197,8 +1199,15 @@ static Match validateNestedExpressions(QuteConfig config, TemplateAnalysis templ } } else { if (INJECT_NAMESPACE.equals(namespace) || CDI_NAMESPACE.equals(namespace)) { - // Skip the first part - the name of the bean, e.g. for {inject:foo.name} we start validation with "name" - match.setValues(rootClazz, Type.create(rootClazz.name(), org.jboss.jandex.Type.Kind.CLASS)); + if (root.hasHints()) { + // Root is not a type info but a property with hint + // E.g. 'it' and 'STATUS' + if (processHints(templateAnalysis, root.asHintInfo().hints, match, index, expression, + generatedIdsToMatches, incorrectExpressions)) { + // In some cases it's necessary to reset the iterator + iterator = parts.iterator(); + } + } } else if (templateData != null) { // Set the root type and reset the iterator match.setValues(rootClazz, Type.create(rootClazz.name(), org.jboss.jandex.Type.Kind.CLASS)); diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/inject/NamedBeanIterableReturnTypeTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/inject/NamedBeanIterableReturnTypeTest.java index 32ff849ef855b..15bb3870c713d 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/inject/NamedBeanIterableReturnTypeTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/inject/NamedBeanIterableReturnTypeTest.java @@ -5,6 +5,7 @@ import java.util.List; import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Produces; import jakarta.inject.Inject; import jakarta.inject.Named; @@ -26,6 +27,7 @@ public class NamedBeanIterableReturnTypeTest { "{@java.lang.String field}" + "{#if cdi:validation.hasViolations(field)}" + "{#each cdi:validation.getViolations(field)}{it}{/each}" + + "{#each cdi:violations}:{it.toUpperCase}{/each}" + "{/if}"), "templates/validate.html")); @@ -34,7 +36,7 @@ public class NamedBeanIterableReturnTypeTest { @Test public void testResult() { - assertEquals("Foo!", validate.data("field", "foo").render()); + assertEquals("Foo!:BAR:BAZ", validate.data("field", "foo").render()); } @ApplicationScoped @@ -48,6 +50,12 @@ public boolean hasViolations(String field) { public List getViolations(String field) { return List.of("Foo!"); } + + @Named("violations") + @Produces + public List getViolations() { + return List.of("bar", "baz"); + } } }