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

add support for Map injection #750

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
9 changes: 7 additions & 2 deletions spec/src/main/asciidoc/configexamples.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public class InjectedConfigUsageSample {
@ConfigProperty(name="myprj.some.supplier.timeout", defaultValue="100")
private java.util.function.Supplier<Long> timeout;

//Injects a Map to resolve multiple configuration parameters under the same configuration key.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the Map injection, what types for Key, Value do you propose to support? We need to clarify that.

@Inject
@ConfigProperty(name = "myprj.some.reasons", defaultValue = "200=OK;201=Created")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The specification must define how the map properties are parsed (both on a source and in the defaults).

private java.util.Map<Integer, String> reasons;
Copy link
Member

@otaviojava otaviojava Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private java.util.Map<Integer, String> reasons;
private java.util.Map<String, String> reasons;

Once we're talking about configuration, I believe the key must be a String

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree to narrow down to Map<String, String>. Otherwise, it will be overwhelming with the tests and it might have more issues to deal with regarding conversion errors.


//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 Expand Up @@ -371,8 +376,8 @@ The table below defines the conversion rules, including some special edge case s
|===

=== Remove config properties
Sometimes, there is a need to remove a property. This can be done by setting an empty value or a value causing the corresponding converter returning `null` in a config source.
When injecting a property that has been deleted, `DeploymentException` will be thrown unless the return type is `Optional`.
Sometimes, there is a need to remove a property. This can be done by setting an empty value or a value causing the corresponding converter returning `null` in a config source.
When injecting a property that has been deleted, `DeploymentException` will be thrown unless the return type is `Optional`.

=== Aggregate related properties into a CDI bean

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.eclipse.microprofile.config.tck;

import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.config.tck.converters.Pizza;

import java.net.URI;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Dependent
public class MapConverterBean {

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.string")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we call the programmatic API? What happens?

private Map<String, String> myStringStringMap;

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.integer.string")
private Map<Integer, String> myIntegerStringMap;

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.string.integer")
private Map<String, Integer> myStringIntegerMap;

@Inject
@ConfigProperty(name = "tck.config.test.javaconfig.converter.map.enum.enum")
private Map<EnumKey, EnumValue> myEnumEnumMap;

public Map<String, String> getMyStringStringMap() {
return myStringStringMap;
}

public Map<Integer, String> getMyIntegerStringMap() {
return myIntegerStringMap;
}

public Map<String, Integer> getMyStringIntegerMap() {
return myStringIntegerMap;
}

public Map<EnumKey, EnumValue> getMyEnumEnumMap() {
return myEnumEnumMap;
}

public enum EnumKey {
key1,
key2;
}

public enum EnumValue {
enum1,
enum2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2017, 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.eclipse.microprofile.config.tck;

import jakarta.inject.Inject;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.Converter;
import org.eclipse.microprofile.config.tck.MapConverterBean.EnumKey;
import org.eclipse.microprofile.config.tck.MapConverterBean.EnumValue;
import org.eclipse.microprofile.config.tck.converters.Pizza;
import org.eclipse.microprofile.config.tck.converters.PizzaConverter;
import org.eclipse.microprofile.config.tck.util.AdditionalAssertions;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.eclipse.microprofile.config.tck.base.AbstractTest.addFile;
import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLArrayEquals;
import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLListEquals;
import static org.eclipse.microprofile.config.tck.util.AdditionalAssertions.assertURLSetEquals;

/**
* Test the implicit converter handling.
**/
public class MapConverterTest extends Arquillian {

@Deployment
public static WebArchive deploy() {
JavaArchive testJar = ShrinkWrap
.create(JavaArchive.class, "mapConverterTest.jar")
.addPackage(PizzaConverter.class.getPackage())
.addClasses(MapConverterTest.class, MapConverterBean.class, AdditionalAssertions.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.addAsServiceProvider(Converter.class, PizzaConverter.class)
.as(JavaArchive.class);
addFile(testJar, "META-INF/microprofile-config.properties");
WebArchive war = ShrinkWrap
.create(WebArchive.class, "mapConverterTest.war")
.addAsLibrary(testJar);
return war;
}

@Inject
private MapConverterBean converterBean;

/////////////////////////////////// Test Map//////////////////////////


@Test
public void testStringStringMapInjection() {
Assert.assertEquals(converterBean.getMyStringStringMap().size(), 2);
Assert.assertEquals(converterBean.getMyStringStringMap().get("key1"), "string.string.value1");
Assert.assertEquals(converterBean.getMyStringStringMap().get("key2"), "string.string.value2");
}

@Test
public void testStringIntegerMapInjection() {
Assert.assertEquals(converterBean.getMyStringIntegerMap().size(), 2);
Assert.assertEquals(converterBean.getMyStringIntegerMap().get("key1"), Integer.valueOf(100));
Assert.assertEquals(converterBean.getMyStringIntegerMap().get("key2"), Integer.valueOf(200));
}

@Test
public void testIntegerStringMapInjection() {
Assert.assertEquals(converterBean.getMyIntegerStringMap().size(), 2);
Assert.assertEquals(converterBean.getMyIntegerStringMap().get(100), "integer.string.value1");
Assert.assertEquals(converterBean.getMyIntegerStringMap().get(200), "integer.string.value2");
}

@Test
public void testEnumEnumMapInjection() {
Assert.assertEquals(converterBean.getMyEnumEnumMap().size(), 2);
Assert.assertEquals(converterBean.getMyEnumEnumMap().get(EnumKey.key1), EnumValue.enum1);
Assert.assertEquals(converterBean.getMyEnumEnumMap().get(EnumKey.key2), EnumValue.enum2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,12 @@ tck.config.test.javaconfig.converter.urlvalues=http://microprofile.io,http://ope
tck.config.test.javaconfig.converter.class=org.eclipse.microprofile.config.tck.ClassConverterTest
tck.config.test.javaconfig.converter.class.array=org.eclipse.microprofile.config.tck.ClassConverterTest,java.lang.String


tck.config.test.javaconfig.converter.map.string.string.key1=string.string.value1
tck.config.test.javaconfig.converter.map.string.string.key2=string.string.value2
tck.config.test.javaconfig.converter.map.integer.string.100=integer.string.value1
tck.config.test.javaconfig.converter.map.integer.string.200=integer.string.value2
tck.config.test.javaconfig.converter.map.string.integer.key1=100
tck.config.test.javaconfig.converter.map.string.integer.key2=200
tck.config.test.javaconfig.converter.map.enum.enum.key1=value1
tck.config.test.javaconfig.converter.map.enum.enum.key2=value2