Skip to content

@JsonUnwrapped is not treated as assuming @JsonProperty("") #1013

@david-bakin

Description

@david-bakin

See discussion here but basically @JsonUnwrapped on a private field by itself does not cause that field to be serialized, currently, You need to add an explicit @JsonProperty. You shouldn't have to do that. (Following test fails currently, should pass, though you can make it pass by commenting out the line with @JsonProperty. Uses TestNG and AssertJ.)

package com.bakins_bits;

import static org.assertj.core.api.Assertions.assertThat;

import org.testng.annotations.Test;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class TestJsonUnwrappedShouldMakePrivateFieldsSerializable
{
    public static class Inner
    {
        public String animal;
    }

    public static class Outer
    {
        // @JsonProperty
        @JsonUnwrapped
        private Inner inner;
    }

    @Test
    public void jsonUnwrapped_should_make_private_fields_serializable() throws JsonProcessingException {
        // ARRANGE
        Inner inner = new Inner();
        inner.animal = "Zebra";

        Outer outer = new Outer();
        outer.inner = inner;

        ObjectMapper sut = new ObjectMapper();

        // ACT
        String actual = sut.writeValueAsString(outer);

        // ASSERT
        assertThat(actual).contains("animal");
        assertThat(actual).contains("Zebra");
        assertThat(actual).doesNotContain("inner");
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions