Skip to content

Throwing error when TargetType and FieldSetMapper is provided #4885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Patrick Baumgartner
* @author François Martin
* @since 4.0
* @see FlatFileItemReader
*/
Expand Down Expand Up @@ -459,6 +461,9 @@ else if (this.delimitedBuilder != null) {
throw new IllegalStateException("No LineTokenizer implementation was provided.");
}

Assert.state(this.targetType == null || this.fieldSetMapper == null,
"Either a TargetType or FieldSetMapper can be set, can't be both.");

if (this.targetType != null || StringUtils.hasText(this.prototypeBeanName)) {
if (this.targetType != null && this.targetType.isRecord()) {
RecordFieldSetMapper<T> mapper = new RecordFieldSetMapper<>(this.targetType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @author Glenn Renfro
* @author Patrick Baumgartner
* @author François Martin
*/
class FlatFileItemReaderBuilderTests {

Expand Down Expand Up @@ -563,6 +565,17 @@ void testErrorMessageWhenNoLineTokenizerWasProvided() {
}
}

@Test
void testErrorWhenTargetTypeAndFieldSetMapperIsProvided() {
var builder = new FlatFileItemReaderBuilder<Foo>().name("fooReader")
.resource(getResource("1;2;3"))
.lineTokenizer(line -> new DefaultFieldSet(line.split(";")))
.targetType(Foo.class)
.fieldSetMapper(fieldSet -> new Foo());
var exception = assertThrows(IllegalStateException.class, builder::build);
assertEquals("Either a TargetType or FieldSetMapper can be set, can't be both.", exception.getMessage());
}

@Test
void testSetupWithRecordTargetType() {
// given
Expand Down
Loading