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

Drop support for full YAML content in parent property names #1201

Merged
merged 1 commit into from
Jul 30, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -47,11 +45,8 @@ public class YamlConfigSource extends MapBackedConfigSource {
DUMPER = new Yaml(dumperOptions);
}

private final Set<String> propertyNames;

public YamlConfigSource(String name, Map<String, String> source, int ordinal) {
super(name, source, ordinal, false);
this.propertyNames = filterPropertyNames(source);
}

public YamlConfigSource(URL url) throws IOException {
Expand All @@ -77,11 +72,6 @@ public YamlConfigSource(String name, String source, int ordinal) {
this(name, stringToMap(source), ordinal);
}

@Override
public Set<String> getPropertyNames() {
return propertyNames;
}

@SuppressWarnings("unchecked")
private static Map<String, String> streamToMap(InputStream inputStream) throws IOException {
Assert.checkNotNullParam("inputStream", inputStream);
Expand Down Expand Up @@ -181,12 +171,6 @@ private static void flattenList(String key, List<Object> source, Map<String, Str
escapeCommas(sb, value, 1);
return sb.toString();
}).collect(Collectors.joining(",")));
} else {
// Mark keys for later removal
key = YamlConfigSource.class.getName() + ".filter." + key;
// This dumps the entire YAML in a parent property. It was added to support complex mappings, but it is not
// needed anymore with the indexed property support. We keep it for compatibility reasons.
target.put(key, DUMPER.dump(singletonMap(key.substring(key.lastIndexOf(".") + 1), source)));
}
}

Expand All @@ -203,19 +187,6 @@ private static void escapeCommas(StringBuilder b, String src, int escapeLevel) {
}
}

private static Set<String> filterPropertyNames(Map<String, String> source) {
final Set<String> filteredKeys = new HashSet<>();
for (final String key : new HashSet<>(source.keySet())) {
if (key.startsWith(YamlConfigSource.class.getName() + ".filter.")) {
String originalKey = key.substring(55);
source.put(originalKey, source.remove(key));
} else {
filteredKeys.add(key);
}
}
return filteredKeys;
}

/**
* Override some yaml constructors, so that the value written in the flatten result is more alike with the
* source. For instance, timestamps may be written in a completely different format which prevents converters to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,6 @@
import io.smallrye.config.SmallRyeConfigBuilder;

class YamlConfigSourceTest {
@Test
void flatten() {
String yaml = "admin:\n" +
" users:\n" +
" -\n" +
" email: \"[email protected]\"\n" +
" username: \"joe\"\n" +
" password: \"123456\"\n" +
" roles:\n" +
" - \"Moderator\"\n" +
" - \"Admin\"\n" +
" -\n" +
" email: \"[email protected]\"\n" +
" username: \"jack\"\n" +
" password: \"654321\"\n" +
" roles:\n" +
" - \"Moderator\"\n";

YamlConfigSource source = new YamlConfigSource("yaml", yaml);
String value = source.getValue("admin.users");
Users users = new UserConverter().convert(value);
assertEquals(2, users.getUsers().size());
assertEquals(users.users.get(0).getEmail(), "[email protected]");
assertEquals(users.users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList()));

assertEquals("[email protected]", source.getValue("admin.users[0].email"));
}

@Test
void profiles() {
String yaml = "---\n" +
Expand Down Expand Up @@ -149,37 +121,6 @@ void indentSpaces() {
assertEquals("smallrye", config.getRawValue("greeting.name"));
}

@Test
void config() {
String yaml = "admin:\n" +
" users:\n" +
" -\n" +
" email: \"[email protected]\"\n" +
" username: \"joe\"\n" +
" password: \"123456\"\n" +
" roles:\n" +
" - \"Moderator\"\n" +
" - \"Admin\"\n" +
" -\n" +
" email: \"[email protected]\"\n" +
" username: \"jack\"\n" +
" password: \"654321\"\n" +
" roles:\n" +
" - \"Moderator\"\n";

SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(new YamlConfigSource("yaml", yaml))
.withConverter(Users.class, 100, new UserConverter())
.build();

Users users = config.getValue("admin.users", Users.class);
assertEquals(2, users.getUsers().size());
assertEquals(users.users.get(0).getEmail(), "[email protected]");
assertEquals(users.users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList()));

assertEquals("[email protected]", config.getRawValue("admin.users[0].email"));
}

@Test
void propertyNames() {
String yaml = "quarkus:\n" +
Expand Down