Skip to content

Commit 43a323f

Browse files
isanghaessifmbenhassine
authored andcommitted
Fix RecordFieldSetMapper for empty record
- add initialization for constructorParameterNames, constructorParameterTypes for empty record case. - add TC for empty record case. Signed-off-by: Seungyong Hong <jesse1231@naver.com>
1 parent 503c49e commit 43a323f

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 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.
@@ -31,6 +31,7 @@
3131
*
3232
* @param <T> type of mapped items
3333
* @author Mahmoud Ben Hassine
34+
* @author Seungyong Hong
3435
* @since 4.3
3536
*/
3637
public class RecordFieldSetMapper<T> implements FieldSetMapper<T> {
@@ -63,6 +64,10 @@ public RecordFieldSetMapper(Class<T> targetType, ConversionService conversionSer
6364
this.constructorParameterNames = BeanUtils.getParameterNames(this.mappedConstructor);
6465
this.constructorParameterTypes = this.mappedConstructor.getParameterTypes();
6566
}
67+
else {
68+
this.constructorParameterNames = new String[0];
69+
this.constructorParameterTypes = new Class[0];
70+
}
6671
}
6772

6873
@Override

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2024 the original author or authors.
2+
* Copyright 2020-2025 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.
@@ -16,7 +16,6 @@
1616
package org.springframework.batch.item.file.mapping;
1717

1818
import org.junit.jupiter.api.Test;
19-
2019
import org.springframework.batch.item.file.transform.DefaultFieldSet;
2120
import org.springframework.batch.item.file.transform.FieldSet;
2221

@@ -26,6 +25,7 @@
2625

2726
/**
2827
* @author Mahmoud Ben Hassine
28+
* @author Seungyong Hong
2929
*/
3030
class RecordFieldSetMapperTests {
3131

@@ -68,7 +68,23 @@ void testMapFieldSetWhenFieldNamesAreNotSpecified() {
6868
assertEquals("Field names must be specified", exception.getMessage());
6969
}
7070

71+
@Test
72+
void testMapFieldSetWhenEmptyRecord() {
73+
// given
74+
RecordFieldSetMapper<EmptyRecord> mapper = new RecordFieldSetMapper<>(EmptyRecord.class);
75+
FieldSet fieldSet = new DefaultFieldSet(new String[0], new String[0]);
76+
77+
// when
78+
EmptyRecord empty = mapper.mapFieldSet(fieldSet);
79+
80+
// then
81+
assertNotNull(empty);
82+
}
83+
7184
record Person(int id, String name) {
7285
}
7386

87+
record EmptyRecord() {
88+
}
89+
7490
}

0 commit comments

Comments
 (0)