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

Qute type-safe fragments - fix validation for loop metadata and globals #31867

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ public void beforeParsing(ParserHelper parserHelper) {
@BuildStep
void validateCheckedFragments(List<CheckedFragmentValidationBuildItem> validations,
List<TemplateExpressionMatchesBuildItem> expressionMatches,
List<TemplateGlobalBuildItem> templateGlobals,
BeanArchiveIndexBuildItem beanArchiveIndex,
BuildProducer<ValidationErrorBuildItem> validationErrors) {

Expand All @@ -682,6 +683,8 @@ void validateCheckedFragments(List<CheckedFragmentValidationBuildItem> validatio
Map<DotName, AssignableInfo> assignableCache = new HashMap<>();
String[] hintPrefixes = { LoopSectionHelper.Factory.HINT_PREFIX, WhenSectionHelper.Factory.HINT_PREFIX,
SetSectionHelper.Factory.HINT_PREFIX };
Set<String> globals = templateGlobals.stream().map(TemplateGlobalBuildItem::getName)
.collect(Collectors.toUnmodifiableSet());

for (CheckedFragmentValidationBuildItem validation : validations) {
Map<String, Type> paramNamesToTypes = new HashMap<>();
Expand All @@ -697,33 +700,40 @@ void validateCheckedFragments(List<CheckedFragmentValidationBuildItem> validatio
}

for (Expression expression : validation.fragmentExpressions) {
// Note that we ignore literals and expressions with no type info and expressions with a hint referencing an expression from inside the fragment
if (expression.isLiteral()) {
// Note that we ignore:
// - literals,
// - globals,
// - expressions with no type info,
// - loop metadata; e.g. |java.lang.Integer|<loop-metadata>
// - expressions with a hint referencing an expression from inside the fragment
if (expression.isLiteral() || globals.contains(expression.getParts().get(0).getName())) {
continue;
}
if (expression.hasTypeInfo()) {
Info info = TypeInfos.create(expression, index, null).get(0);
if (info.isTypeInfo()) {
// |org.acme.Foo|.name
paramNamesToTypes.put(expression.getParts().get(0).getName(), info.asTypeInfo().resolvedType);
} else if (info.hasHints()) {
// foo<set#123>.name
hintLoop: for (String helperHint : info.asHintInfo().hints) {
for (String prefix : hintPrefixes) {
if (helperHint.startsWith(prefix)) {
int generatedId = parseHintId(helperHint, prefix);
Expression localExpression = findExpression(generatedId, validation.fragmentExpressions);
if (localExpression == null) {
Match match = matchResults.getMatch(generatedId);
if (match == null) {
throw new IllegalStateException(
"Match result not found for expression [" + expression.toOriginalString()
+ "] in: "
+ validation.templateId);
}
paramNamesToTypes.put(expression.getParts().get(0).getName(), match.type);
break hintLoop;
String typeInfo = expression.getParts().get(0).getTypeInfo();
if (typeInfo == null || (typeInfo != null && typeInfo.endsWith(LoopSectionHelper.Factory.HINT_METADATA))) {
continue;
}
Info info = TypeInfos.create(expression, index, null).get(0);
if (info.isTypeInfo()) {
// |org.acme.Foo|.name
paramNamesToTypes.put(expression.getParts().get(0).getName(), info.asTypeInfo().resolvedType);
} else if (info.hasHints()) {
// foo<set#123>.name
hintLoop: for (String helperHint : info.asHintInfo().hints) {
for (String prefix : hintPrefixes) {
if (helperHint.startsWith(prefix)) {
int generatedId = parseHintId(helperHint, prefix);
Expression localExpression = findExpression(generatedId, validation.fragmentExpressions);
if (localExpression == null) {
Match match = matchResults.getMatch(generatedId);
if (match == null) {
throw new IllegalStateException(
"Match result not found for expression [" + expression.toOriginalString()
+ "] in: "
+ validation.templateId);
}
paramNamesToTypes.put(expression.getParts().get(0).getName(), match.type);
break hintLoop;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.qute.CheckedTemplate;
import io.quarkus.qute.TemplateGlobal;
import io.quarkus.qute.TemplateInstance;
import io.quarkus.test.QuarkusUnitTest;

Expand All @@ -20,21 +21,30 @@ public class CheckedTemplateFragmentTest {
.addClasses(Templates.class, Item.class)
.addAsResource(new StringAsset(
"{#each items}{#fragment id='item'}{it.name}{#if it.name.length > 5} is a long name{/if}{/fragment}{/each}"),
"templates/CheckedTemplateFragmentTest/items.html"));
"templates/CheckedTemplateFragmentTest/items.html")
.addAsResource(new StringAsset(
"{#fragment id=foo}{#for i in bar}{i_count}. <{i}>{#if i_hasNext}, {/if}{/for}{/fragment}"),
"templates/CheckedTemplateFragmentTest/foos.html"));

@Test
public void testFragment() {
assertEquals("Foo", Templates.items(null).getFragment("item").data("it", new Item("Foo")).render());
assertEquals("Foo", Templates.items$item(new Item("Foo")).render());
assertEquals("FooAndBar is a long name", Templates.items$item(new Item("FooAndBar")).render());
assertEquals("1. <1>, 2. <2>, 3. <3>, 4. <4>, 5. <5>", Templates.foos$foo().render());
}

@CheckedTemplate
public static class Templates {

@TemplateGlobal
static int bar = 5;

static native TemplateInstance items(List<Item> items);

static native TemplateInstance items$item(Item it);

static native TemplateInstance foos$foo();
}

}