Skip to content

Commit 8aaf1d3

Browse files
committed
Fix decoding of default job parameter type in extended notation
Resolves #4299
1 parent 0b10569 commit 8aaf1d3

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/converter/JsonJobParametersConverter.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 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.
@@ -15,16 +15,11 @@
1515
*/
1616
package org.springframework.batch.core.converter;
1717

18-
import java.io.IOException;
19-
20-
import com.fasterxml.jackson.core.JsonParser;
2118
import com.fasterxml.jackson.core.JsonProcessingException;
22-
import com.fasterxml.jackson.databind.MappingJsonFactory;
2319
import com.fasterxml.jackson.databind.ObjectMapper;
2420

2521
import org.springframework.batch.core.JobParameter;
2622
import org.springframework.batch.core.JobParameters;
27-
import org.springframework.util.StringUtils;
2823

2924
/**
3025
* Converter for {@link JobParameters} instances that uses a JSON naming convention for
@@ -36,7 +31,7 @@
3631
* where:
3732
*
3833
* <ul>
39-
* <li>value: string literal repesenting the value</li>
34+
* <li>value: string literal representing the value</li>
4035
* <li>type (optional): fully qualified name of the type of the value. Defaults to
4136
* String.</li>
4237
* <li>identifying (optional): boolean to flag the job parameter as identifying or not.
@@ -58,7 +53,7 @@
5853
*/
5954
public class JsonJobParametersConverter extends DefaultJobParametersConverter {
6055

61-
private ObjectMapper objectMapper = new ObjectMapper();
56+
private final ObjectMapper objectMapper;
6257

6358
/**
6459
* Create a new {@link JsonJobParametersConverter} with a default
@@ -96,7 +91,10 @@ protected JobParameter decode(String encodedJobParameter) {
9691
try {
9792
JobParameterDefinition jobParameterDefinition = this.objectMapper.readValue(encodedJobParameter,
9893
JobParameterDefinition.class);
99-
Class<?> parameterType = Class.forName(jobParameterDefinition.type());
94+
Class<?> parameterType = String.class;
95+
if (jobParameterDefinition.type() != null) {
96+
parameterType = Class.forName(jobParameterDefinition.type());
97+
}
10098
boolean parameterIdentifying = true;
10199
if (jobParameterDefinition.identifying() != null && !jobParameterDefinition.identifying().isEmpty()) {
102100
parameterIdentifying = Boolean.valueOf(jobParameterDefinition.identifying());

spring-batch-core/src/test/java/org/springframework/batch/core/converter/JsonJobParametersConverterTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 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.
@@ -85,4 +85,20 @@ void testDecodeWithDefaultIdentifyingFlag() {
8585
Assertions.assertTrue(jobParameter.isIdentifying());
8686
}
8787

88+
@Test
89+
void testDecodeWithDefaultIdentifyingFlagAndDefaultType() {
90+
// given
91+
JsonJobParametersConverter converter = new JsonJobParametersConverter();
92+
String encodedJobParameter = "{\"value\":\"foo\"}";
93+
94+
// when
95+
JobParameter<String> jobParameter = converter.decode(encodedJobParameter);
96+
97+
// then
98+
Assertions.assertNotNull(jobParameter);
99+
Assertions.assertEquals("foo", jobParameter.getValue());
100+
Assertions.assertEquals(String.class, jobParameter.getType());
101+
Assertions.assertTrue(jobParameter.isIdentifying());
102+
}
103+
88104
}

0 commit comments

Comments
 (0)