Skip to content

Nested @JsonUnwrapped property names not correctly handled #2461

@plovell

Description

@plovell

We are using @JsonUnwrapped to serialise objects with three levels of nesting. If we serialise the top level object first, then we get the JSON we expect, but if we serialise the second level object first and then try to serialise the top level object, it is as if the @JsonUnwrapped annotation is ignored for the top level object. We are using Jackson databind version 2.9.9.3.

The following test case demonstrates the problem:

@Test
public void jsonMappingOrderTest() throws JsonProcessingException {
    ObjectMapper mapperOrder1 = new ObjectMapper();
    ObjectMapper mapperOrder2 = new ObjectMapper();

    BaseContainer inner = new BaseContainer(new Base("12345"));
    BaseContainerContainer outer = new BaseContainerContainer(inner);

    assertEquals("{\"container.base.id\":\"12345\"}", mapperOrder1.writeValueAsString(outer));
    assertEquals("{\"base.id\":\"12345\"}", mapperOrder1.writeValueAsString(inner));
    assertEquals("{\"container.base.id\":\"12345\"}", mapperOrder1.writeValueAsString(outer));

    assertEquals("{\"base.id\":\"12345\"}", mapperOrder2.writeValueAsString(inner));
    //  Will fail here
    assertEquals("{\"container.base.id\":\"12345\"}", mapperOrder2.writeValueAsString(outer));
}

static class Base {
    private String id;

    Base(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}

static class BaseContainer {
    private Base base;

    BaseContainer(Base base) {
        this.base = base;
    }

    @JsonUnwrapped(prefix = "base.")
    public Base getBase() {
        return base;
    }
}

static class BaseContainerContainer {
    private BaseContainer container;

    BaseContainerContainer(BaseContainer container) {
        this.container = container;
    }

    @JsonUnwrapped(prefix = "container.")
    public BaseContainer getContainer() {
        return container;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.19Issues planned at 2.19 or laterhas-failing-testIndicates that there exists a test case (under `failing/`) to reproduce the issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions