Skip to content

Commit

Permalink
Ignore interface/class without default constructs fields in SB config
Browse files Browse the repository at this point in the history
This should behave similarly that when the setter is private. In this case, the field is ignored and we write a warning saying that a field is going to be ignored.

Fix #30896

(cherry picked from commit 999289a)
  • Loading branch information
Sgitario authored and gsmet committed Feb 7, 2023
1 parent be49b54 commit 945459a
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,7 @@ private ResultHandle populateConfigObject(ClassLoader classLoader, ClassInfo con
break;
}
}
} else {
if (!fieldTypeClassInfo.hasNoArgsConstructor()) {
throw new IllegalArgumentException(
"Nested configuration class '" + fieldTypeClassInfo
+ "' must contain a no-args constructor ");
}

} else if (fieldTypeClassInfo.hasNoArgsConstructor()) {
if (!Modifier.isPublic(fieldTypeClassInfo.flags())) {
throw new IllegalArgumentException(
"Nested configuration class '" + fieldTypeClassInfo + "' must be public ");
Expand All @@ -325,6 +319,10 @@ private ResultHandle populateConfigObject(ClassLoader classLoader, ClassInfo con
getFullConfigName(prefixStr, namingStrategy, field), namingStrategy, failOnMismatchingMember,
null, methodCreator);
createWriteValue(methodCreator, configObject, field, setter, useFieldAccess, nestedConfigObject);
} else {
LOGGER.warn("Nested configuration class '" + fieldTypeClassInfo
+ "' declared in '" + currentClassInHierarchy.name() + "." + field.name() + "' is either an "
+ "interface or does not have a non-args constructor, so this field will not be initialized");
}
} else {
String fullConfigName = getFullConfigName(prefixStr, namingStrategy, field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public final class ClassProperties {

private AnotherClass anotherClass;

private Interface anInterface;

public String getValue() {
return value;
}
Expand All @@ -24,4 +26,12 @@ public AnotherClass getAnotherClass() {
public void setAnotherClass(AnotherClass anotherClass) {
this.anotherClass = anotherClass;
}

public Interface getAnInterface() {
return anInterface;
}

public void setAnInterface(Interface anInterface) {
this.anInterface = anInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public String getValue() {
public boolean isAnotherClassValue() {
return properties.getAnotherClass().isValue();
}

@Path("/interface")
@GET
public boolean getUrlFromClassWithInterface() {
return properties.getAnInterface() == null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.quarkus.it.spring.boot;

public interface Interface {
String getUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ cl.value=class-value
cl.another-class.value=true
bean.value=1
bean.inner-class.value=inner-class-value
interface.value=interface-value
interface.value=interface-value
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ void shouldHaveAnotherClassValue() {
.then()
.body(is(equalTo("true")));
}

@Test
void shouldInterfaceNotBePopulated() {
when().get("/class/interface")
.then()
.body(is(equalTo("true")));
}
}

0 comments on commit 945459a

Please sign in to comment.