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

YamlGenerator closes the target stream when configured not to #99

Closed
moabck opened this issue Aug 2, 2018 · 2 comments
Closed

YamlGenerator closes the target stream when configured not to #99

moabck opened this issue Aug 2, 2018 · 2 comments
Labels
yaml Issue related to YAML format backend
Milestone

Comments

@moabck
Copy link

moabck commented Aug 2, 2018

Bug description

YamlGenerator closes the target stream when configured not to.

Versions used

jackson-dataformat-yaml 2.9.2
jackson-databind 2.9.6

Expected result

The target stream not closed when writing a value. No output when running reproduction script/program.

Actual result

The target stream is closed when using the YamlGenerator, with the following output when running the reproduction script/program.

Yaml test failed: Stream was closed.
Yaml2 test failed: Stream was closed.

Steps to reproduce

package com.spotify;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.IOException;
import java.io.OutputStream;

public class YamlBug {

  static final Pojo pojo = new Pojo("bar");

  public static void main(String[] args) throws IOException {
    try {
      jsonTest();
    } catch (AssertionError e) {
      System.out.println("Json test failed: " + e.getMessage());
    }

    try {
      yamlTest();
    } catch (AssertionError e) {
      System.out.println("Yaml test failed: " + e.getMessage());
    }

    try {
      yamlTest2();
    } catch (AssertionError e) {
      System.out.println("Yaml2 test failed: " + e.getMessage());
    }

  }

  static void jsonTest() throws IOException {
    final ThrowingOutputStream jsonOutputStream = new ThrowingOutputStream();
    final ObjectMapper jsonMapper = new ObjectMapper()
        .disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    jsonMapper.writeValue(jsonOutputStream, pojo);
  }

  static void yamlTest() throws IOException {
    final ThrowingOutputStream yamlOutputStream = new ThrowingOutputStream();
    final ObjectMapper yamlMapper = new ObjectMapper(
        new YAMLFactory()
            .disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET)
    );
    yamlMapper.writeValue(yamlOutputStream, pojo);
  }

  static void yamlTest2() throws IOException {
    final ThrowingOutputStream yamlOutputStream = new ThrowingOutputStream();
    final ObjectMapper yamlMapper =
        new ObjectMapper(
            new YAMLFactory()
        ).disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
    yamlMapper.writeValue(yamlOutputStream, pojo);
  }

  static class ThrowingOutputStream extends OutputStream {

    @Override
    public void write(final int b) throws IOException {

    }

    @Override
    public void close() throws IOException {
      throw new AssertionError("Stream was closed.");
    }
  }

  static class Pojo {

    public final String foo;

    Pojo(final String foo) {this.foo = foo;}
  }
}
@cowtowncoder cowtowncoder added yaml Issue related to YAML format backend active labels Aug 31, 2018
@cowtowncoder
Copy link
Member

Thank you for reporting this. I hope to figure out what causes this; I hope it is not something SnakeYAML does.

vboulaye added a commit to vboulaye/jackson-dataformats-text that referenced this issue Oct 20, 2018
vboulaye added a commit to vboulaye/jackson-dataformats-text that referenced this issue Oct 20, 2018
vboulaye added a commit to vboulaye/jackson-dataformats-text that referenced this issue Oct 20, 2018
cowtowncoder added a commit that referenced this issue Oct 24, 2018
…sabled

fix #99 do not close stream when autoclose is disabled in yaml generator/parser
@cowtowncoder cowtowncoder changed the title YamlGenerator closes the target stream when configured not to YamlGenerator closes the target stream when configured not to Oct 24, 2018
@cowtowncoder
Copy link
Member

Fixed by @vboulaye's patch, will be resolved in 2.9.8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
yaml Issue related to YAML format backend
Projects
None yet
Development

No branches or pull requests

2 participants