Skip to content

FlatFileItemWriterBuilder - RecordFieldExtractor does not honor names() for Java Record types #4935

@DevSeongmin

Description

@DevSeongmin

Bug description
When using FlatFileItemWriterBuilder with Java Record types, specifying partial fields via .names() does not work as expected. Internally, the RecordFieldExtractor is created but the .setNames() method is not called, causing the extractor to output all record fields instead of only the specified subset.

Environment

  • Spring Batch version: 6.x (e.g., 6.0.0-M1)
  • Java version: 17 or higher
  • Build tool: Maven or Gradle
  • Database: In-memory database (H2) or none for this test
  • Operating system: Any

Steps to reproduce
Define a Java Record class, e.g.:

public record UserInfo(
    String username,
    String email,
    int age,
    String phoneNumber
) {}

Create and configure the writer with .sourceType(UserInfo.class) and partial fields with .names("username", "phoneNumber").
Write items using this writer and verify that the output file contains all fields instead of only the specified ones.

Expected behavior
Only the fields specified in .names() should be extracted and written to the output file, excluding all others.

Minimal Complete Reproducible example

@Bean
@StepScope
public FlatFileItemWriter<UserInfo> userInfoWriter(
        @Value("#{jobParameters['outputDir']}") String outputDir) {
    return new FlatFileItemWriterBuilder<UserInfo>()
            .name("userInfoWriter")
            .resource(new FileSystemResource(outputDir + "/user_infos.csv"))
            .delimited()
            .delimiter(",")
            .sourceType(UserInfo.class)
            .names("username", "phoneNumber")
            .build();
}

// After writing, the output CSV still contains all fields instead of only username and phoneNumber.

Expected.

Alice Johnson,555-123-4567
Bob Smith,555-987-6543
Carol Williams,555-234-5678
David Brown,555-345-6789
Eve Davis,555-456-7890

Result.

Alice Johnson,[email protected],28,555-123-4567
Bob Smith,[email protected],35,555-987-6543
Carol Williams,[email protected],42,555-234-5678
David Brown,[email protected],30,555-345-6789
Eve Davis,[email protected],27,555-456-7890

Additional Notes
Powerd by KILL-9 💀

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions