eclipse-ee4j / yasson

Eclipse Yasson project
https://projects.eclipse.org/projects/ee4j.yasson
Other
204 stars 96 forks source link

JsonBinding#fromJson not closing InputStream after successfull completion #591

Closed Scharage closed 1 year ago

Scharage commented 1 year ago

Describe the bug The problem is the following: I want to use the fromJson method with a FileInputStream and noticed that the Stream is not closed as promised through the api documentation:

Description copied from interface:
jakarta.json.bind.Jsonb Reads in a JSON data from the specified InputStream and return the resulting content tree.
Specified by:
fromJson in interface Jsonb
Params:
stream – The stream is read as a JSON data. **Upon a successful completion, the stream will be closed by this method.**
clazz – Type of the content tree's root object.
Type parameters:
<T> – Type of the content tree's root object.
Returns:
the newly created root object of the java content tree
Throws:
JsonbException – If any unexpected error(s) occur(s) during deserialization.
NullPointerException – If any of the parameters is null.

To Reproduce To reproduce the behaviour i wrote a code snippet. To make it functional replace PATHTOJSONFILE with an actual json file which holds {"key":"Test"} as a content for example.

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Test {

    public static void main(String[] args) throws FileNotFoundException {
        Jsonb jsonb = JsonbBuilder.create();
        InputStream testStream = new FileInputStream(new File("PATHTOJSONFILE"));
        jsonb.fromJson(testStream,TestEntity.class);

        try{
            testStream.read();
            throw new RuntimeException("Failed, because stream was not closed.");
        }catch (IOException e) {
            //This is wanted as stream should be closed.
        }

    }

    public static class TestEntity{
        private String key;

        public TestEntity() {
        }
    }
}

Expected behavior I expect the method to close the inputstream after completion, just as documented in the documentation. Or the documentation to be updated describing that its not closed, with a reason.

System information:

Additional context Add any other context about the problem here. test.txt

Degubi commented 1 year ago

This was fixed by #586

Scharage commented 1 year ago

I tested it again and it is indeed fixed in the 3.0.3 version. Thank you, this Ticket will be closed now.