From 03bb2bd1ce0c4576f27210720d68afe2b8045d73 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 23 Aug 2021 09:11:58 +0300 Subject: [PATCH] Fail at build time if SpEL is used in @Value Fixes: #19368 (cherry picked from commit 224c9c136359ae74ff55a1b26ae10607bc146e50) --- .../di/deployment/SpringDIProcessor.java | 8 +++- .../spring/di/deployment/SpelTest.java | 40 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 extensions/spring-di/deployment/src/test/java/io/quarkus/spring/di/deployment/SpelTest.java diff --git a/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java b/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java index bf12ca33fe460..15ea530b98433 100644 --- a/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java +++ b/extensions/spring-di/deployment/src/main/java/io/quarkus/spring/di/deployment/SpringDIProcessor.java @@ -522,7 +522,13 @@ private void addSpringValueAnnotations(AnnotationTarget target, AnnotationInstan return; } String defaultValue = null; - String propertyName = annotationValue.asString().replace("${", "").replace("}", ""); + String stringValue = annotationValue.asString(); + if (stringValue.contains("#{")) { + throw new IllegalArgumentException( + "SpEL expressions are not supported when using " + SPRING_VALUE_ANNOTATION + ". Offending value is '" + + annotation + "'"); + } + String propertyName = stringValue.replace("${", "").replace("}", ""); if (propertyName.contains(":")) { final int index = propertyName.indexOf(':'); if (index < propertyName.length() - 1) { diff --git a/extensions/spring-di/deployment/src/test/java/io/quarkus/spring/di/deployment/SpelTest.java b/extensions/spring-di/deployment/src/test/java/io/quarkus/spring/di/deployment/SpelTest.java new file mode 100644 index 0000000000000..ddc555105ac83 --- /dev/null +++ b/extensions/spring-di/deployment/src/test/java/io/quarkus/spring/di/deployment/SpelTest.java @@ -0,0 +1,40 @@ +package io.quarkus.spring.di.deployment; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.util.List; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import io.quarkus.test.QuarkusUnitTest; + +public class SpelTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest() + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClasses(SomeService.class)) + .assertException(e -> { + assertEquals(IllegalArgumentException.class, e.getClass()); + assertTrue(e.getMessage().contains("#{'${values.list}'.split(',')}")); + }); + + @Test + public void shouldNotBeInvoked() { + // This method should not be invoked + fail(); + } + + @Service + public static class SomeService { + + @Value("#{'${values.list}'.split(',')}") + List fieldUsingList; // does not work + } +}