Skip to content

Make Parser extend Closeable - #1937

Open
Huyvux12 wants to merge 1 commit into
spring-projects:mainfrom
Huyvux12:fix/issue-1936-parser-closeable
Open

Huyvux12 wants to merge 1 commit into
spring-projects:mainfrom
Huyvux12:fix/issue-1936-parser-closeable

Conversation

@Huyvux12

Copy link
Copy Markdown

Make Parser usable in a try-with-resources statement, as requested in gh-1936:

try (Parser parser = new LdifParser(resource)) {
	parser.open();
	while (parser.hasMoreRecords()) {
		doWith(parser.getRecord());
	}
} catch (IOException ex) {
	//
}

(Note: open() is still required before parsing, as before; the issue's snippet omits it.)

Changes

  • Parser now extends java.io.Closeable. It already declared close() throws IOException, which is signature-identical to Closeable.close(), so no implementer changes are needed (LdifParser is the only implementer in the repo) and the change is binary-compatible.
  • Also fix LdifParser.close(), which this change depends on to be meaningful: it only closed the reader when resource.isOpen() was true, and Resource.isOpen() defaults to false — ClassPathResource and FileSystemResource do 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

  • Base SHA on upstream main: fae250d5a104.
  • Reproduction (before the fix): the new test does not compile — try-with-resources not applicable to variable type (Parser cannot be converted to AutoCloseable); and with only the Closeable change, the new stream-closed test fails because close() never closed the reader for a non-open resource.
  • After the fix: the full spring-ldap-ldif test module passes, 33/33 (31 existing + 2 new tests in LdifParserCloseableTests: parsing through try-with-resources, and the underlying stream being closed for a non-open resource).
  • Control case: the 31 pre-existing tests pass 31/31 on the unmodified base, so the close() change introduces no regression there.
  • Regression scope: the whole spring-ldap-ldif module (all 4 test classes). No other module is touched by this diff.
  • Risks checked and excluded: no caller depends on close() being a no-op (only LdifParserTests.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 failed open() is a no-op instead of an NPE as before. No @since tag: no new public API member is added and Parser itself predates 4.3. LdapTestUtils.loadLdif still never calls close() — left untouched as a possible follow-up, not part of this issue.

Environment note

./gradlew format check and ./gradlew checkstyleMain checkstyleTest could 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 manual javac + JUnit Platform Console build using the exact dependency versions from gradle/libs.versions.toml. The diff follows existing repo style (tabs, import order). CI verification of formatting/checkstyle is appreciated.

Closes #1936

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Closeable to Parser in spring-ldap-ldif-core

1 participant