From 2a8ec4dd3f424a9ff273ebd8b3b4a484aab93328 Mon Sep 17 00:00:00 2001 From: Andrey Somov Date: Sun, 27 Aug 2023 12:31:35 +0400 Subject: [PATCH] Update SnakeYAML to version 2.2 --- polyglot-yaml/pom.xml | 2 +- .../maven/polyglot/yaml/ModelConstructor.java | 5 ++- .../maven/polyglot/yaml/ModelRepresenter.java | 37 +++++++++++-------- .../maven/polyglot/yaml/YamlModelReader.java | 6 ++- .../maven/polyglot/yaml/YamlModelWriter.java | 4 +- .../polyglot/yaml/CompactFormatTest.java | 6 ++- .../polyglot/yaml/ModelRepresenterTest.java | 2 +- 7 files changed, 36 insertions(+), 26 deletions(-) diff --git a/polyglot-yaml/pom.xml b/polyglot-yaml/pom.xml index 3f6065c8..8c220126 100644 --- a/polyglot-yaml/pom.xml +++ b/polyglot-yaml/pom.xml @@ -28,7 +28,7 @@ org.yaml snakeyaml - 1.17 + 2.2 diff --git a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelConstructor.java b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelConstructor.java index f8ea5311..536d9ccc 100644 --- a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelConstructor.java +++ b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelConstructor.java @@ -9,6 +9,7 @@ import org.apache.maven.model.*; import org.codehaus.plexus.util.xml.Xpp3Dom; +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.TypeDescription; import org.yaml.snakeyaml.constructor.Construct; import org.yaml.snakeyaml.constructor.Constructor; @@ -39,8 +40,8 @@ public final class ModelConstructor extends Constructor { */ private final Map, Construct> pomConstructors = new HashMap<>(); - public ModelConstructor() { - super(Model.class); + public ModelConstructor(LoaderOptions loadingConfig) { + super(Model.class, loadingConfig); yamlConstructors.put(XPP3DOM_TAG, new ConstructXpp3Dom()); yamlClassConstructors.put(NodeId.mapping, new MavenObjectConstruct()); diff --git a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelRepresenter.java b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelRepresenter.java index 4fe399da..43c2a8d9 100644 --- a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelRepresenter.java +++ b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/ModelRepresenter.java @@ -14,6 +14,7 @@ import org.apache.maven.model.Plugin; import org.codehaus.plexus.util.xml.Xpp3Dom; import org.yaml.snakeyaml.error.YAMLException; +import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.introspector.Property; import org.yaml.snakeyaml.nodes.Node; import org.yaml.snakeyaml.nodes.NodeTuple; @@ -34,7 +35,8 @@ * @since 0.7 */ class ModelRepresenter extends Representer { - public ModelRepresenter() { + public ModelRepresenter(DumperOptions options) { + super(options); this.representers.put(Xpp3Dom.class, new RepresentXpp3Dom()); Represent stringRepresenter = this.representers.get(String.class); this.representers.put(Boolean.class, stringRepresenter); @@ -96,7 +98,7 @@ private class RepresentXpp3Dom implements Represent { private static final String ATTRIBUTE_PREFIX = "attr/"; public Node representData(Object data) { - return representMapping(Tag.MAP, toMap((Xpp3Dom) data), null); + return representMapping(Tag.MAP, toMap((Xpp3Dom) data), DumperOptions.FlowStyle.AUTO); } private Map toMap(Xpp3Dom node) { @@ -215,20 +217,23 @@ private List toList(Xpp3Dom node, String childName) { * Change the default order. Important data goes first. */ @Override - protected Set getProperties(Class type) - throws IntrospectionException { - if (type.isAssignableFrom(Model.class)) { - return sortTypeWithOrder(type, ORDER_MODEL); - } else if (type.isAssignableFrom(Developer.class)) { - return sortTypeWithOrder(type, ORDER_DEVELOPER); - } else if (type.isAssignableFrom(Contributor.class)) { - return sortTypeWithOrder(type, ORDER_CONTRIBUTOR); - } else if (type.isAssignableFrom(Dependency.class)) { - return sortTypeWithOrder(type, ORDER_DEPENDENCY); - } else if (type.isAssignableFrom(Plugin.class)) { - return sortTypeWithOrder(type, ORDER_PLUGIN); - } else { - return super.getProperties(type); + protected Set getProperties(Class type) { + try { + if (type.isAssignableFrom(Model.class)) { + return sortTypeWithOrder(type, ORDER_MODEL); + } else if (type.isAssignableFrom(Developer.class)) { + return sortTypeWithOrder(type, ORDER_DEVELOPER); + } else if (type.isAssignableFrom(Contributor.class)) { + return sortTypeWithOrder(type, ORDER_CONTRIBUTOR); + } else if (type.isAssignableFrom(Dependency.class)) { + return sortTypeWithOrder(type, ORDER_DEPENDENCY); + } else if (type.isAssignableFrom(Plugin.class)) { + return sortTypeWithOrder(type, ORDER_PLUGIN); + } else { + return super.getProperties(type); + } + } catch (IntrospectionException e) { + throw new YAMLException(e); } } diff --git a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelReader.java b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelReader.java index f9fe967c..df564020 100644 --- a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelReader.java +++ b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelReader.java @@ -13,6 +13,7 @@ import org.codehaus.plexus.component.annotations.Component; import org.sonatype.maven.polyglot.io.ModelReaderSupport; import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.representer.Representer; @@ -33,8 +34,9 @@ public class YamlModelReader extends ModelReaderSupport { private final Yaml yaml; public YamlModelReader() { - ModelConstructor constructor = new ModelConstructor(); - yaml = new Yaml(constructor, new Representer(), new DumperOptions(), new ModelResolver()); + ModelConstructor constructor = new ModelConstructor(new LoaderOptions()); + DumperOptions options = new DumperOptions(); + yaml = new Yaml(constructor, new Representer(options), options, new ModelResolver()); } public Model read(Reader input, Map options) throws IOException, ModelParseException { diff --git a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelWriter.java b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelWriter.java index 91b7a71c..e6088920 100644 --- a/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelWriter.java +++ b/polyglot-yaml/src/main/java/org/sonatype/maven/polyglot/yaml/YamlModelWriter.java @@ -33,12 +33,12 @@ @Component(role = ModelWriter.class, hint = "yaml") public class YamlModelWriter extends ModelWriterSupport { public void write(Writer output, Map o, Model model) throws IOException { - //TODO improve SnakeYAML API (A. Somov) DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setIndent(2); dumperOptions.setWidth(80); + dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); Serializer serializer = new Serializer(new Emitter(output, dumperOptions), new ModelResolver(), dumperOptions, Tag.MAP); - Representer representer = new ModelRepresenter(); + Representer representer = new ModelRepresenter(dumperOptions); try { serializer.open(); Node node = representer.represent(model); diff --git a/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/CompactFormatTest.java b/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/CompactFormatTest.java index 9979eef4..feb6347c 100644 --- a/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/CompactFormatTest.java +++ b/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/CompactFormatTest.java @@ -98,11 +98,13 @@ public void testModelReader() throws Exception { @Test public void testColonInFlowContext() throws Exception { + // support for colon in the flow context is improved + // in SnakeYAML 2.0 try { getModel("dependencies-colon-issue.yaml"); - throw new UnsupportedOperationException("Colon in flow context should not be accepted."); } catch (Exception e) { - assertTrue(e.getMessage().contains("http://pyyaml.org/wiki/YAMLColonInFlowContext")); + String message = e.getMessage(); + assertTrue(message, message.contains("Unable to find property 'groupId:log4j' on class: org.apache.maven.model.Exclusion")); } } diff --git a/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/ModelRepresenterTest.java b/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/ModelRepresenterTest.java index 36aa36ac..a1b9ab85 100644 --- a/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/ModelRepresenterTest.java +++ b/polyglot-yaml/src/test/java/org/sonatype/maven/polyglot/yaml/ModelRepresenterTest.java @@ -22,7 +22,7 @@ public class ModelRepresenterTest { @Before public void setUp() throws Exception { - modelRepresenter = new ModelRepresenter(); + modelRepresenter = new ModelRepresenter(new DumperOptions()); } @Test