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 check for primitive return types #96

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ The individual methods may have arbitrary classes as return types, in particular

**Note**: Primitives are not supported as return types as they cannot be null and therefore cannot express a missing configuration value.

> [!CAUTION]
> Primitives are not supported as return types as they cannot be null and therefore cannot express a missing configuration value.
> If you use Baigan with Kotlin, it means you need to use nullable primitive types, e.g. `Int?` instead of `Int`.

The above example code enables the application to inject _ExpressFeature_ spring bean into any other Spring bean:

```Java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ public class BaiganConfigClasses {
public BaiganConfigClasses() {}

public void setConfigTypesByKey(Map<String, Type> configTypesByKey) {
configTypesByKey.forEach((key, value) -> {
if (value.getClass().isPrimitive()) {
throw new IllegalArgumentException("Config " + key + " has an illegal return type " + value + ". Primitives are not supported as return type.");
}
});
this.configTypesByKey = configTypesByKey;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,22 @@ private void createAndRegisterBeanDefinitions(final Set<String> packages,
Map<String, Type> configTypesByKey = baiganConfigClasses.stream().flatMap(clazz ->
Arrays.stream(clazz.getMethods()).map(method -> new ConfigType(createKey(clazz, method), method.getGenericReturnType()))
).collect(toMap(c -> c.key, c -> c.type));
assertNoPrimitiveTypes(configTypesByKey);
lukasniemeier-zalando marked this conversation as resolved.
Show resolved Hide resolved
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(BaiganConfigClasses.class);
beanDefinition.getPropertyValues().add("configTypesByKey", configTypesByKey);
registry.registerBeanDefinition("baiganConfigClasses", beanDefinition);
}

private void assertNoPrimitiveTypes(final Map<String, Type> configTypesByKey) {
configTypesByKey.forEach((key, value) -> {
if (value instanceof Class && ((Class<?>)value).isPrimitive()) {
throw new IllegalArgumentException("Config " + key + " has an illegal return type " + value
+ ". Primitives are not supported as return type.");
}
});
}

private Class<?> registerAsBean(final BeanDefinitionRegistry registry, final GenericBeanDefinition genericDefinition) {
try {
final Class<?> interfaceToImplement = genericDefinition.resolveBeanClass(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,17 @@ public void whenNothingAnnotatedWithConfigurationServiceScan_shouldThrowExceptio
when(metaData.getAnnotationAttributes(ConfigurationServiceScan.class.getName())).thenReturn(ImmutableMap.of());
assertThrows(IllegalArgumentException.class, () -> registrar.registerBeanDefinitions(metaData, registry));
}

@Test
public void whenConfigurationContainsPrimitiveTypes_shouldThrowException() {
when(metaData.getAnnotationAttributes(ConfigurationServiceScan.class.getName())).thenReturn(
ImmutableMap.of(
"value", new String[]{"org.zalando.baigan.context.packaged"},
"basePackages", new String[]{},
"basePackageClasses", new Class[]{}
)
);

assertThrows(IllegalArgumentException.class, () -> registrar.registerBeanDefinitions(metaData, registry));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.zalando.baigan.context.packaged;

import org.zalando.baigan.annotation.BaiganConfig;

@BaiganConfig
public interface PrimitiveTypeConfig {
boolean enabled();
}