Skip to content

YamlGenerator closes the target stream when configured not to Β #99

@moabck

Description

@moabck

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;}
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    yamlIssue related to YAML format backend

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions