Skip to content

appendAllowed(true) breaks JSON array when the writer is opened/written/closed multiple times #5272

Closed

Description

@banseok1216

Bug description
When org.springframework.batch.infrastructure.item.json.JsonFileItemWriter is configured with setAppendAllowed(true) and used in multiple runs (open → write → close) against the same target file, the resulting file is not a valid JSON array. The second run appends data after the closing bracket (]) instead of inserting it inside the array with a comma.

Environment
Spring Batch 6.0.2

Steps to reproduce
Even with appendAllowed(true), after multiple runs the final file should remain a valid JSON array, and in the reproduction test below ["foo", "bar"] should be read correctly.

/*
 * Copyright 2018-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.batch.infrastructure.item.json;

import java.io.File;
import java.nio.file.Files;

import tools.jackson.databind.json.JsonMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import org.springframework.batch.infrastructure.item.Chunk;
import org.springframework.batch.infrastructure.item.ExecutionContext;
import org.springframework.batch.infrastructure.item.json.JsonFileItemWriter;
import org.springframework.batch.infrastructure.item.json.JsonObjectMarshaller;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.WritableResource;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class JsonFileItemWriterTests {

	private WritableResource resource;

	@BeforeEach
	void setUp() throws Exception {
		File file = Files.createTempFile("test", "json").toFile();
		this.resource = new FileSystemResource(file);
	}

	@Test
	void appendAllowedShouldNotCorruptJsonArray() throws Exception {
		JsonFileItemWriter<String> writer = new JsonFileItemWriter<>(this.resource, new JacksonJsonObjectMarshaller<>());
		writer.setAppendAllowed(true);

		writer.open(new ExecutionContext());
		writer.write(Chunk.of("foo"));
		writer.close();

		writer.open(new ExecutionContext());
		writer.write(Chunk.of("bar"));
		writer.close();

		String[] result = readJsonArrayOfStrings();
		assertArrayEquals(new String[] { "foo", "bar" }, result);
	}

	private String[] readJsonArrayOfStrings() throws Exception {
		File file = this.resource.getFile();
		String raw = Files.readString(file.toPath());
		System.out.println(raw);
		return new JsonMapper().readValue(raw, String[].class);
	}

}

current json structure

[
 "foo"
]
 "bar"
]

Expected behavior

expect json structure

[
 "foo",
 "bar"
]

Activity

  1. added
    status: waiting-for-triage
    on Feb 5, 2026
  2. quaff commented on Feb 5, 2026

    @quaff
    Contributor

    It requires deleting ] before writing new items which violates append semantics, I doubt that JsonFileItemWriter should supports this feature since JSON is not appendable.

  3. banseok1216 commented on Feb 5, 2026

    @banseok1216
    ContributorAuthor

    I agree. It's ambiguous.

    It's confusing because JsonFileItemWriter inherits setAppendAllowed(true).

    appendAllowed should be explicitly rejected as an unsupported feature, or the semantics should be changed.

  4. fmbenhassine commented on Feb 5, 2026

    @fmbenhassine
    Contributor

    I don't see this as a violation of appending logic. We should be able to append a json element in the file, even if the content is wrapped in an array. Same thinking about an XML file (Spring Batch should be able to append an element before the closing the tag).

    This is a bug to me and should be fixed.

  5. added
    has: minimal-example
    for: backport-to-5.2.x
    and removed
    status: waiting-for-triage
    on Feb 5, 2026
  6. added a commit that references this issue on Feb 6, 2026
    56d7983
  7. quaff commented on Feb 6, 2026

    @quaff
    Contributor

    I created #5273 to fix.

  8. added 5 commits that reference this issue on Feb 6, 2026
    aa36aec
    acf9a5a
    7eafb4f
    fd1b321
    0157704
  9. added this to the 6.0.4 milestone on May 29, 2026
  10. added a commit that references this issue on May 29, 2026
    a187a1f
  11. added a commit that references this issue on May 29, 2026
    550c65f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: backport-to-5.2.xhas: minimal-examplein: infrastructurerelated-to: item-readers-writerstype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions