Skip to content

Commit

Permalink
#169 - Uplift Snakeyaml to v2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
gazbert committed Nov 12, 2024
1 parent 49366f6 commit 6c5d394
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ ext.libraries = [

jakarta_xml_api : dependencies.create("jakarta.xml.bind:jakarta.xml.bind-api:4.0.2"),

snake_yaml : dependencies.create("org.yaml:snakeyaml:1.33"),
snake_yaml : dependencies.create("org.yaml:snakeyaml:2.2"),

springdoc_openapi_ui : dependencies.create("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.6.0"),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.introspector.BeanAccess;
Expand All @@ -52,7 +53,7 @@
@Log4j2
public class ConfigurationManager {

private static final String YAML_HEADER = "---" + System.getProperty("line.separator");
private static final String YAML_HEADER = "---" + System.lineSeparator();

/** Creates the Configuration Manager. */
public ConfigurationManager() {
Expand All @@ -72,7 +73,9 @@ public synchronized <T> T loadConfig(final Class<T> configClass, String yamlConf
log.info("Loading configuration for [" + configClass + "] from: " + yamlConfigFile + " ...");

try (final FileInputStream fileInputStream = new FileInputStream(yamlConfigFile)) {
final Yaml yaml = new Yaml(new Constructor(configClass));

final LoaderOptions options = new LoaderOptions();
final Yaml yaml = new Yaml(new Constructor(configClass, options));
final T requestedConfig = yaml.load(fileInputStream);

log.info("Loaded and set configuration for [" + configClass + "] successfully!");
Expand Down Expand Up @@ -108,10 +111,11 @@ public synchronized <T> void saveConfig(Class<T> configClass, T config, String y
new PrintWriter(fileOutputStream, true, StandardCharsets.UTF_8)) {

// Skip null fields and order the YAML fields
final Representer representer = new SkipNullFieldRepresenter();
final DumperOptions options = new DumperOptions();
final Representer representer = new SkipNullFieldRepresenter(options);
representer.setPropertyUtils(new ReversedPropertyUtils());

final Yaml yaml = new Yaml(representer);

final StringBuilder sb = new StringBuilder(YAML_HEADER);
sb.append(yaml.dumpAs(config, Tag.MAP, DumperOptions.FlowStyle.BLOCK));

Expand All @@ -133,6 +137,11 @@ public synchronized <T> void saveConfig(Class<T> configClass, T config, String y

/** Stops null fields from getting written out to YAML. */
private static class SkipNullFieldRepresenter extends Representer {

SkipNullFieldRepresenter(DumperOptions options) {
super(options);
}

@Override
protected NodeTuple representJavaBeanProperty(
Object javaBean, Property property, Object propertyValue, Tag customTag) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
Expand Down

0 comments on commit 6c5d394

Please sign in to comment.