Skip to content

Fix separator handling in JsonFileItemWriter when appending - #5615

Open
noojung wants to merge 1 commit into
spring-projects:mainfrom
noojung:fix-jsonfileitemwriter
Open

noojung wants to merge 1 commit into
spring-projects:mainfrom
noojung:fix-jsonfileitemwriter

Conversation

@noojung

@noojung noojung commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Overview

JsonFileItemWriter produces invalid JSON in append mode in two cases.

Empty chunk: if only empty chunks are written to a file that already contains items (for example, when the processor filters out every item), a trailing comma is left before the closing ]:

[
 "aaa",
 "bbb",

]

Restart: if a step is restarted after at least one chunk was committed, two separators are written before the first item after the restart:

[
 "aaa",
,
 "bbb"
]

The empty-chunk case occurs because doWrite() writes a separator for existing items without checking whether the chunk is empty. On restart, both hasExistingItems and the restored linesWritten cause a separator to be written.

This PR skips empty chunks and writes a single separator when either condition applies. Regression tests cover both cases.

Related PR

Signed-off-by: Hyunwoo Jung <hyunwoojung@kakao.com>
@noojung noojung changed the title Fix separator handling in JsonFileItemWriter when appending Fix separator handling in JsonFileItemWriter append mode Oct 20, 2026
@noojung noojung changed the title Fix separator handling in JsonFileItemWriter append mode Fix separator handling in JsonFileItemWriter when appending Oct 20, 2026
@noojung
noojung marked this pull request as draft October 20, 2026 15:17
@noojung
noojung marked this pull request as ready for review October 20, 2026 15:46
@quaff

quaff commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Good catch!

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.

2 participants