diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java index 09a635bdd56..4cae9f4b0ec 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,9 @@ package io.helidon.config.mp; +import java.util.HashMap; import java.util.Iterator; +import java.util.Map; import io.helidon.config.ConfigSources; import io.helidon.config.OverrideSources; @@ -48,29 +50,42 @@ public static io.helidon.config.Config toHelidonConfig(Config mpConfig) { return (io.helidon.config.Config) mpConfig; } - // If the mpConfig is based on an SE config (such as when we use meta configuration)pom.xml - // we must reuse that se config instance - Iterator configSources = mpConfig.getConfigSources().iterator(); - ConfigSource first = configSources.hasNext() ? configSources.next() : null; - if (!configSources.hasNext() && first instanceof MpHelidonConfigSource) { - // we only have Helidon SE config as a source - let's just use it - return ((MpHelidonConfigSource) first).unwrap(); + if (mpConfig instanceof MpConfigImpl) { + + // If the mpConfig is based on an SE config (such as when we use meta configuration)pom.xml + // we must reuse that se config instance + Iterator configSources = mpConfig.getConfigSources().iterator(); + ConfigSource first = configSources.hasNext() ? configSources.next() : null; + if (!configSources.hasNext() && first instanceof MpHelidonConfigSource) { + // we only have Helidon SE config as a source - let's just use it + return ((MpHelidonConfigSource) first).unwrap(); + } + + // we use Helidon SE config to handle object mapping (and possible other mappers on classpath) + io.helidon.config.Config mapper = io.helidon.config.Config.builder() + .sources(ConfigSources.empty()) + .overrides(OverrideSources.empty()) + .disableEnvironmentVariablesSource() + .disableSystemPropertiesSource() + .disableParserServices() + .disableFilterServices() + .disableCaching() + .disableValueResolving() + .changesExecutor(command -> { + }) + .build(); + + return new SeConfig(mapper, mpConfig); } - // we use Helidon SE config to handle object mapping (and possible other mappers on classpath) - io.helidon.config.Config mapper = io.helidon.config.Config.builder() - .sources(ConfigSources.empty()) - .overrides(OverrideSources.empty()) - .disableEnvironmentVariablesSource() - .disableSystemPropertiesSource() - .disableParserServices() - .disableFilterServices() - .disableCaching() - .disableValueResolving() - .changesExecutor(command -> { - }) - .build(); + // Generic Properties convert + Map propertyMap = new HashMap<>(); + for (ConfigSource configSource : mpConfig.getConfigSources()) { + for (String propertyName : configSource.getPropertyNames()) { + propertyMap.putIfAbsent(propertyName, configSource.getValue(propertyName)); + } + } - return new SeConfig(mapper, mpConfig); + return io.helidon.config.Config.create(ConfigSources.create(propertyMap)); } } diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java b/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java new file mode 100644 index 00000000000..99c6ff41ddd --- /dev/null +++ b/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * 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 io.helidon.microprofile.config; + +import io.helidon.config.mp.MpConfig; +import io.helidon.microprofile.tests.junit5.AddConfig; +import io.helidon.microprofile.tests.junit5.HelidonTest; +import jakarta.inject.Inject; +import org.eclipse.microprofile.config.Config; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Test for correct handling of other implementations of {@link Config}. + * + * @see "https://github.com/helidon-io/helidon/issues/6668" + */ +@HelidonTest +@AddConfig(key = "key", value = "value") +public class MpConfigConvertTest { + + @Inject + private Config mpConfig; + + //No exceptions should occur. + @Test + void testConvertToHelidonConfig() { + io.helidon.config.Config helidonConfig = MpConfig.toHelidonConfig(mpConfig); + assertThat(helidonConfig.get("key").asString().get(), is("value")); + } +}