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

Regression: yasson on SpringBoot: IOException Stream Closed #604

Open
IndyNaessens opened this issue May 30, 2023 · 4 comments
Open

Regression: yasson on SpringBoot: IOException Stream Closed #604

IndyNaessens opened this issue May 30, 2023 · 4 comments
Labels
bug Something isn't working right

Comments

@IndyNaessens
Copy link

Describe the bug
It seems there is a regression on the following issue #389. I originally reported the issue here spring-projects/spring-boot#35651.

To Reproduce

FileWriter writer = new FileWriter("output.json");
Jsonb jsonb = JsonbBuilder.create();
jsonb.toJson(Collections.singletonMap("a", "alpha"), writer);
jsonb.toJson(Collections.singletonMap("a", "alpha"), writer);

Expected behavior
toJson not closing the writer.

System information:

  • OS: Windows
  • Java Version: 17
  • Yasson Version: 3.0.3

Additional context
This issue seems to be introduced with version 3.0.3. Version 3.0.2 and below work as expected.

@IndyNaessens IndyNaessens added the bug Something isn't working right label May 30, 2023
@Degubi
Copy link
Contributor

Degubi commented Jul 10, 2023

Seems to be caused by #586, reverting this fixes it for me

@jamezp
Copy link

jamezp commented Oct 6, 2023

#586 is the correct fix according the Jsonb.fromJson() JavaDoc.

stream - The stream is read as a JSON data. Upon a successful completion, the stream will be closed by this method.

@greek1979
Copy link
Contributor

greek1979 commented Feb 12, 2025

Discovered few days ago in our test system that REST/JSON based backend app logs are literally FULL of the very same error. The output stream being already closed by the moment Spring's JSON HTTP message converter attempts to write the response and flush it to the (http) output stream. After some code analysis, I reached the same offending change (#586 ) that someone introduced into Yasson 3.0.3 to supposedly auto-close the generators and parsers...

Well, it ruins the intent of original fix (#389 ) as can be clearly evident from org.eclipse.yasson.internal.JsonBinding code:

@Override
public String toJson(Object object, Type type) throws JsonbException {
    StringWriter writer = new StringWriter();
    try (JsonGenerator generator = writerGenerator(writer)) {
        new SerializationContextImpl(jsonbContext, type).marshall(object, generator);
    }
    return writer.toString();
}

@Override
public void toJson(Object object, Writer writer) throws JsonbException {
    final SerializationContextImpl marshaller = new SerializationContextImpl(jsonbContext);
    try (JsonGenerator generator = writerGenerator(writer)) {
        marshaller.marshallWithoutClose(object, generator);
    }
}

The try-with-resources obviously closes the JSON generator once the "marshall-WITHOUT-close" (emphasis mine) method completes. Why close the resource if the intent is NOT to close it?... Sorry for the rant.

System information:

  • OS: Windows 11
  • Java Version: 17
  • Yasson Version: 3.0.4 (latest one as of writing)
  • Spring Version: 5.3.39 (not Spring Boot)

@greek1979
Copy link
Contributor

The #631 did resolve the implicitly closing the output STREAMS when generating JSON content, but my particular issue is with (output) WRITERS, not streams. They should not be closed by Yasson code either if a ready-to-use Writer has been passed in by the calling code; the calling code is supposed to take care of its resources and objects.

greek1979 pushed a commit to greek1979/yasson that referenced this issue Feb 12, 2025
greek1979 added a commit to greek1979/yasson that referenced this issue Feb 12, 2025
greek1979 added a commit to greek1979/yasson that referenced this issue Feb 12, 2025
greek1979 added a commit to greek1979/yasson that referenced this issue Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

No branches or pull requests

4 participants