Skip to content

Conversation

LeeHyungGeol
Copy link

Related Issues
Fixes #4916
Related to #4908, #4935

Description

This PR fixes an issue where RecordFieldExtractor ignores the names() configuration when using Java Records with FlatFileItemWriterBuilder.

Problem

When using FlatFileItemWriterBuilder with a Java Record type and specifying field names via names(), the RecordFieldExtractor was created but the setNames() method was not called, causing all record fields to be extracted instead of only the specified subset.

// This configuration was being ignored
FlatFileItemWriter<UserRecord> writer = new FlatFileItemWriterBuilder<UserRecord>()
    .sourceType(UserRecord.class)
    .names("name", "age")  // <-- These names were not being applied
    .delimited()
    .build();

Solution

Updated both DelimitedBuilder and FormattedBuilder in FlatFileItemWriterBuilder to properly call setNames() on the RecordFieldExtractor when names are specified:

if (this.sourceType != null && this.sourceType.isRecord()) {
    RecordFieldExtractor<T> recordFieldExtractor = new RecordFieldExtractor<>(this.sourceType);
    if (!this.names.isEmpty()) {
        recordFieldExtractor.setNames(this.names.toArray(new String[0]));
    }
    this.fieldExtractor = recordFieldExtractor;
}

Testing

Added comprehensive tests covering:

  • Selecting specific fields from a record
  • Reordering fields in the output
  • Both DelimitedBuilder and FormattedBuilder configurations
  • Backward compatibility when all fields are specified

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.

RecordFieldExtractor in FlatFileItemWriterBuilder doesn't reflect names() setting
1 participant