From 8f178b8e755af4b05b320439fb91fda4df85fd18 Mon Sep 17 00:00:00 2001 From: Emily Jiang Date: Sun, 23 Sep 2018 23:05:24 +0100 Subject: [PATCH] #365 allow null to be injected Signed-off-by: Emily Jiang --- .../microprofile/config/inject/ConfigProperty.java | 10 ++++++++++ .../microprofile/config/inject/package-info.java | 2 +- spec/src/main/asciidoc/configexamples.asciidoc | 5 +++++ .../microprofile/config/tck/CDIPlainInjectionTest.java | 10 ++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/org/eclipse/microprofile/config/inject/ConfigProperty.java b/api/src/main/java/org/eclipse/microprofile/config/inject/ConfigProperty.java index 2c156731..70143a3d 100644 --- a/api/src/main/java/org/eclipse/microprofile/config/inject/ConfigProperty.java +++ b/api/src/main/java/org/eclipse/microprofile/config/inject/ConfigProperty.java @@ -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. + *
+     * @Inject
+     * @ConfigProperty(name="my.port" defaultValue=ConfigProperty.NULL_VALUE)
+     * String value;
+     * 
+ */ + 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 .}, diff --git a/api/src/main/java/org/eclipse/microprofile/config/inject/package-info.java b/api/src/main/java/org/eclipse/microprofile/config/inject/package-info.java index 5dce0641..098ca201 100644 --- a/api/src/main/java/org/eclipse/microprofile/config/inject/package-info.java +++ b/api/src/main/java/org/eclipse/microprofile/config/inject/package-info.java @@ -44,6 +44,6 @@ * @author Emily Jiang * */ -@org.osgi.annotation.versioning.Version("1.0.1") +@org.osgi.annotation.versioning.Version("1.1.0") package org.eclipse.microprofile.config.inject; diff --git a/spec/src/main/asciidoc/configexamples.asciidoc b/spec/src/main/asciidoc/configexamples.asciidoc index 67944fe5..99ddf335 100644 --- a/spec/src/main/asciidoc/configexamples.asciidoc +++ b/spec/src/main/asciidoc/configexamples.asciidoc @@ -91,6 +91,11 @@ public class InjectedConfigUsageSample { @Inject @ConfigProperty(name="myprj.some.dynamic.timeout", defaultValue="100") private javax.inject.Provider 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; diff --git a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java index 3cdb7222..bdd89bfb 100644 --- a/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java +++ b/tck/src/main/java/org/eclipse/microprofile/config/tck/CDIPlainInjectionTest.java @@ -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. @@ -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)); } /* @@ -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 @@ -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; }