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

Update SnakeYAML to version 2.2 #268

Merged
merged 1 commit into from
Aug 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion polyglot-yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.17</version>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,8 +40,8 @@ public final class ModelConstructor extends Constructor {
*/
private final Map<Class<?>, 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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<String, Object> toMap(Xpp3Dom node) {
Expand Down Expand Up @@ -215,20 +217,23 @@ private List<Object> toList(Xpp3Dom node, String childName) {
* Change the default order. Important data goes first.
*/
@Override
protected Set<Property> getProperties(Class<? extends Object> 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<Property> getProperties(Class<? extends Object> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, ?> options) throws IOException, ModelParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
@Component(role = ModelWriter.class, hint = "yaml")
public class YamlModelWriter extends ModelWriterSupport {
public void write(Writer output, Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ModelRepresenterTest {

@Before
public void setUp() throws Exception {
modelRepresenter = new ModelRepresenter();
modelRepresenter = new ModelRepresenter(new DumperOptions());
}

@Test
Expand Down
Loading