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: remove diagnostic of missing property for Optional fields #1418

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 @@ -35,4 +35,16 @@ public class DefaultValueResource {

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

@ConfigProperty(name = "greeting.optional") // this optional properties are not set
java.util.Optional<String> optional;

@ConfigProperty(name = "greeting.optional.int")
java.util.OptionalInt optional;

@ConfigProperty(name = "greeting.optional.long")
java.util.OptionalLong optional;

@ConfigProperty(name = "greeting.optional.double")
java.util.OptionalDouble optional;
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void visitAnnotation(PsiAnnotation annotation) {
if (AnnotationUtils.isMatchAnnotation(annotation, CONFIG_PROPERTY_ANNOTATION) && parent != null) {
PsiAnnotationMemberValue defaultValueExpr = getAnnotationMemberValueExpression(annotation, MicroProfileConfigConstants.CONFIG_PROPERTY_ANNOTATION_DEFAULT_VALUE);
validatePropertyDefaultValue(annotation, defaultValueExpr, parent);
validatePropertyHasValue(annotation, defaultValueExpr);
validatePropertyHasValue(annotation, defaultValueExpr, parent);
}

}
Expand Down Expand Up @@ -162,7 +162,7 @@ private void validatePropertyDefaultValue(PsiAnnotation annotation, PsiAnnotatio
* @param defaultValueExpr the default value expression, or null if no default
* value is defined
*/
private void validatePropertyHasValue(PsiAnnotation annotation, PsiAnnotationMemberValue defaultValueExpr) {
private void validatePropertyHasValue(PsiAnnotation annotation, PsiAnnotationMemberValue defaultValueExpr, PsiField parent) {
String name = null;
PsiAnnotationMemberValue nameExpression = getAnnotationMemberValueExpression(annotation,
CONFIG_PROPERTY_ANNOTATION_NAME);
Expand All @@ -178,7 +178,7 @@ private void validatePropertyHasValue(PsiAnnotation annotation, PsiAnnotationMem
String message = MessageFormat.format(EMPTY_KEY_ERROR_MESSAGE, CONFIG_PROPERTY_ANNOTATION_NAME);
Diagnostic d = super.addDiagnostic(message, MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, nameExpression,
MicroProfileConfigErrorCode.EMPTY_KEY, DiagnosticSeverity.Error);
} else if (!hasDefaultValue && !doesPropertyHaveValue(name, getContext()) && !isPropertyIgnored(name)) {
} else if (!hasDefaultValue && !doesPropertyHaveValue(name, getContext()) && !isPropertyIgnored(name) && !isOptionalType(parent.getType())) {
String message = MessageFormat.format(NO_VALUE_ERROR_MESSAGE, name);
Diagnostic d = super.addDiagnostic(message, MICRO_PROFILE_CONFIG_DIAGNOSTIC_SOURCE, nameExpression,
MicroProfileConfigErrorCode.NO_VALUE_ASSIGNED_TO_PROPERTY, DiagnosticSeverity.Warning);
Expand Down Expand Up @@ -282,6 +282,11 @@ private static boolean isAssignable(String typeFqn, String value, Module javaPro
}
}

private boolean isOptionalType(PsiType fieldBinding) {
// Optional containers: java.util.Optional, java.util.OptionalInt, java.util.OptionalLong, and java.util.OptionalDouble
return TypeConversionUtil.erasure(fieldBinding).getCanonicalText().startsWith("java.util.Optional");
}

private static boolean doesPropertyHaveValue(String property, JavaDiagnosticsContext context) {
Module javaProject = context.getJavaProject();
PsiMicroProfileProject mpProject = PsiMicroProfileProjectManager.getInstance(javaProject.getProject())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void testConfigItemIntBoolDefaultValueTest() throws Exception {
assertProperties(infoFromClasspath,
259 + 31 /* properties from Java sources with ConfigProperty */ + //
7 /* static properties from microprofile-context-propagation-api */ +
1 /* static property from microprofile config_ordinal */,
1 /* static property from microprofile config_ordinal */ +
4 /* optional properties */,

// GreetingConstructorResource(
// @ConfigProperty(name = "greeting.constructor.message") String message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void testConfigQuickstartFromClasspath() throws Exception {
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 */,
1 /* static property from microprofile config_ordinal */ +
4 /* optional properties */,

// io.quarkus.deployment.ApplicationConfig
p("quarkus-core", "quarkus.application.name", "java.util.Optional<java.lang.String>",
Expand Down Expand Up @@ -120,7 +121,8 @@ public void testConfigQuickstartFromJavaSources() throws Exception {
MicroProfileProjectInfo infoFromJavaSources = PropertiesManager.getInstance().getMicroProfileProjectInfo(module, MicroProfilePropertiesScope.ONLY_SOURCES, ClasspathKind.SRC, PsiUtilsLSImpl.getInstance(myProject), DocumentFormat.PlainText, new EmptyProgressIndicator());

assertProperties(infoFromJavaSources, 31 /* properties from Java sources with ConfigProperty */ + //
2 /* properties from Java sources with ConfigRoot */,
2 /* properties from Java sources with ConfigRoot */ +
4 /* optional properties */,

// GreetingResource
// @ConfigProperty(name = "greeting.message")
Expand Down
Loading