Skip to content

Commit 5ef9af3

Browse files
quafffmbenhassine
authored andcommitted
RecordFieldSetMapper shouldn't reject null values
Close GH-5392 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 899411a commit 5ef9af3

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/file/mapping/RecordFieldSetMapper.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @param <T> type of mapped items
3434
* @author Mahmoud Ben Hassine
3535
* @author Seungyong Hong
36+
* @author Yanming Zhou
3637
* @since 4.3
3738
*/
3839
public class RecordFieldSetMapper<T> implements FieldSetMapper<T> {
@@ -76,14 +77,12 @@ public T mapFieldSet(FieldSet fieldSet) {
7677
Assert.isTrue(fieldSet.getFieldCount() == this.constructorParameterNames.length,
7778
"Fields count must be equal to record components count");
7879
Assert.isTrue(fieldSet.hasNames(), "Field names must be specified");
79-
Object[] args = new Object[this.constructorParameterNames.length];
80+
@Nullable Object[] args = new Object[this.constructorParameterNames.length];
8081
for (int i = 0; i < args.length; i++) {
8182
String name = this.constructorParameterNames[i];
8283
Class<?> type = this.constructorParameterTypes[i];
8384
Assert.notNull(name, "Constructor parameter names must not be null");
8485
Object converted = this.typeConverter.convertIfNecessary(fieldSet.readRawString(name), type);
85-
Assert.notNull(converted,
86-
() -> String.format("Cannot convert field '%s' to required type '%s'", name, type.getName()));
8786
args[i] = converted;
8887
}
8988
return BeanUtils.instantiateClass(this.mappedConstructor, args);

spring-batch-infrastructure/src/test/java/org/springframework/batch/infrastructure/item/file/mapping/RecordFieldSetMapperTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@
1717

1818
import org.junit.jupiter.api.Test;
1919

20-
import org.springframework.batch.infrastructure.item.file.mapping.RecordFieldSetMapper;
2120
import org.springframework.batch.infrastructure.item.file.transform.DefaultFieldSet;
2221
import org.springframework.batch.infrastructure.item.file.transform.FieldSet;
2322

2423
import static org.junit.jupiter.api.Assertions.assertEquals;
2524
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
import static org.junit.jupiter.api.Assertions.assertNull;
2626
import static org.junit.jupiter.api.Assertions.assertThrows;
2727

2828
/**
2929
* @author Mahmoud Ben Hassine
3030
* @author Seungyong Hong
31+
* @author Yanming Zhou
3132
*/
3233
class RecordFieldSetMapperTests {
3334

@@ -83,10 +84,25 @@ void testMapFieldSetWhenEmptyRecord() {
8384
assertNotNull(empty);
8485
}
8586

87+
@Test
88+
void testMapFieldSetWhenFieldsMayBeNull() {
89+
// given
90+
RecordFieldSetMapper<User> recordFieldSetMapper = new RecordFieldSetMapper<>(User.class);
91+
FieldSet fieldSet = new DefaultFieldSet(new String[] { "", "foo" }, new String[] { "id", "name" });
92+
93+
// when
94+
User user = recordFieldSetMapper.mapFieldSet(fieldSet);
95+
assertNotNull(user);
96+
assertNull(user.id());
97+
}
98+
8699
record Person(int id, String name) {
87100
}
88101

89102
record EmptyRecord() {
90103
}
91104

105+
record User(Integer id, String name) {
106+
}
107+
92108
}

0 commit comments

Comments
 (0)