Skip to content

Jackson2ExecutionContextStringSerializer ignores QName prefix #4697

Open

Description

@jpraet

Bug description
See FasterXML/jackson-databind#4771: jackson QName (de)serialization ignores prefix.

Environment
Spring Batch 5.1.2, JDK 21

Steps to reproduce

    @Test
    void qname() throws Exception {
        Jackson2ExecutionContextStringSerializer serializer = new Jackson2ExecutionContextStringSerializer();
        ExecutionContext context = new ExecutionContext();
        QName qname = new QName("http://namespace", "test", "p");
        context.put("qname", qname);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        serializer.serialize(new HashMap<>(context.toMap()), outputStream);
        LOGGER.info(outputStream.toString(StandardCharsets.UTF_8));
        // <- {"@class":"java.util.HashMap","qname":["javax.xml.namespace.QName","{http://namespace}test"]}
        Map<String, Object> result = serializer.deserializeFromByteArray(outputStream.toByteArray());
        QName value = (QName) result.get("qname");
        assertThat(value.getLocalPart()).isEqualTo(qname.getLocalPart());
        assertThat(value.getNamespaceURI()).isEqualTo(qname.getNamespaceURI());
        assertThat(value.getPrefix()).isEqualTo(qname.getPrefix()); // <- fails
    }

Expected behavior
QName prefix is preserved in context (de)serialization roundtrip.

Spring Batch StaxEventItemWriter depends on QName prefix being preserved correctly:

if (!unclosedHeaderCallbackElements.isEmpty()) {
executionContext.put(getExecutionContextKey(UNCLOSED_HEADER_CALLBACK_ELEMENTS_NAME),
unclosedHeaderCallbackElements);
}

Activity

  1. added
    status: waiting-for-triage
    on Oct 30, 2024
  2. jjh75607 commented on Aug 26, 2026

    @jjh75607
    Contributor

    Some findings, since the situation has changed since this was reported.

    The cause for Jackson2ExecutionContextStringSerializer turns out to sit upstream rather than here. #4771 was resolved in Jackson 2.19 as an opt-in object form, so the fix would be to enable JsonFormat.Shape.OBJECT for QName. But this class writes type ids As.PROPERTY (Jackson2ExecutionContextStringSerializer.java:272), and Jackson then never enters its QName object branch, silently returning a corrupted value. I have reported that as FasterXML/jackson-databind#6175. The class is deprecated for removal in 7.0 in any case.

    JacksonExecutionContextStringSerializer is a different matter. It uses wrapper-array type ids, which are unaffected, so withConfigOverride(QName.class, c -> c.setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.OBJECT))) preserves the prefix. Verified on main. It needs #5502 first, which currently rejects QName outright.

    The stored format stays compatible either way. I checked both directions on main: the new config reads old string-form rows, and the old config reads new object-form rows with the prefix intact. The read path branches on the token it sees.

    A pull request with that one-line change will follow once #5502 is in, since it depends on QName being accepted there first.

  3. fmbenhassine commented on Oct 31, 2026

    @fmbenhassine
    Contributor

    @jpraet Thank you for raising this, it is a valid issue.

    While #5529 fixes the issue in JacksonExecutionContextStringSerializer (Jackson 3 based), porting it to Jackson2ExecutionContextStringSerializer requires more than the object-format configuration change. Although our Jackson 2 versions include support for QName object formatting, the serializer’s As.PROPERTY type handling encounters FasterXML/jackson-databind#6175. The fix for FasterXML/jackson-databind#6175 has not been backported to Jackson 2, so that port is currently blocked.

    Given that the Jackson 2-based serializer is deprecated for removal, it's unlikely that we continue spending effort maintaining it (or wait for Jackson to back port their fix to 2.x). Do you still use Jackson2ExecutionContextStringSerializer?


    @jjh75607 Thanks for the investigation and for PR #5529! It fixes the prefix loss in JacksonExecutionContextStringSerializer, including malformed XML produced after restarting a StaxEventItemWriter. I planned to merge it in 6.1.0 and 6.0.x.

  4. added
    status: waiting-for-reporter
    and removed
    status: waiting-for-triage
    on Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: waiting-for-reportertype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions