Skip to content

Commit

Permalink
Fail at build time if SpEL is used in @value
Browse files Browse the repository at this point in the history
Fixes: quarkusio#19368
(cherry picked from commit 224c9c1)
  • Loading branch information
geoand authored and gsmet committed Aug 23, 2021
1 parent 8b9f4bd commit 03bb2bd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> fieldUsingList; // does not work
}
}

0 comments on commit 03bb2bd

Please sign in to comment.