Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

@JsonUnwrapped annotation is ignored when a field is an Optional #64

@akobiakov

Description

@akobiakov

Consider the example:

public class Test {
    public static void main(String[] args) throws JsonProcessingException {
        System.out.println(new ObjectMapper()
                .registerModule(new GuavaModule())
                .writeValueAsString(new Parent()));
    }
}

class Child {
    @JsonProperty
    private String text = "hello, world!";
}

class Parent {
    @JsonProperty
    @JsonUnwrapped
    private Child child = new Child();
}

Produces: {"text":"hello, world!"}

Whereas the following example:

public class Test {
    public static void main(String[] args) throws JsonProcessingException {
        System.out.println(new ObjectMapper()
                .registerModule(new GuavaModule())
                .writeValueAsString(new Parent()));
    }
}

class Child {
    @JsonProperty
    private String text = "hello, world!";
}

class Parent {
    @JsonProperty
    @JsonUnwrapped
    private Optional<Child> child = Optional.of(new Child());
}

(note, that the child field is now Optional)

Produces: {"child":{"text":"hello, world!"}}

Such a behaviour looks a bit odd and counterintuitive. In my opinion, the output should be the same as in the first case.

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