Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: @ConfigProperty for java.util.Duration with defaultValue flagged as error #1366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import javax.ws.rs.Path;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import java.time.Duration;

@Path("/greeting")
public class DefaultValueResource {

Expand Down Expand Up @@ -35,4 +37,27 @@ public class DefaultValueResource {

@ConfigProperty(name = "greeting10", defaultValue="AB")
char greeting10;
}

@ConfigProperty(name = "greeting11", defaultValue="1")
int greeting11;

@ConfigProperty(name = "greeting12", defaultValue="1")
Integer greeting12;

@ConfigProperty(name = "greeting13", defaultValue = "PT15M")
Duration greeting13;

@ConfigProperty(name = "greeting14", defaultValue = "PT15")
Duration greeting14;

public static enum Profile {
admin,
user
}

@ConfigProperty(name = "greeting15", defaultValue = "user")
Profile greeting15;

@ConfigProperty(name = "greeting16", defaultValue = "userXXX")
Profile greeting16;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import org.eclipse.lsp4mp.commons.utils.AntPathMatcher;

import java.text.MessageFormat;
import java.time.Duration;
import java.util.List;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.MicroProfileConfigConstants.*;
import static com.redhat.devtools.intellij.lsp4mp4ij.psi.core.utils.AnnotationUtils.getAnnotationMemberValueExpression;
Expand Down Expand Up @@ -274,7 +276,24 @@ private static boolean isAssignable(String typeFqn, String value, Module javaPro
return PsiTypeUtils.findType(javaProject, value) != null;
case "java.lang.String":
return true;
case "java.time.Duration":
try {
Duration.parse(value);
return true;
}
catch(Exception e) {
return false;
}
default:
PsiClass type = PsiTypeUtils.findType(javaProject, typeFqn);
if (type != null) {
if (type.isEnum()) {
return Stream.of(type.getFields())
.anyMatch(e -> e.getName().equals(value));


}
}
return false;
}
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,16 @@ public void testImproperDefaultValues() throws Exception {
MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE,
MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE);

Diagnostic d6 = d(49, 56, 62, "'PT15' does not match the expected type of 'Duration'.", DiagnosticSeverity.Error,
MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE,
MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE);

Diagnostic d7 = d(60, 56, 65, "'user' does not match the expected type of 'Profile'.", DiagnosticSeverity.Error,
MicroProfileConfigConstants.MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE,
MicroProfileConfigErrorCode.DEFAULT_VALUE_IS_WRONG_TYPE);

assertJavaDiagnostics(diagnosticsParams, utils, //
d1, d2, d3, d4, d5);
d1, d2, d3, d4, d5, d6, d7);
}

@Test
Expand Down
Loading