Skip to content

Commit 4d46d58

Browse files
committed
Configure the right FieldSetMapper based on the type of items in FlatFileItemReaderBuilder
Resolves #4160
1 parent 9a8fb1e commit 4d46d58

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilder.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@
3737
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
3838
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
3939
import org.springframework.batch.item.file.mapping.FieldSetMapper;
40+
import org.springframework.batch.item.file.mapping.RecordFieldSetMapper;
4041
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
4142
import org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy;
4243
import org.springframework.batch.item.file.transform.DefaultFieldSetFactory;
@@ -459,21 +460,26 @@ else if (this.delimitedBuilder != null) {
459460
}
460461

461462
if (this.targetType != null || StringUtils.hasText(this.prototypeBeanName)) {
462-
BeanWrapperFieldSetMapper<T> mapper = new BeanWrapperFieldSetMapper<>();
463-
mapper.setTargetType(this.targetType);
464-
mapper.setPrototypeBeanName(this.prototypeBeanName);
465-
mapper.setStrict(this.beanMapperStrict);
466-
mapper.setBeanFactory(this.beanFactory);
467-
mapper.setDistanceLimit(this.distanceLimit);
468-
mapper.setCustomEditors(this.customEditors);
469-
try {
470-
mapper.afterPropertiesSet();
463+
if (this.targetType != null && this.targetType.isRecord()) {
464+
RecordFieldSetMapper<T> mapper = new RecordFieldSetMapper(this.targetType);
465+
lineMapper.setFieldSetMapper(mapper);
471466
}
472-
catch (Exception e) {
473-
throw new IllegalStateException("Unable to initialize BeanWrapperFieldSetMapper", e);
467+
else {
468+
BeanWrapperFieldSetMapper<T> mapper = new BeanWrapperFieldSetMapper<>();
469+
mapper.setTargetType(this.targetType);
470+
mapper.setPrototypeBeanName(this.prototypeBeanName);
471+
mapper.setStrict(this.beanMapperStrict);
472+
mapper.setBeanFactory(this.beanFactory);
473+
mapper.setDistanceLimit(this.distanceLimit);
474+
mapper.setCustomEditors(this.customEditors);
475+
try {
476+
mapper.afterPropertiesSet();
477+
lineMapper.setFieldSetMapper(mapper);
478+
}
479+
catch (Exception e) {
480+
throw new IllegalStateException("Unable to initialize BeanWrapperFieldSetMapper", e);
481+
}
474482
}
475-
476-
lineMapper.setFieldSetMapper(mapper);
477483
}
478484
else if (this.fieldSetMapper != null) {
479485
lineMapper.setFieldSetMapper(this.fieldSetMapper);

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/builder/FlatFileItemReaderBuilderTests.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,11 +20,16 @@
2020
import java.util.ArrayList;
2121
import java.util.List;
2222

23+
import org.junit.Assert;
2324
import org.junit.Test;
2425

2526
import org.springframework.batch.item.ExecutionContext;
2627
import org.springframework.batch.item.file.FlatFileItemReader;
28+
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
29+
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
30+
import org.springframework.batch.item.file.mapping.RecordFieldSetMapper;
2731
import org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy;
32+
import org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor;
2833
import org.springframework.batch.item.file.transform.DefaultFieldSet;
2934
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
3035
import org.springframework.batch.item.file.transform.FieldSet;
@@ -442,6 +447,49 @@ public void testErrorMessageWhenNoLineTokenizerWasProvided() {
442447
}
443448
}
444449

450+
@Test
451+
public void testSetupWithRecordTargetType() {
452+
// given
453+
record Person(int id, String name) {
454+
}
455+
456+
// when
457+
FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>().name("personReader")
458+
.resource(getResource("1,foo")).targetType(Person.class).delimited().names("id", "name").build();
459+
460+
// then
461+
Object lineMapper = ReflectionTestUtils.getField(reader, "lineMapper");
462+
Assert.assertNotNull(lineMapper);
463+
Assert.assertTrue(lineMapper instanceof DefaultLineMapper);
464+
Object fieldSetMapper = ReflectionTestUtils.getField(lineMapper, "fieldSetMapper");
465+
Assert.assertNotNull(fieldSetMapper);
466+
Assert.assertTrue(fieldSetMapper instanceof RecordFieldSetMapper);
467+
}
468+
469+
@Test
470+
public void testSetupWithClassTargetType() {
471+
// given
472+
class Person {
473+
474+
int id;
475+
476+
String name;
477+
478+
}
479+
480+
// when
481+
FlatFileItemReader<Person> reader = new FlatFileItemReaderBuilder<Person>().name("personReader")
482+
.resource(getResource("1,foo")).targetType(Person.class).delimited().names("id", "name").build();
483+
484+
// then
485+
Object lineMapper = ReflectionTestUtils.getField(reader, "lineMapper");
486+
Assert.assertNotNull(lineMapper);
487+
Assert.assertTrue(lineMapper instanceof DefaultLineMapper);
488+
Object fieldSetMapper = ReflectionTestUtils.getField(lineMapper, "fieldSetMapper");
489+
Assert.assertNotNull(fieldSetMapper);
490+
Assert.assertTrue(fieldSetMapper instanceof BeanWrapperFieldSetMapper);
491+
}
492+
445493
private Resource getResource(String contents) {
446494
return new ByteArrayResource(contents.getBytes());
447495
}

0 commit comments

Comments
 (0)