Skip to content

OutputDecorator not called with XmlMapper #333

@ndionisi

Description

@ndionisi

OutputDecorator seems to be ignored on XmlMapper.
With a (JSON) ObjectMapper, the following test passes:

    @Test
    public void json_objectMapper_should_use_decorator() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.getFactory().setOutputDecorator(new OutputDecorator() {
            @Override
            public OutputStream decorate(IOContext ctxt, OutputStream out) throws IOException {
                out.write("DECORATOR".getBytes(StandardCharsets.UTF_8));
                return out;
            }

            @Override
            public Writer decorate(IOContext ctxt, Writer w) throws IOException {
                w.write("DECORATOR");
                return w;
            }
        });
        assertThat(objectMapper.writeValueAsString("value")).isEqualTo("DECORATOR\"value\"");
    }

But with an XmlMapper, the following test fails:

    @Test
    public void xml_objectMapper_should_use_decorator() throws Exception {
        ObjectMapper objectMapper = new XmlMapper();
        objectMapper.getFactory().setOutputDecorator(new OutputDecorator() {
            @Override
            public OutputStream decorate(IOContext ctxt, OutputStream out) throws IOException {
                out.write("DECORATOR".getBytes(StandardCharsets.UTF_8));
                return out;
            }

            @Override
            public Writer decorate(IOContext ctxt, Writer w) throws IOException {
                w.write("DECORATOR");
                return w;
            }
        });
        assertThat(objectMapper.writeValueAsString("value")).isEqualTo("DECORATOR<String>value</String>");
    }

with the following error:

org.junit.ComparisonFailure: 
Expected :"DECORATOR<String>value</String>"
Actual   :"<String>value</String>"

According to the source code, it seems that the XmlFactory never calls its _decorate() method.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions