Detected tag for scalars in inconsistent

Issue #511 invalid
Fabio Silva Carvalho created an issue

Problem definition

When evaluating values right before serializing them, the detectedTag resolution seems to be inconsistent between Strings and integers. Consider the example below please.

Example

A Map object has one entry whose key is ports and the value is a String list with “8000“, “10000:9000“ and “10600:23“. When dumping that with SnakeYaml, you would expect the entries in the String list to be represented consistently. But that is not what happens, as seen below.

Expected output

ports:
- '8000'
- '10000:9000'
- '10600:23'

Actual output

ports:
- '8000'
- 10000:9000
- '10600:23'

Notice the second item in the list is not surrounded by single quotes, like the others.

The unit test below exposes this issue.

package com.paypal.evergreen.docker.pkg;

import org.testng.Assert;
import org.testng.annotations.Test;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SnakeYamlIssueTest {

    @Test
    public void test() {
        Map<String, Object> value = new HashMap<>();
        value.put("ports", Arrays.asList("8000", "10000:9000", "10600:23"));

        DumperOptions dumperOptions = new DumperOptions();
        dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        Yaml yaml = new Yaml(dumperOptions);
        String result = yaml.dump(value);

        String expectedResult =
                "ports:\n" +
                "- '8000'\n" +
                "- '10000:9000'\n" +
                "- '10600:23'\n";

        Assert.assertEquals(result, expectedResult);
    }

}

Root cause

This regular expression is matching for the first and third items in the list in the example, but not for the second one.

Comments (7)

  1. Fabio Silva Carvalho reporter

    Andrey, thanks for answering by the way. So, I know SnakeYaml supports YAMl version 1.1. But, is there a plan to support version 1.2? Or should we use SnakeYaml Engine? Thanks.

  2. Andrey Somov

    SnakeYAML Engine is in production.

    Because of recent introduction of comments, it may have issues which should be fixed soon. (I hope that @Wolf2323 can help us )

  3. Andrey Somov

    If you use version 2.2.1 it should meet all the quality criteria (or wait for the release of 2.3.1)

  4. Log in to comment