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

JsonBinding#fromJson not closing InputStream after successfull completion #591

Closed
Scharage opened this issue Feb 20, 2023 · 2 comments
Closed
Labels
bug Something isn't working right

Comments

@Scharage
Copy link

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:

  • OS: Windows
  • Java Version: 11
  • Yasson Version: Noticed first 2.0.1 , tested in 3.0.2 again

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

@Scharage Scharage added the bug Something isn't working right label Feb 20, 2023
@Degubi
Copy link
Contributor

Degubi commented Jul 10, 2023

This was fixed by #586

@Scharage
Copy link
Author

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

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

2 participants