Skip to content

Commit

Permalink
Merge pull request #30960 from radcortez/fix-29631
Browse files Browse the repository at this point in the history
Register CDI Bean when ConfigMapping is marked as Unremovable
  • Loading branch information
gsmet authored Feb 8, 2023
2 parents 6782256 + f153119 commit af74ebb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,17 @@ void registerConfigMappingsBean(
return;
}

Map<Type, ConfigClassBuildItem> configMappingTypes = configClassesToTypesMap(configClasses, MAPPING);
Set<ConfigClassBuildItem> configMappings = new HashSet<>();

// Add beans for all unremovable mappings
for (ConfigClassBuildItem configClass : configClasses) {
if (configClass.getConfigClass().isAnnotationPresent(Unremovable.class)) {
configMappings.add(configClass);
}
}

// Add beans for all injection points
Map<Type, ConfigClassBuildItem> configMappingTypes = configClassesToTypesMap(configClasses, MAPPING);
for (InjectionPointInfo injectionPoint : beanRegistration.getInjectionPoints()) {
Type type = Type.create(injectionPoint.getRequiredType().name(), Type.Kind.CLASS);
ConfigClassBuildItem configClass = configMappingTypes.get(type);
Expand All @@ -309,6 +318,7 @@ void registerConfigMappingsBean(
}
}

// Generate the mappings beans
for (ConfigClassBuildItem configClass : configMappings) {
BeanConfigurator<Object> bean = beanRegistration.getContext()
.configure(configClass.getConfigClass())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import jakarta.enterprise.inject.spi.CDI;
import jakarta.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand All @@ -29,6 +31,12 @@ public class UnremovedConfigMappingTest {
void unremoved() {
UnremovedConfigMapping mapping = config.getConfigMapping(UnremovedConfigMapping.class);
assertEquals("1234", mapping.prop());

mapping = CDI.current().select(UnremovedConfigMapping.class).get();
assertEquals("1234", mapping.prop());

UnremovedConfigProperties properties = CDI.current().select(UnremovedConfigProperties.class).get();
assertEquals("1234", properties.prop);
}

@Test
Expand All @@ -54,4 +62,10 @@ public interface ExtendsBase extends Base {
@WithDefault("default")
String myProp();
}

@Unremovable
@ConfigProperties(prefix = "mapping")
public static class UnremovedConfigProperties {
String prop;
}
}

0 comments on commit af74ebb

Please sign in to comment.