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 process EachProperty configurations with abstract class for KSP #10523

Merged
Merged
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 @@ -48,7 +48,7 @@ public static String buildPropertyPath(ClassElement owningType, ClassElement dec
} else {
typePath = getRequiredTypePath(owningType);
}
return typePath + "." + propertyName;
return typePath + '.' + propertyName;
}

public static String getRequiredTypePath(ClassElement classElement) {
Expand All @@ -57,7 +57,7 @@ public static String getRequiredTypePath(ClassElement classElement) {

public static Optional<String> getTypePath(ClassElement classElement) {
if (!classElement.hasStereotype(ConfigurationReader.class)) {
return Optional.empty();
return Optional.of(StringUtils.EMPTY_STRING);
}
if (classElement.isTrue(ConfigurationReader.class, ConfigurationReader.PREFIX_CALCULATED)) {
return classElement.stringValue(ConfigurationReader.class, ConfigurationReader.PREFIX);
Expand All @@ -75,7 +75,7 @@ public static Optional<String> getTypePath(ClassElement classElement) {

private static String combinePaths(String p1, String p2) {
if (StringUtils.isNotEmpty(p1) && StringUtils.isNotEmpty(p2)) {
return p1 + "." + p2;
return p1 + '.' + p2;
}
if (StringUtils.isNotEmpty(p1)) {
return p1;
Expand All @@ -91,7 +91,7 @@ private static String getPath(AnnotationMetadata annotationMetadata) {
if (prefixOptional.isEmpty()) {
prefix = basePrefixOptional.get();
} else {
prefix = prefixOptional.map(p -> basePrefixOptional.get() + "." + p).orElse(null);
prefix = prefixOptional.map(p -> basePrefixOptional.get() + '.' + p).orElse(null);
}
} else {
prefix = prefixOptional.orElse(null);
Expand All @@ -100,7 +100,7 @@ private static String getPath(AnnotationMetadata annotationMetadata) {
return computeIterablePrefix(annotationMetadata, prefix);
}
if (prefix == null) {
return "";
return StringUtils.EMPTY_STRING;
}
return prefix;
}
Expand Down Expand Up @@ -128,7 +128,8 @@ private static String prependInners(ClassElement classElement, String path) {
while (classElement.isInner() && inner.isPresent()) {
ClassElement enclosingType = inner.get();
if (enclosingType.isTrue(ConfigurationReader.class, ConfigurationReader.PREFIX_CALCULATED)) {
String parentPrefix = enclosingType.stringValue(ConfigurationReader.class, ConfigurationReader.PREFIX).orElse("");
String parentPrefix = enclosingType.stringValue(ConfigurationReader.class, ConfigurationReader.PREFIX)
.orElse(StringUtils.EMPTY_STRING);
path = combinePaths(parentPrefix, path);
break;
} else {
Expand All @@ -149,7 +150,8 @@ private static String prependSuperclasses(ClassElement declaringType, String pat
while (optionalSuperType.isPresent()) {
ClassElement superType = optionalSuperType.get();
if (superType.isTrue(ConfigurationReader.class, ConfigurationReader.PREFIX_CALCULATED)) {
String parentPrefix = superType.stringValue(ConfigurationReader.class, ConfigurationReader.PREFIX).orElse("");
String parentPrefix = superType.stringValue(ConfigurationReader.class, ConfigurationReader.PREFIX)
.orElse(StringUtils.EMPTY_STRING);
path = combinePaths(parentPrefix, path);
break;
} else {
Expand Down Expand Up @@ -177,7 +179,10 @@ private static String prependInterfaces(ClassElement declaringType, String path)
}

private static ClassElement resolveSuperInterface(ClassElement declaringType) {
return declaringType.getInterfaces().stream().filter(tm -> tm.hasStereotype(ConfigurationReader.class)).findFirst().orElse(null);
return declaringType.getInterfaces().stream()
.filter(tm -> tm.hasStereotype(ConfigurationReader.class))
.findFirst()
.orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.micronaut.inject.configproperties.inheritance;

import org.jetbrains.annotations.NotNull;

public abstract class AbstractConfig {
@NotNull
private final String value;
@NotNull
private String notThing;

public AbstractConfig(@NotNull String value) {
this.value = value;
this.notThing = "def notThing";
}

@NotNull
public final String getValue() {
return this.value;
}

@NotNull
public final String getNotThing() {
return this.notThing;
}

public final void setNotThing(@NotNull String notThing) {
this.notThing = notThing;
}

@NotNull
public abstract String getThing();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.micronaut.inject.configproperties.inheritance;

import io.micronaut.context.annotation.ConfigurationInject;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@EachProperty("teams")
public class AbstractConfigImpl extends AbstractConfig {
@NotNull
private final String name;
@Nullable
private String childThing;
@NotNull
private String thing;

@ConfigurationInject
public AbstractConfigImpl(@Parameter("name") @NotNull String name, @NotNull String value) {
super(value);
this.name = name;
this.childThing = "def childThing";
this.thing = "thing";
}

@NotNull
public final String getName() {
return this.name;
}

@Nullable
public final String getChildThing() {
return this.childThing;
}

public final void setChildThing(@Nullable String childThing) {
this.childThing = childThing;
}

@Override
@NotNull
public String getThing() {
return this.thing;
}

public void setThing(@NotNull String thing) {
this.thing = thing;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,57 @@ class ConfigurationPropertiesInheritanceSpec extends Specification {
managers[0].is(teams[0].manager)
managers.size() == 1
}

void "test EachProperty with overridden abstract class method"() {
given:
ApplicationContext context = ApplicationContext.run([
'teams.one.value': 'My value',
'teams.one.not-thing': 'First not thing',
'teams.one.thing': 'This is the thing',
'teams.one.child-thing': 'My child thing',

'teams.two.value': 'My second value',
'teams.two.thing': 'This is the big thing',
'teams.two.not-thing': 'Second not thing',
])

when:
def teamOne = context.getBean(AbstractConfigImpl, Qualifiers.byName("one"))
def teamTwo = context.getBean(AbstractConfigImpl, Qualifiers.byName("two"))

then:
teamOne.name == 'one'
teamOne.value == 'My value'
teamOne.notThing == 'First not thing'
teamOne.thing == 'This is the thing'
teamOne.childThing == 'My child thing'

teamTwo.name == 'two'
teamTwo.value == 'My second value'
teamTwo.notThing == 'Second not thing'
teamTwo.thing == 'This is the big thing'
teamTwo.childThing == 'def childThing'
}

void "test EachProperty with overridden non-abstract class method"() {
given:
ApplicationContext context = ApplicationContext.run([
'teams.one.value': 'My value',
'teams.one.super-value': 'My super value',
'teams.one.super-value-with-override': 'My super overriden value',
'teams.one.thing': 'This is the thing',
'teams.one.child-thing': 'My child thing',
])

when:
def teamOne = context.getBean(NotAbstractConfigImpl, Qualifiers.byName("one"))

then:
teamOne.name == 'one'
teamOne.value == 'My value'
teamOne.superValue == 'My super value'
teamOne.superValueWithOverride == 'My super overriden value'
teamOne.thing == 'This is the thing'
teamOne.childThing == 'My child thing'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.micronaut.inject.configproperties.inheritance;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class NotAbstractConfig {
@NotNull
private final String value;
@Nullable
private String superValue;
@Nullable
private String superValueWithOverride;

public NotAbstractConfig(@NotNull String value) {
this.value = value;
this.superValue = "";
this.superValueWithOverride = "this is ";
}

@NotNull
public final String getValue() {
return this.value;
}

@Nullable
public final String getSuperValue() {
return this.superValue;
}

public final void setSuperValue(@Nullable String var1) {
this.superValue = var1;
}

@Nullable
public String getSuperValueWithOverride() {
return this.superValueWithOverride;
}

public void setSuperValueWithOverride(@Nullable String var1) {
this.superValueWithOverride = var1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.micronaut.inject.configproperties.inheritance;

import io.micronaut.context.annotation.ConfigurationInject;
import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@EachProperty("teams")
public class NotAbstractConfigImpl extends NotAbstractConfig {
@NotNull
private final String name;
@NotNull
private final String thing;
@Nullable
private String childThing;
@Nullable
private String superValueWithOverride;

@ConfigurationInject
public NotAbstractConfigImpl(@Parameter("name") @NotNull String name, @NotNull String value, @NotNull String thing) {
super(value);
this.name = name;
this.thing = thing;
this.childThing = "def childThing";
this.superValueWithOverride = "my defaultValue";
}

@NotNull
public final String getName() {
return this.name;
}

@NotNull
public final String getThing() {
return this.thing;
}

@Nullable
public final String getChildThing() {
return this.childThing;
}

public final void setChildThing(@Nullable String var1) {
this.childThing = var1;
}

@Override
@Nullable
public String getSuperValueWithOverride() {
return this.superValueWithOverride;
}

@Override
public void setSuperValueWithOverride(@Nullable String var1) {
this.superValueWithOverride = var1;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,57 @@ class ConfigurationPropertiesInheritanceSpec extends Specification {
managers.size() == 1
managers[0].is(teams[0].manager)
}

void "test EachProperty with overridden abstract class method"() {
given:
ApplicationContext context = ApplicationContext.run([
'teams.one.value': 'My value',
'teams.one.not-thing': 'First not thing',
'teams.one.thing': 'This is the thing',
'teams.one.child-thing': 'My child thing',

'teams.two.value': 'My second value',
'teams.two.thing': 'This is the big thing',
'teams.two.not-thing': 'Second not thing',
])

when:
def teamOne = context.getBean(AbstractConfigImpl, Qualifiers.byName("one"))
def teamTwo = context.getBean(AbstractConfigImpl, Qualifiers.byName("two"))

then:
teamOne.name == 'one'
teamOne.value == 'My value'
teamOne.notThing == 'First not thing'
teamOne.thing == 'This is the thing'
teamOne.childThing == 'My child thing'

teamTwo.name == 'two'
teamTwo.value == 'My second value'
teamTwo.notThing == 'Second not thing'
teamTwo.thing == 'This is the big thing'
teamTwo.childThing == 'def childThing'
}

void "test EachProperty with overridden non-abstract class method"() {
given:
ApplicationContext context = ApplicationContext.run([
'teams.one.value': 'My value',
'teams.one.super-value': 'My super value',
'teams.one.super-value-with-override': 'My super overriden value',
'teams.one.thing': 'This is the thing',
'teams.one.child-thing': 'My child thing',
])

when:
def teamOne = context.getBean(NotAbstractConfigImpl, Qualifiers.byName("one"))

then:
teamOne.name == 'one'
teamOne.value == 'My value'
teamOne.superValue == 'My super value'
teamOne.superValueWithOverride == 'My super overriden value'
teamOne.thing == 'This is the thing'
teamOne.childThing == 'My child thing'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.micronaut.kotlin.processing.beans.configproperties.inheritance

abstract class AbstractConfig(
val value: String = ""
) {
var notThing: String = "def notThing"
abstract val thing: String
}
Loading
Loading