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 enums as each property keys. Fixes #10944 #10993

Merged
merged 2 commits into from
Jul 19, 2024
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
@@ -0,0 +1,20 @@
package io.micronaut.inject.foreach.withenum;

import io.micronaut.context.annotation.EachProperty;
import io.micronaut.context.annotation.Parameter;
import io.micronaut.context.annotation.Requires;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

@EachProperty("enumconfig")
@Requires(property = "enumconfig")
public record EnumConfiguration(@Parameter MyEnum myEnum,
@NotEmpty List<@NotBlank String> cities) {

public enum MyEnum {
SUMMER,
WINTER
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.micronaut.inject.foreach.withenum

import io.micronaut.context.ApplicationContext
import io.micronaut.inject.qualifiers.Qualifiers
import spock.lang.Specification

class EnumEachPropertySpec extends Specification {

void "test each property with enum keys"() {
given:
def ctx = ApplicationContext.run(
"enumconfig.summer.cities":"barcelona,atlanta,sydney",
"enumconfig.winter.cities": "albertville,lillehammer,nagano"
)

when:
EnumConfiguration configuration = ctx.getBean(EnumConfiguration, Qualifiers.byName(EnumConfiguration.MyEnum.SUMMER.name()))

then:
configuration != null
configuration.myEnum() == EnumConfiguration.MyEnum.SUMMER


cleanup:
ctx.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,18 @@ private Map<String, Object> getParametersValues(BeanResolutionContext resolution
for (Argument<Object> argument : requiredArguments) {
String argumentName = argument.getName();
if (argument.isAnnotationPresent(Parameter.class)) {
Class<Object> type = (Class<Object>) argument.getWrapperType();
if (CharSequence.class.isAssignableFrom(type)) {
Class<?> type = argument.getWrapperType();
boolean isEnum = Enum.class.isAssignableFrom(type);
if (CharSequence.class.isAssignableFrom(type) || isEnum) {
String simpleName = configurationPath.simpleName();
if (simpleName != null) {
fulfilled.put(argumentName, simpleName);
Object value = isEnum ? context.getConversionService().convertRequired(simpleName, type) : simpleName;
fulfilled.put(argumentName, value);
} else {
String name = findName(resolutionContext.getCurrentQualifier());
if (name != null) {
fulfilled.put(argumentName, name);
Object value = isEnum ? context.getConversionService().convertRequired(name, type) : name;
fulfilled.put(argumentName, value);
}
}
} else if (Number.class.isAssignableFrom(type)) {
Expand All @@ -199,7 +202,7 @@ private Map<String, Object> getParametersValues(BeanResolutionContext resolution
if (argument.isProvider()) {
Argument<?> pt = argument.getFirstTypeVariable().orElse(null);
if (pt != null) {
type = (Class<Object>) pt.getType();
type = pt.getType();
}
}

Expand Down
Loading