Skip to content

@JsonProperty in @JsonCreator is conflicting with @JsonProperty on mix-ins using views set on the field rather than the getter #1135

@NWilson

Description

@NWilson

This is similar to a previous issue (#541), but I think is a bit different because the previous issue didn't use mix-ins, which might the cause of the trouble I'm getting.

public class TestData {
  public interface View {}

  public interface MixIn {
    @JsonProperty("decoratedProp")
    Long getProp();
  }

  // does not work on the field:
  // @JsonView(View.class)
  private final Long prop;

  // does work on the getter:
  @JsonView(View.class)
  public Long getProp() { return prop; }

  @JsonCreator
  public TestData(@JsonProperty("prop") Long prop) {
    this.prop = prop;
  }
}

I serialise in using an object mapper without the MixIn, using the JSON: {"prop":123}.

I serialise out using an object mapper where the MixIn has been added, using the TestData.View view. I expect to get the JSON {"decoratedProp":123}, but instead I get {"prop":123}, plus the following exception in my logs:

T+546256 [http-nio-8443-exec-6] WARN o.s.h.c.j.MappingJackson2HttpMessageConverter - Failed to evaluate serialization for type [class c.r.s.a.a.d.TestData]: java.lang.IllegalStateException: Conflicting/ambiguous property name definitions (implicit name 'prop'): found multiple explicit names: [prop, decoratedProp], but also implicit accessor: [field c.r.s.a.a.d.TestData#prop][visible=true,ignore=false,explicitName=false]

Note that the problem goes away if I switch to setting the @JsonView() annotation on the getter method, rather than the field. Also, the problem goes away if I remove the @JsonProperty() from the constructor and instead create the objects directly and then serialise them out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    need-test-caseTo work on issue, a reproduction (ideally unit test) needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions