From d6fc6051b6144aeaf0e844567f9251391783b4f5 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Mon, 6 Nov 2023 14:04:39 +0100 Subject: [PATCH] Qute: fix validation of array index-based expressions - such as {myArray[1].name} --- .../qute/deployment/QuteProcessor.java | 6 ++++-- .../CheckedTemplateArrayParamTest.java | 20 +++++++++++++++++-- 2 files changed, 22 insertions(+), 4 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 0fc9df79094f6..5c08077669ebb 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 @@ -1422,7 +1422,8 @@ private static boolean processArray(Info info, MatchResult match) { // myArray[0], myArray.1 try { Integer.parseInt(name); - match.setValues(null, match.type().asArrayType().constituent()); + Type constituent = match.type().asArrayType().constituent(); + match.setValues(match.assignabilityCheck.computingIndex.getClassByName(constituent.name()), constituent); return true; } catch (NumberFormatException e) { // not an integer index @@ -1436,7 +1437,8 @@ private static boolean processArray(Info info, MatchResult match) { Expression param = params.get(0); Object literalValue = param.getLiteral(); if (literalValue == null || literalValue instanceof Integer) { - match.setValues(null, match.type().asArrayType().constituent()); + Type constituent = match.type().asArrayType().constituent(); + match.setValues(match.assignabilityCheck.computingIndex.getClassByName(constituent.name()), constituent); return true; } } else if (name.equals("take") || name.equals("takeLast")) { diff --git a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/CheckedTemplateArrayParamTest.java b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/CheckedTemplateArrayParamTest.java index a6fd2b8a729f3..1e41d08c99482 100644 --- a/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/CheckedTemplateArrayParamTest.java +++ b/extensions/qute/deployment/src/test/java/io/quarkus/qute/deployment/typesafe/CheckedTemplateArrayParamTest.java @@ -15,14 +15,18 @@ public class CheckedTemplateArrayParamTest { @RegisterExtension static final QuarkusUnitTest config = new QuarkusUnitTest() .withApplicationRoot((jar) -> jar - .addClasses(Templates.class) + .addClasses(Templates.class, Color.class) .addAsResource(new StringAsset("Hello {myArray[1]}! ::{myArray.take(1).size}"), - "templates/CheckedTemplateArrayParamTest/arrays.txt")); + "templates/CheckedTemplateArrayParamTest/arrays.txt") + .addAsResource(new StringAsset("{array.0.asStr}"), + "templates/CheckedTemplateArrayParamTest/colors.txt")); @Test public void testBasePath() { assertEquals("Hello 1! ::1", Templates.arrays(new int[] { 0, 1 }).render()); + assertEquals("RED", + Templates.colors(new Color[] { Color.RED, Color.BLUE }).render()); } @CheckedTemplate @@ -30,6 +34,18 @@ public static class Templates { static native TemplateInstance arrays(int[] myArray); + static native TemplateInstance colors(Color[] array); + + } + + public static enum Color { + + RED, + BLUE; + + public String asStr() { + return toString(); + } } }