Skip to content

Commit

Permalink
#365 allow null to be injected
Browse files Browse the repository at this point in the history
Signed-off-by: Emily Jiang <[email protected]>
  • Loading branch information
Emily-Jiang committed Sep 23, 2018
1 parent 9ba1f5c commit 8f178b8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface ConfigProperty {
String UNCONFIGURED_VALUE="org.eclipse.microprofile.config.configproperty.unconfiguredvalue";
/**
* Provide a way to specify {@code null} value for a property.
* e.g. The following example is to set the default value of {@code my.port} to null if the property is not specified in any config sources.
* <pre>
* &#064;Inject
* &#064;ConfigProperty(name="my.port" defaultValue=ConfigProperty.NULL_VALUE)
* String value;
* </pre>
*/
String NULL_VALUE="org.eclipse.microprofile.config.configproperty.nullvalue";
/**
* The key of the config property used to look up the configuration value.
* If it is not specified, it will be derived automatically as {@code <class_name>.<injection_point_name>},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
* @author <a href="mailto:[email protected]">Emily Jiang</a>
*
*/
@org.osgi.annotation.versioning.Version("1.0.1")
@org.osgi.annotation.versioning.Version("1.1.0")
package org.eclipse.microprofile.config.inject;

5 changes: 5 additions & 0 deletions spec/src/main/asciidoc/configexamples.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class InjectedConfigUsageSample {
@Inject
@ConfigProperty(name="myprj.some.dynamic.timeout", defaultValue="100")
private javax.inject.Provider<Long> timeout;
//Injects the value of the property myprj.name if specified in any of the configures, otherwise null will be injected.
@Inject
@ConfigProperty(name="myprj.name" defaultValue=ConfigProperty.NULL_VALUE)
String name;
//The following code injects an Array, List or Set for the `myPets` property,
//where its value is a comma separated value ( myPets=dog,cat,dog\\,cat)
@Inject @ConfigProperty(name="myPets") private String[] myArrayPets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import junit.framework.Assert;

/**
* Test cases for CDI-based API that test retrieving values from the configuration.
* The tests depend only on CDI 1.2.
Expand Down Expand Up @@ -95,6 +97,8 @@ public void canInjectSimpleValuesWhenDefined() {
assertThat(bean.characterProperty, is(equalTo(Character.valueOf('c'))));

assertThat(bean.doublePropertyWithDefaultValue, is(closeTo(3.1415, 0.1)));
Assert.assertNull("The property my.not.configured.nullable.property should be null",
ConfigProvider.getConfig().getOptionalValue("my.not.configured.nullable.property", String.class).orElse(null));
}

/*
Expand Down Expand Up @@ -126,6 +130,8 @@ public void injectedValuesAreEqualToProgrammaticValues() {
assertThat(bean.doublePropertyWithDefaultValue, is(closeTo(
ConfigProvider.getConfig().getOptionalValue("my.not.configured.double.property", Double.class)
.orElse(3.1415), 0.1)));
Assert.assertNull("The injected field nullableConfigValue is null", bean.nullableConfigValue);

}

@Test
Expand Down Expand Up @@ -251,6 +257,10 @@ public static class SimpleValuesBean {
@Inject
@ConfigProperty(name="my.not.configured.double.property", defaultValue = "3.1415")
private Double doublePropertyWithDefaultValue;
// the property is not configured in any ConfigSoources, so null will be used to set the filed
@Inject
@ConfigProperty(name="my.not.configured.nullable.property", defaultValue = ConfigProperty.NULL_VALUE)
private String nullableConfigValue;

}

Expand Down

0 comments on commit 8f178b8

Please sign in to comment.