Skip to content

Commit

Permalink
Fix enums as each property keys. Fixes #10944 (#10993)
Browse files Browse the repository at this point in the history
* Fix enums as each property keys. Fixes #10944
  • Loading branch information
graemerocher authored Jul 19, 2024
1 parent d247e9d commit 22aab3d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
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

0 comments on commit 22aab3d

Please sign in to comment.