Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,13 @@ public ObjectContent<T> read(Resource resource) throws IOException {
verify();
Assert.notNull(resource, "'resource' must not be null");
InputStream inputStream = resource.getInputStream();
T object = readObject(inputStream, getTypeNotNull());
closeQuietly(inputStream);
return new ObjectContent<>(this.type, object);
try {
T object = readObject(inputStream, getTypeNotNull());
return new ObjectContent<>(this.type, object);
}
finally {
closeQuietly(inputStream);
}
}

/**
Expand All @@ -322,9 +326,13 @@ public T readObject(Reader reader) throws IOException {
public ObjectContent<T> read(Reader reader) throws IOException {
verify();
Assert.notNull(reader, "'reader' must not be null");
T object = readObject(reader, getTypeNotNull());
closeQuietly(reader);
return new ObjectContent<>(this.type, object);
try {
T object = readObject(reader, getTypeNotNull());
return new ObjectContent<>(this.type, object);
}
finally {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a test where close() itself throws an IOException? This would verify that closeQuietly suppresses the close failure and does not affect the result of the read operation.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking a look.

That case is worth noting, though it is pre-existing behaviour rather than something this change introduces: closeQuietly already swallowed close failures before this PR, on the paths where it was reached.

What this PR does change is that the source is now closed when readObject throws. readResourceWhenReadFailsShouldCloseInputStream covers that, and it fails without the production change.

The finally block plus closeQuietly is also deliberate over try-with-resources: it keeps a failing close() from propagating on the success path, and from being attached as a suppressed exception on the failure path, so the read outcome is always what the caller sees.

Happy to add a test pinning that down if a maintainer would like it in this PR.

closeQuietly(reader);
}
}

private void closeQuietly(Closeable closeable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
Expand All @@ -40,7 +41,13 @@
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

/**
* Tests for {@link AbstractJsonMarshalTester}.
Expand All @@ -55,6 +62,8 @@ abstract class AbstractJsonMarshalTesterTests {

private static final String ARRAY_JSON = "[" + JSON + "]";

private static final String TRUNCATED_JSON = "{\"name\":\"Spring\",";

private static final ExampleObject OBJECT = createExampleObject("Spring", 123);

private static final ResolvableType TYPE = ResolvableType.forClass(ExampleObject.class);
Expand Down Expand Up @@ -145,13 +154,31 @@ void readResourceShouldReturnObject() throws Exception {
assertThat(tester.read(resource)).isEqualTo(OBJECT);
}

@Test
void readResourceWhenReadFailsShouldCloseInputStream() throws IOException {
Resource resource = mock();
InputStream inputStream = spy(new ByteArrayInputStream(TRUNCATED_JSON.getBytes()));
given(resource.getInputStream()).willReturn(inputStream);
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
assertThatException().isThrownBy(() -> tester.read(resource));
then(inputStream).should(atLeastOnce()).close();
}

@Test
void readReaderShouldReturnObject() throws Exception {
Reader reader = new StringReader(JSON);
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
assertThat(tester.read(reader)).isEqualTo(OBJECT);
}

@Test
void readReaderWhenReadFailsShouldCloseReader() throws IOException {
Reader reader = spy(new StringReader(TRUNCATED_JSON));
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
assertThatException().isThrownBy(() -> tester.read(reader));
then(reader).should(atLeastOnce()).close();
}

@Test
void parseListShouldReturnContent() throws Exception {
ResolvableType type = ResolvableTypes.get("listOfExampleObject");
Expand Down