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

Register CDI Bean when ConfigMapping is marked as Unremovable #30960

Merged
merged 1 commit into from
Feb 8, 2023
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 @@ -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;
}
}