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

[4.x] - MpConfig convertToHelidon exception fix #6834

Merged
merged 3 commits into from
May 26, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 37 additions & 22 deletions config/config-mp/src/main/java/io/helidon/config/mp/MpConfig.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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<ConfigSource> 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<ConfigSource> 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<String, String> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}