Representer blocks parsing comments

Issue #532 invalid
Björn Zandvliet created an issue

The code below is from: src/test/java/org/yaml/snakeyaml/comment/EmitterWithCommentEnabledTest.java

With some minor changes.

@Test
public void testCommentsAtDataWindowBreak() {
    String data = getComplexConfig();   

    final DumperOptions yamlOptions = new DumperOptions();    
    final LoaderOptions loaderOptions = new LoaderOptions();    
    final Representer yamlRepresenter = new Representer();

    yamlOptions.setProcessComments(true);    
    yamlOptions.setIndent(4);    
    yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

    yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

    loaderOptions.setProcessComments(true);
    loaderOptions.setMaxAliasesForCollections(Integer.MAX_VALUE);    
    final Yaml yaml = new Yaml(new SafeConstructor(), yamlRepresenter, yamlOptions, loaderOptions);    

    System.out.println(yaml.dump(yaml.load(data)));
    // Output of this line: (file: output)
  }

The code below is from: src/main/java/org/yaml/snakeyaml/Yaml.java

public String dump(Object data) {
    List<Object> list = new ArrayList<Object>(1);    
    list.add(data);    
    return dumpAll(list.iterator());
}
private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) {
    Serializer serializer = new Serializer(new Emitter(output, dumperOptions), resolver, dumperOptions, rootTag);    
    try {serializer.open();        
      while (data.hasNext()) {
            Node node = representer.represent(data.next());            
            serializer.serialize(node);        
      }
      serializer.close();    
    } catch (IOException e) {
        throw new YAMLException(e);    
    }
}

The problem is that the Representer is making new nodes, so the parsed comments are gone.

Comments (8)

  1. Andrey Somov

    I am sorry. I do not understand the problem.

    Can you please provide a failing test ? So I can see your expectation

  2. Zachary Boronka

    Not the original poster, but I believe I understand the problem as it is affecting me as well. Below find a further modification of the test posted in this issue, which fails.

    @Test
    public void testCommentsAtDataWindowBreak() {
        String data = getComplexConfig();   
    
        final DumperOptions yamlOptions = new DumperOptions();    
        final LoaderOptions loaderOptions = new LoaderOptions();    
        final Representer yamlRepresenter = new Representer();
    
        yamlOptions.setProcessComments(true);    
        yamlOptions.setIndent(4);    
        yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    
        yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
    
        loaderOptions.setProcessComments(true);
        loaderOptions.setMaxAliasesForCollections(Integer.MAX_VALUE);    
        final Yaml yaml = new Yaml(new SafeConstructor(), yamlRepresenter, yamlOptions, loaderOptions);    
    
        String result = yaml.dump(yaml.load(data));
    
        assertEquals(data, result);
      }
    

    This test fails at the moment. It is my expectation that if you toggle processComments to true in both the LoaderOptions and the DumperOptions that this test would pass. This however is not the case, due to neither the Constructor nor the Representer utilizing the state of processComments. The Data Structure made by the Constructor currently has no way of having comments embedded in it, and the Representer has no way of interpreting that a given Data Structure contains comments. As far as I can tell, comments can currently go up to the level of the Node graph in the current implementation. I have been contemplating what it would take to make a custom Constructor and Representer for the purpose of embedding comments into a data structure, but so far have made little progress.

  3. Andrey Somov

    I think I got the question.

    The comments do not survive over the Node. You have to keep the Nodes and to emit the Nodes. This is by design. Java Structures do not have any place for comments

  4. Log in to comment