Uploaded image for project: 'Spring Batch'
  1. Spring Batch
  2. BATCH-2680

JobParameters deserialization fails

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 4.0.0
    • Fix Version/s: 4.0.1
    • Component/s: Core

      Description

      org.springframework.batch.core.step.job.JobStep#doExecute adds JobParameters into executionContext. This will be serialized into BATCH_STEP_EXECUTION_CONTEXT, but deserialization will fail if using the default org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.

      Example unit test if using empty JobParameters:

        @Test
        public void testDeserializingJob() throws Exception {
          Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
      
          Map<String, Object> m = new HashMap<>();
          m.put("params", new JobParameters());
      
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          serializer.serialize(m, out);
          String serialized = new String(out.toByteArray(), "ISO-8859-1");
      
          InputStream is = new ByteArrayInputStream(serialized.getBytes(StandardCharsets.UTF_8));
          Map<String, Object> deserialized = serializer.deserialize(is);
        }
      
      com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "empty" (class org.springframework.batch.core.JobParameters), not marked as ignorable (one known property: "parameters"])
       at [Source: (ByteArrayInputStream); line: 1, column: 116] (through reference chain: java.util.HashMap["params"]->org.springframework.batch.core.JobParameters["empty"])
      
      	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
      	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
      	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1567)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1545)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:374)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
      	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:116)
      	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
      	at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:529)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
      	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
      	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3065)
      	at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.deserialize(Jackson2ExecutionContextStringSerializer.java:59)
      

      Example unit test if using non-empty JobParameters:

        @Test
        public void testDes() throws Exception {
          Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
      
          Map<String, JobParameter> jobParametersMap = new HashMap<>();
          jobParametersMap.put("paramName", new JobParameter("paramValue"));
      
          Map<String, Object> m = new HashMap<>();
          m.put("params", new JobParameters(jobParametersMap));
      
          ByteArrayOutputStream out = new ByteArrayOutputStream();
          serializer.serialize(m, out);
          String serialized = new String(out.toByteArray(), "ISO-8859-1");
      
          InputStream is = new ByteArrayInputStream(serialized.getBytes(StandardCharsets.UTF_8));
          Map<String, Object> deserialized = serializer.deserialize(is);
        }
      
      com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.springframework.batch.core.JobParameter` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
       at [Source: (ByteArrayInputStream); line: 1, column: 114] (through reference chain: java.util.HashMap["params"]->org.springframework.batch.core.JobParameters["parameters"]->java.util.LinkedHashMap["paramName"])
      
      	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
      	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1342)
      	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1275)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:325)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:527)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
      	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:116)
      	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromObject(AsArrayTypeDeserializer.java:61)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserializeWithType(MapDeserializer.java:400)
      	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:138)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:368)
      	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
      	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:116)
      	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromAny(AsArrayTypeDeserializer.java:71)
      	at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializer$Vanilla.deserializeWithType(UntypedObjectDeserializer.java:712)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:529)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:364)
      	at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:29)
      	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
      	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3065)
      	at org.springframework.batch.core.repository.dao.Jackson2ExecutionContextStringSerializer.deserialize(Jackson2ExecutionContextStringSerializer.java:59)
      

        Attachments

          Activity

            People

            • Assignee:
              mbenhassine Mahmoud Ben Hassine
              Reporter:
              madis Madis Liias
            • Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: