Skip to content

Fix RecordFieldSetMapper for empty record #4907

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2024 the original author or authors.
* Copyright 2020-2025 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.
Expand Down Expand Up @@ -31,6 +31,7 @@
*
* @param <T> type of mapped items
* @author Mahmoud Ben Hassine
* @author isanghaessi
* @since 4.3
*/
public class RecordFieldSetMapper<T> implements FieldSetMapper<T> {
Expand Down Expand Up @@ -62,6 +63,9 @@ public RecordFieldSetMapper(Class<T> targetType, ConversionService conversionSer
if (this.mappedConstructor.getParameterCount() > 0) {
this.constructorParameterNames = BeanUtils.getParameterNames(this.mappedConstructor);
this.constructorParameterTypes = this.mappedConstructor.getParameterTypes();
} else {
this.constructorParameterNames = new String[0];
this.constructorParameterTypes = new Class[0];
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2024 the original author or authors.
* Copyright 2020-2025 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.
Expand All @@ -16,7 +16,6 @@
package org.springframework.batch.item.file.mapping;

import org.junit.jupiter.api.Test;

import org.springframework.batch.item.file.transform.DefaultFieldSet;
import org.springframework.batch.item.file.transform.FieldSet;

Expand All @@ -26,14 +25,15 @@

/**
* @author Mahmoud Ben Hassine
* @author isanghaessi
*/
class RecordFieldSetMapperTests {

@Test
void testMapFieldSet() {
// given
RecordFieldSetMapper<Person> recordFieldSetMapper = new RecordFieldSetMapper<>(Person.class);
FieldSet fieldSet = new DefaultFieldSet(new String[] { "1", "foo" }, new String[] { "id", "name" });
FieldSet fieldSet = new DefaultFieldSet(new String[] {"1", "foo"}, new String[] {"id", "name"});

// when
Person person = recordFieldSetMapper.mapFieldSet(fieldSet);
Expand All @@ -48,27 +48,42 @@ void testMapFieldSet() {
void testMapFieldSetWhenFieldCountIsIncorrect() {
// given
RecordFieldSetMapper<Person> recordFieldSetMapper = new RecordFieldSetMapper<>(Person.class);
FieldSet fieldSet = new DefaultFieldSet(new String[] { "1" }, new String[] { "id" });
FieldSet fieldSet = new DefaultFieldSet(new String[] {"1"}, new String[] {"id"});

// when
Exception exception = assertThrows(IllegalArgumentException.class,
() -> recordFieldSetMapper.mapFieldSet(fieldSet));
() -> recordFieldSetMapper.mapFieldSet(fieldSet));
assertEquals("Fields count must be equal to record components count", exception.getMessage());
}

@Test
void testMapFieldSetWhenFieldNamesAreNotSpecified() {
// given
RecordFieldSetMapper<Person> recordFieldSetMapper = new RecordFieldSetMapper<>(Person.class);
FieldSet fieldSet = new DefaultFieldSet(new String[] { "1", "foo" });
FieldSet fieldSet = new DefaultFieldSet(new String[] {"1", "foo"});

// when
Exception exception = assertThrows(IllegalArgumentException.class,
() -> recordFieldSetMapper.mapFieldSet(fieldSet));
() -> recordFieldSetMapper.mapFieldSet(fieldSet));
assertEquals("Field names must be specified", exception.getMessage());
}

@Test
void testMapFieldSetWhenEmptyRecord() {
// given
RecordFieldSetMapper<EmptyRecord> mapper = new RecordFieldSetMapper<>(EmptyRecord.class);
FieldSet fieldSet = new DefaultFieldSet(new String[0], new String[0]);

// when
EmptyRecord empty = mapper.mapFieldSet(fieldSet);

// then
assertNotNull(empty);
}

record Person(int id, String name) {
}

record EmptyRecord() {
}
}