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

#365 allow null to be injected #388

Merged
merged 1 commit into from
Sep 26, 2018
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 @@ -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