Repository navigation
Conversation
Parser already declared close() throws IOException, so making it extend java.io.Closeable enables try-with-resources with no changes required from implementers (LdifParser is the only one). Also fix LdifParser.close(): it only closed the reader when resource.isOpen() was true, which is false for the standard ClassPathResource and FileSystemResource, so try-with-resources silently leaked the reader for those resources. Close the reader whenever one was opened instead. Add LdifParserCloseableTests covering try-with-resources parsing and that the underlying stream is closed for a non-open resource. Closes spring-projectsgh-1936 Signed-off-by: Huyvux12 <152865675+Huyvux12@users.noreply.github.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make
Parserusable in a try-with-resources statement, as requested in gh-1936:(Note:
open()is still required before parsing, as before; the issue's snippet omits it.)Changes
Parsernow extendsjava.io.Closeable. It already declaredclose() throws IOException, which is signature-identical toCloseable.close(), so no implementer changes are needed (LdifParseris the only implementer in the repo) and the change is binary-compatible.LdifParser.close(), which this change depends on to be meaningful: it only closed the reader whenresource.isOpen()was true, andResource.isOpen()defaults tofalse—ClassPathResourceandFileSystemResourcedo not override it — so try-with-resources silently leaked the reader for the standard resource types.close()now closes the reader whenever one was opened (and is a no-op when the parser was never opened).Verification
main:fae250d5a104.try-with-resources not applicable to variable type (Parser cannot be converted to AutoCloseable); and with only theCloseablechange, the new stream-closed test fails becauseclose()never closed the reader for a non-open resource.spring-ldap-ldiftest module passes, 33/33 (31 existing + 2 new tests inLdifParserCloseableTests: parsing through try-with-resources, and the underlying stream being closed for a non-open resource).close()change introduces no regression there.spring-ldap-ldifmodule (all 4 test classes). No other module is touched by this diff.close()being a no-op (onlyLdifParserTests.closeLdif()calls it, on a per-test parser that is never reused);open()always creates a fresh reader, so closing it cannot affect a shared stream; double-close is safe (BufferedReader.close()is idempotent);close()after a failedopen()is a no-op instead of an NPE as before. No@sincetag: no new public API member is added andParseritself predates 4.3.LdapTestUtils.loadLdifstill never callsclose()— left untouched as a possible follow-up, not part of this issue.Environment note
./gradlew format checkand./gradlew checkstyleMain checkstyleTestcould not be run in this sandbox (the Gradle daemon cannot communicate here: loopback TCP connections are reset once they carry ~128 KB), so the test run above was done with a manualjavac+ JUnit Platform Console build using the exact dependency versions fromgradle/libs.versions.toml. The diff follows existing repo style (tabs, import order). CI verification of formatting/checkstyle is appreciated.Closes #1936