From 1ef378a42135e87ae5a6da0d118c1aaf4914d5a6 Mon Sep 17 00:00:00 2001 From: Fred Bricon Date: Fri, 7 Jul 2023 12:55:13 +0200 Subject: [PATCH] feat: Validate defaultValue for lists and arrays Port of https://github.com/eclipse/lsp4mp/commit/a95a3f719ee88a61072b6df0eadf7a06b391bd89 Signed-off-by: Fred Bricon --- .../acme/config/DefaultValueListResource.java | 41 +++++ .../org/acme/config/DefaultValueResource.java | 2 + .../java/MicroProfileConfigASTValidator.java | 142 ++++++++++++------ .../java/MicroProfileConfigErrorCode.java | 2 +- ...MicroProfileConfigJavaDiagnosticsTest.java | 47 +++++- .../ConfigItemIntBoolDefaultValueTest.java | 2 +- .../MicroProfileConfigPropertyTest.java | 4 +- 7 files changed, 188 insertions(+), 52 deletions(-) create mode 100644 projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueListResource.java diff --git a/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueListResource.java b/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueListResource.java new file mode 100644 index 000000000..9547cbf2b --- /dev/null +++ b/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueListResource.java @@ -0,0 +1,41 @@ +package org.acme.config; + +import javax.ws.rs.Path; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import java.util.List; +import java.util.Set; + +@Path("/greetings") +public class DefaultValueListResource { + + @ConfigProperty(name = "listprop1", defaultValue="foo") + List greeting1; + + @ConfigProperty(name = "listprop2", defaultValue="12,13,14") + List greeting2; + + @ConfigProperty(name = "listprop3", defaultValue="12,13,14") + int[] greeting3; + + @ConfigProperty(name = "listprop4", defaultValue="12,13\\,14") + int[] greeting4; + + @ConfigProperty(name = "listprop5", defaultValue="1") + List greeting5; + + @ConfigProperty(name = "listprop6", defaultValue=",,,,,,,,") + Set greeting6; + + @ConfigProperty(name = "listprop7", defaultValue="1.0,2.0,3.0") + float[] greeting7; + + @ConfigProperty(name = "listprop8", defaultValue="AB,CD") + char[] greeting8; + + @ConfigProperty(name = "listprop9", defaultValue=",,,,") + char[] greeting9; + + @ConfigProperty(name = "listprop10", defaultValue="") + List greeting10; + +} \ No newline at end of file diff --git a/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueResource.java b/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueResource.java index a8916fdba..dcf264ce3 100644 --- a/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueResource.java +++ b/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueResource.java @@ -33,4 +33,6 @@ public class DefaultValueResource { @ConfigProperty(name = "greeting9") String greeting9; + @ConfigProperty(name = "greeting10", defaultValue="AB") + char greeting10; } diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/config/java/MicroProfileConfigASTValidator.java b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/config/java/MicroProfileConfigASTValidator.java index 3164cd4e4..22adb2bee 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/config/java/MicroProfileConfigASTValidator.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/config/java/MicroProfileConfigASTValidator.java @@ -15,13 +15,9 @@ import com.google.gson.JsonObject; import com.intellij.openapi.module.Module; -import com.intellij.psi.PsiAnnotation; -import com.intellij.psi.PsiAnnotationMemberValue; -import com.intellij.psi.PsiClass; -import com.intellij.psi.PsiField; -import com.intellij.psi.PsiLiteral; -import com.intellij.psi.PsiType; +import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.TypeConversionUtil; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.MicroProfileConfigConstants; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.diagnostics.JavaDiagnosticsContext; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.validators.JavaASTValidator; @@ -38,6 +34,7 @@ import java.text.MessageFormat; import java.util.List; import java.util.logging.Logger; +import java.util.regex.Pattern; import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.MicroProfileConfigConstants.CONFIG_PROPERTIES_ANNOTATION; import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION; @@ -65,8 +62,12 @@ public class MicroProfileConfigASTValidator extends JavaASTValidator { private static final AntPathMatcher pathMatcher = new AntPathMatcher(); + private static final Pattern ARRAY_SPLITTER = Pattern.compile("(?'.", DiagnosticSeverity.Error, + MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, + MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE); + + Diagnostic d2 = d(19, 53, 65, "'12,13\\,14' does not match the expected type of 'int[]'.", DiagnosticSeverity.Error, + MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, + MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE); + + Diagnostic d3 = d(31, 53, 60, "'AB,CD' does not match the expected type of 'char[]'.", DiagnosticSeverity.Error, + MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, + MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE); + + Diagnostic d4 = d(34, 53, 59, "',,,,' does not match the expected type of 'char[]'.", DiagnosticSeverity.Error, + MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, + MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE); + + Diagnostic d5 = d(37, 54, 56, "'defaultValue=\"\"' will behave as if no default value is set, and will not be treated as an empty 'List'.", DiagnosticSeverity.Warning, + MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, + MicroProfileConfigErrorCode.EMPTY_LIST_NOT_SUPPORTED); + + assertJavaDiagnostics(diagnosticsParams, utils, // + d1, d2, d3, d4, d5); } @Test @@ -106,8 +145,12 @@ public void testNoValueAssignedWithIgnore() throws Exception { MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE); + Diagnostic d4 = d(35, 54, 58, "'AB' does not match the expected type of 'char'.", DiagnosticSeverity.Error, + MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, + MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE); + assertJavaDiagnostics(diagnosticsParams, utils, // - d1, d2, d3); + d1, d2, d3, d4); } @Test diff --git a/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/ConfigItemIntBoolDefaultValueTest.java b/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/ConfigItemIntBoolDefaultValueTest.java index e13d38311..9bbcab274 100644 --- a/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/ConfigItemIntBoolDefaultValueTest.java +++ b/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/ConfigItemIntBoolDefaultValueTest.java @@ -49,7 +49,7 @@ public void testConfigItemIntBoolDefaultValueTest() throws Exception { String intDefault = "0"; assertProperties(infoFromClasspath, - 259 + 20 /* properties from Java sources with ConfigProperty */ + // + 259 + 31 /* properties from Java sources with ConfigProperty */ + // 7 /* static properties from microprofile-context-propagation-api */ + 1 /* static property from microprofile config_ordinal */, diff --git a/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/MicroProfileConfigPropertyTest.java b/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/MicroProfileConfigPropertyTest.java index 50a5e6123..66e65102c 100644 --- a/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/MicroProfileConfigPropertyTest.java +++ b/src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/properties/MicroProfileConfigPropertyTest.java @@ -41,7 +41,7 @@ public void testConfigQuickstartFromClasspath() throws Exception { assertNotNull("Test existing of quarkus-core-deployment*.jar", f); assertProperties(infoFromClasspath, 257 /* properties from JAR */ + // - 20 /* properties from Java sources with ConfigProperty */ + // + 31 /* properties from Java sources with ConfigProperty */ + // 2 /* properties from Java sources with ConfigRoot */ + // 7 /* static properties from microprofile-context-propagation-api */ + 1 /* static property from microprofile config_ordinal */, @@ -118,7 +118,7 @@ public void testConfigQuickstartFromJavaSources() throws Exception { Module module = createMavenModule(new File("projects/lsp4mp/projects/maven/config-quickstart")); MicroProfileProjectInfo infoFromJavaSources = PropertiesManager.getInstance().getMicroProfileProjectInfo(module, MicroProfilePropertiesScope.ONLY_SOURCES, ClasspathKind.SRC, PsiUtilsLSImpl.getInstance(myProject), DocumentFormat.PlainText); - assertProperties(infoFromJavaSources, 20 /* properties from Java sources with ConfigProperty */ + // + assertProperties(infoFromJavaSources, 31 /* properties from Java sources with ConfigProperty */ + // 2 /* properties from Java sources with ConfigRoot */, // GreetingResource