Skip to content

use try-with-resources statement in ResourceHttpMessageConverter #35048

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

Conversation

Pankraz76
Copy link

@Pankraz76 Pankraz76 commented Jun 13, 2025

before:

100% (3/3) 86% (13/15) 89% (26/29) 71% (10/14)

now:

100% (3/3) 93% (14/15) 96% (28/29) 78% (11/14)
100% (3/3) 93% (14/15) 96% (28/29) 85% (12/14)

}
catch (FileNotFoundException ex) {
// ignore, see SPR-12999
catch (Throwable ignored) {
Copy link
Author

@Pankraz76 Pankraz76 Jun 13, 2025

Choose a reason for hiding this comment

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

Suggested change
catch (Throwable ignored) {
catch (Throwable | NullPointerException | FileNotFoundException ignored) {

thats whats going on, right? @bclozel

in.transferTo(out);
out.flush();
}
catch (NullPointerException ex) {
Copy link
Author

Choose a reason for hiding this comment

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

NPE is ignored by try-with-resource.

}
finally {
try {
in.close();
Copy link
Author

Choose a reason for hiding this comment

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

will be called anyways by try-with-resource.

in.close();
}
catch (Throwable ex) {
// ignore, see SPR-12999
Copy link
Author

Choose a reason for hiding this comment

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

yes we still ignore but in 1 catch not in 3.

first 2 are now handled by try-with-resource.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2025
Signed-off-by: Vincent Potucek <[email protected]>
@Pankraz76 Pankraz76 force-pushed the fix-ignored-exceptions-fix-try-with-resources branch from 07e6101 to a143254 Compare June 13, 2025 14:30
@Pankraz76 Pankraz76 marked this pull request as ready for review June 13, 2025 14:32
@@ -156,5 +156,123 @@ public void writeContentInputStreamThrowingNullPointerException() throws Excepti

assertThat(outputMessage.getHeaders().getContentLength()).isEqualTo(0);
}
@Test
Copy link
Author

Choose a reason for hiding this comment

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

Suggested change
@Test
@Test

checkstyle solution seems broken and costly to obtain such a custom tool not working. better use spot.

@@ -147,31 +145,14 @@ protected boolean supportsRepeatableWrites(Resource resource) {


protected void writeContent(Resource resource, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {

// We cannot use try-with-resources here for the InputStream, since we have
Copy link
Author

Choose a reason for hiding this comment

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

its seems to be just a try catch around the resource operation.

BUILD SUCCESSFUL

image

@Pankraz76 Pankraz76 changed the title Fix ignored exceptions fix try with resources use try-with-resources statement in ResourceHttpMessageConverter Jun 13, 2025
@bclozel
Copy link
Member

bclozel commented Jun 14, 2025

Similar to #35053 and #35051
I have checked again this pattern and using try-with-resources does not keep the existing behavior.

@bclozel bclozel closed this Jun 14, 2025
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants