Skip to content

Commit

Permalink
Fix config lookup for list types (#2048)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar authored Oct 30, 2024
1 parent 4bd0e68 commit 148613d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 16 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@
<artifactId>smallrye-config</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-source-yaml</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
Expand Down
36 changes: 20 additions & 16 deletions core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -85,6 +86,9 @@ public static OpenApiConfig fromConfig(Config config) {

<R, T> T getConfigValue(String propertyName, Class<R> type, Function<R, T> converter, Supplier<T> defaultValue);

<R, T extends Collection<R>> T getConfigValues(String propertyName, Class<R> elementType, Function<List<R>, T> converter,
Supplier<T> defaultValue);

<R, T> Map<String, T> getConfigValueMap(String propertyNamePrefix, Class<R> type, Function<R, T> converter);

default <T> T getConfigValue(String propertyName, Class<T> type, Supplier<T> defaultValue) {
Expand All @@ -104,23 +108,23 @@ default boolean scanDisable() {
}

default Set<String> scanPackages() {
return getConfigValue(OASConfig.SCAN_PACKAGES, String[].class, this::toSet, Collections::emptySet);
return getConfigValues(OASConfig.SCAN_PACKAGES, String.class, this::toSet, Collections::emptySet);
}

default Set<String> scanClasses() {
return getConfigValue(OASConfig.SCAN_CLASSES, String[].class, this::toSet, Collections::emptySet);
return getConfigValues(OASConfig.SCAN_CLASSES, String.class, this::toSet, Collections::emptySet);
}

default Set<String> scanExcludePackages() {
return getConfigValue(OASConfig.SCAN_EXCLUDE_PACKAGES, String[].class, values -> {
return getConfigValues(OASConfig.SCAN_EXCLUDE_PACKAGES, String.class, values -> {
Set<String> valueSet = toSet(values);
valueSet.addAll(NEVER_SCAN_PACKAGES);
return Collections.unmodifiableSet(valueSet);
}, () -> NEVER_SCAN_PACKAGES);
}

default Set<String> scanExcludeClasses() {
return getConfigValue(OASConfig.SCAN_EXCLUDE_CLASSES, String[].class, values -> {
return getConfigValues(OASConfig.SCAN_EXCLUDE_CLASSES, String.class, values -> {
Set<String> valueSet = toSet(values);
valueSet.addAll(NEVER_SCAN_CLASSES);
return Collections.unmodifiableSet(valueSet);
Expand All @@ -132,15 +136,15 @@ default boolean scanBeanValidation() {
}

default List<String> servers() {
return getConfigValue(OASConfig.SERVERS, String[].class, this::toList, Collections::emptyList);
return getConfigValues(OASConfig.SERVERS, String.class, this::toList, Collections::emptyList);
}

default List<String> pathServers(String path) {
return getConfigValue(OASConfig.SERVERS_PATH_PREFIX + path, String[].class, this::toList, Collections::emptyList);
return getConfigValues(OASConfig.SERVERS_PATH_PREFIX + path, String.class, this::toList, Collections::emptyList);
}

default List<String> operationServers(String operationId) {
return getConfigValue(OASConfig.SERVERS_OPERATION_PREFIX + operationId, String[].class, this::toList,
return getConfigValues(OASConfig.SERVERS_OPERATION_PREFIX + operationId, String.class, this::toList,
Collections::emptyList);
}

Expand All @@ -151,8 +155,8 @@ default boolean scanDependenciesDisable() {
}

default Set<String> scanDependenciesJars() {
return getConfigValue(SmallRyeOASConfig.SMALLRYE_SCAN_DEPENDENCIES_JARS, String[].class, this::toSet,
() -> getConfigValue(SmallRyeOASConfig.SCAN_DEPENDENCIES_JARS, String[].class, this::toSet,
return getConfigValues(SmallRyeOASConfig.SMALLRYE_SCAN_DEPENDENCIES_JARS, String.class, this::toSet,
() -> getConfigValues(SmallRyeOASConfig.SCAN_DEPENDENCIES_JARS, String.class, this::toSet,
Collections::emptySet));
}

Expand Down Expand Up @@ -283,11 +287,11 @@ default void doAllowNakedPathParameter() {
}

default Set<String> getScanProfiles() {
return getConfigValue(SmallRyeOASConfig.SCAN_PROFILES, String[].class, this::toSet, Collections::emptySet);
return getConfigValues(SmallRyeOASConfig.SCAN_PROFILES, String.class, this::toSet, Collections::emptySet);
}

default Set<String> getScanExcludeProfiles() {
return getConfigValue(SmallRyeOASConfig.SCAN_EXCLUDE_PROFILES, String[].class, this::toSet, Collections::emptySet);
return getConfigValues(SmallRyeOASConfig.SCAN_EXCLUDE_PROFILES, String.class, this::toSet, Collections::emptySet);
}

default Map<String, String> getScanResourceClasses() {
Expand All @@ -309,18 +313,18 @@ default AutoInheritance getAutoInheritance() {
}

default Set<String> getScanCompositionExcludePackages() {
return getConfigValue(SmallRyeOASConfig.SCAN_COMPOSITION_EXCLUDE_PACKAGES, String[].class, this::toSet,
return getConfigValues(SmallRyeOASConfig.SCAN_COMPOSITION_EXCLUDE_PACKAGES, String.class, this::toSet,
() -> DEFAULT_COMPOSITION_EXCLUDE_PACKAGES);
}

default Set<String> toSet(String[] items) {
return Arrays.stream(items)
default Set<String> toSet(List<String> items) {
return items.stream()
.map(String::trim)
.collect(Collectors.toCollection(LinkedHashSet::new));
}

default List<String> toList(String[] items) {
return Arrays.stream(items)
default List<String> toList(List<String> items) {
return items.stream()
.map(String::trim)
.collect(Collectors.toCollection(ArrayList::new));
}
Expand Down
20 changes: 20 additions & 0 deletions core/src/main/java/io/smallrye/openapi/api/OpenApiConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -111,6 +112,25 @@ public <R, T> T getConfigValue(String propertyName, Class<R> type, Function<R, T
return value;
}

@SuppressWarnings("unchecked")
@Override
public <R, T extends Collection<R>> T getConfigValues(String propertyName,
Class<R> elementType,
Function<List<R>, T> converter,
Supplier<T> defaultValue) {
if (cache.containsKey(propertyName)) {
return (T) cache.get(propertyName);
}

T value = getConfig().getOptionalValues(propertyName, elementType)
.map(converter)
.orElseGet(defaultValue);

cache.put(propertyName, value);

return value;
}

@SuppressWarnings("unchecked")
@Override
public <R, T> Map<String, T> getConfigValueMap(String propertyNamePrefix, Class<R> type, Function<R, T> converter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.Set;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.openapi.OASConfig;
import org.junit.jupiter.api.Test;

import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.source.yaml.YamlConfigSource;

class OpenApiConfigImplTest {

private static final String TEST_PROPERTY = OASConfig.EXTENSIONS_PREFIX + "OpenApiConfigImplTest";
Expand Down Expand Up @@ -48,4 +53,14 @@ void testGetStringConfigValuePresent() {
}
}

@Test
void testLoadPropertyYaml() throws Exception {
var builder = new SmallRyeConfigBuilder();
builder.withSources(new YamlConfigSource(getClass().getResource("config.yaml")));
Config config = builder.build();

OpenApiConfig oaiConfig = OpenApiConfig.fromConfig(config);
assertEquals(Set.of("java.lang", "java.util"), oaiConfig.scanExcludePackages());
assertEquals(Set.of("my.pkg.Test", "my.pkg.Another"), oaiConfig.scanExcludeClasses());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -69,6 +70,12 @@ public <R, T> T getConfigValue(String propertyName, Class<R> type, Function<R, T
return defaultValue.get();
}

@Override
public <R, T extends Collection<R>> T getConfigValues(String propertyName, Class<R> elementType,
Function<List<R>, T> converter, Supplier<T> defaultValue) {
return defaultValue.get();
}

@Override
public <R, T> Map<String, T> getConfigValueMap(String propertyNamePrefix, Class<R> type, Function<R, T> converter) {
return Collections.emptyMap();
Expand Down
10 changes: 10 additions & 0 deletions core/src/test/resources/io/smallrye/openapi/api/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mp:
openapi:
scan:
exclude:
packages:
- java.lang
- java.util
classes:
- my.pkg.Test
- my.pkg.Another
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
<version>${version.io.smallrye.smallrye-config}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-source-yaml</artifactId>
<version>${version.io.smallrye.smallrye-config}</version>
<scope>test</scope>
</dependency>

<!-- Jakarta EE -->
<dependency>
Expand Down

0 comments on commit 148613d

Please sign in to comment.