Skip to content
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
Expand Up @@ -33,6 +33,7 @@
* @param <T> type of mapped items
* @author Mahmoud Ben Hassine
* @author Seungyong Hong
* @author Yanming Zhou
* @since 4.3
*/
public class RecordFieldSetMapper<T> implements FieldSetMapper<T> {
Expand Down Expand Up @@ -76,14 +77,12 @@ public T mapFieldSet(FieldSet fieldSet) {
Assert.isTrue(fieldSet.getFieldCount() == this.constructorParameterNames.length,
"Fields count must be equal to record components count");
Assert.isTrue(fieldSet.hasNames(), "Field names must be specified");
Object[] args = new Object[this.constructorParameterNames.length];
@Nullable Object[] args = new Object[this.constructorParameterNames.length];
for (int i = 0; i < args.length; i++) {
String name = this.constructorParameterNames[i];
Class<?> type = this.constructorParameterTypes[i];
Assert.notNull(name, "Constructor parameter names must not be null");
Object converted = this.typeConverter.convertIfNecessary(fieldSet.readRawString(name), type);
Assert.notNull(converted,
() -> String.format("Cannot convert field '%s' to required type '%s'", name, type.getName()));
args[i] = converted;
}
return BeanUtils.instantiateClass(this.mappedConstructor, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@

import org.junit.jupiter.api.Test;

import org.springframework.batch.infrastructure.item.file.mapping.RecordFieldSetMapper;
import org.springframework.batch.infrastructure.item.file.transform.DefaultFieldSet;
import org.springframework.batch.infrastructure.item.file.transform.FieldSet;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* @author Mahmoud Ben Hassine
* @author Seungyong Hong
* @author Yanming Zhou
*/
class RecordFieldSetMapperTests {

Expand Down Expand Up @@ -83,10 +84,25 @@ void testMapFieldSetWhenEmptyRecord() {
assertNotNull(empty);
}

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

// when
User user = recordFieldSetMapper.mapFieldSet(fieldSet);
assertNotNull(user);
assertNull(user.id());
}

record Person(int id, String name) {
}

record EmptyRecord() {
}

record User(Integer id, String name) {
}

}
Loading