Skip to content

Commit 6de6bc6

Browse files
committed
Update nullability check in SpELItemKeyMapper
Related to spring-projects/spring-framework@d36475c
1 parent 82121a5 commit 6de6bc6

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/KeyValueItemWriter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ public KeyValueItemWriter(Converter<V, K> itemKeyMapper) {
5252
public void write(Chunk<? extends V> chunk) throws Exception {
5353
for (V item : chunk) {
5454
K key = itemKeyMapper.convert(item);
55-
// TODO should we add a strict mode and throw an exception instead?
56-
if (key == null) {
57-
logger.warn("Derived Key is null for item = " + item + ". This item will be skipped.");
58-
continue;
59-
}
6055
writeKeyValue(key, item);
6156
}
6257
flush();

spring-batch-infrastructure/src/main/java/org/springframework/batch/infrastructure/item/SpELItemKeyMapper.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-present the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
55
* the License. You may obtain a copy of the License at
@@ -12,7 +12,6 @@
1212
*/
1313
package org.springframework.batch.infrastructure.item;
1414

15-
import org.jspecify.annotations.Nullable;
1615
import org.springframework.core.convert.converter.Converter;
1716
import org.springframework.expression.Expression;
1817
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -22,6 +21,7 @@
2221
*
2322
* @author David Turanski
2423
* @author Stefano Cordio
24+
* @author Mahmoud Ben Hassine
2525
* @since 2.2
2626
*/
2727
public class SpELItemKeyMapper<K, V> implements Converter<V, K> {
@@ -34,8 +34,13 @@ public SpELItemKeyMapper(String keyExpression) {
3434

3535
@SuppressWarnings("unchecked")
3636
@Override
37-
public @Nullable K convert(V item) {
38-
return (K) parsedExpression.getValue(item);
37+
public K convert(V item) {
38+
K key = (K) parsedExpression.getValue(item);
39+
if (key == null) {
40+
throw new IllegalArgumentException(
41+
"Derived Key is null for item = " + item + ". This item will be skipped.");
42+
}
43+
return key;
3944
}
4045

4146
}

0 commit comments

Comments
 (0)