Skip to content

Commit 3301cdd

Browse files
committed
fix: @ConfigProperty for java.util.Duration with defaultValue flagged as
error Fixes #1207 Signed-off-by: azerr <[email protected]>
1 parent 869ead4 commit 3301cdd

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

projects/lsp4mp/projects/maven/config-quickstart/src/main/java/org/acme/config/DefaultValueResource.java

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.ws.rs.Path;
44
import org.eclipse.microprofile.config.inject.ConfigProperty;
55

6+
import java.time.Duration;
7+
68
@Path("/greeting")
79
public class DefaultValueResource {
810

@@ -35,4 +37,27 @@ public class DefaultValueResource {
3537

3638
@ConfigProperty(name = "greeting10", defaultValue="AB")
3739
char greeting10;
38-
}
40+
41+
@ConfigProperty(name = "greeting11", defaultValue="1")
42+
int greeting11;
43+
44+
@ConfigProperty(name = "greeting12", defaultValue="1")
45+
Integer greeting12;
46+
47+
@ConfigProperty(name = "greeting13", defaultValue = "PT15M")
48+
Duration greeting13;
49+
50+
@ConfigProperty(name = "greeting14", defaultValue = "PT15")
51+
Duration greeting14;
52+
53+
public static enum Profile {
54+
admin,
55+
user
56+
}
57+
58+
@ConfigProperty(name = "greeting15", defaultValue = "user")
59+
Profile greeting15;
60+
61+
@ConfigProperty(name = "greeting16", defaultValue = "userXXX")
62+
Profile greeting16;
63+
}

src/main/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/internal/config/java/MicroProfileConfigASTValidator.java

+19
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
import org.eclipse.lsp4mp.commons.utils.AntPathMatcher;
3232

3333
import java.text.MessageFormat;
34+
import java.time.Duration;
3435
import java.util.List;
3536
import java.util.logging.Logger;
3637
import java.util.regex.Pattern;
38+
import java.util.stream.Stream;
3739

3840
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.MicroProfileConfigConstants.*;
3941
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils.getAnnotationMemberValueExpression;
@@ -274,7 +276,24 @@ private static boolean isAssignable(String typeFqn, String value, Module javaPro
274276
return PsiTypeUtils.findType(javaProject, value) != null;
275277
case "java.lang.String":
276278
return true;
279+
case "java.time.Duration":
280+
try {
281+
Duration.parse(value);
282+
return true;
283+
}
284+
catch(Exception e) {
285+
return false;
286+
}
277287
default:
288+
PsiClass type = PsiTypeUtils.findType(javaProject, typeFqn);
289+
if (type != null) {
290+
if (type.isEnum()) {
291+
return Stream.of(type.getFields())
292+
.anyMatch(e -> e.getName().equals(value));
293+
294+
295+
}
296+
}
278297
return false;
279298
}
280299
} catch (NumberFormatException e) {

src/test/java/com/redhat/devtools/intellij/lsp4mp4ij/psi/core/config/java/MicroProfileConfigJavaDiagnosticsTest.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,16 @@ public void testImproperDefaultValues() throws Exception {
8181
MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE,
8282
MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE);
8383

84+
Diagnostic d6 = d(49, 56, 62, "'PT15' does not match the expected type of 'Duration'.", DiagnosticSeverity.Error,
85+
MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE,
86+
MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE);
87+
88+
Diagnostic d7 = d(60, 56, 65, "'user' does not match the expected type of 'Profile'.", DiagnosticSeverity.Error,
89+
MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE,
90+
MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE);
91+
8492
assertJavaDiagnostics(diagnosticsParams, utils, //
85-
d1, d2, d3, d4, d5);
93+
d1, d2, d3, d4, d5, d6, d7);
8694
}
8795

8896
@Test

0 commit comments

Comments
 (0)