Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization issue with @JsonIgnore and @JsonCreator + @JsonProperty for same property name #2001

Closed
jskierbi opened this issue Apr 18, 2018 · 3 comments
Milestone

Comments

@jskierbi
Copy link

Issue originally found on jackson-module-kotlin: FasterXML/jackson-module-kotlin#124. Issue reproducible with Java code.

Entity class:

static public class Foo {
  @JsonIgnore 
  public String query;

  @JsonCreator
  @ConstructorProperties("rawQuery")
  public Foo(@JsonProperty("query") String rawQuery) {
    query = rawQuery;
  }
}

Fails this test:

Foo deserialized = new ObjectMapper().readValue("{\"query\": \"bar\"}", Foo.class);
assertEquals("bar", deserialized.query);
@cowtowncoder
Copy link
Member

One quick note: example is ambiguous, and I wonder if it should fail -- first constructor is given two different names ("rawQuery", "query"), and it is not clear as to which one should be used.
It would be possible to think of "query" as being more specific, being on parameter, whereas "rawQuery" is associated with constructor itself.

So maybe it's just matter of clarifying that this is indeed the proper annotation precedence...

@jskierbi
Copy link
Author

jskierbi commented Apr 18, 2018

That is my thinking. Exact same behaviour without additional ctor props annotation is triggered in Kotlin: data class Foo(@JsonProperty("query") rawQuery: String) (with jackson-module-kotlin) - I would expect "query" to have precedence over ctor parameter name

Actual behaviour is here:

POJOPropertyBuilder prop = (expl && impl.isEmpty())
? _property(props, pn) : _property(props, impl);

Named ctor param name is reported as implicit property name, thus property initially gets "rawQuery" name upon discovery, then is renamed to "query" in rename phase. When same property exists in the class with @JsonIgnore annotation, "query" property gets into ignored, then rename phase changes "rawQuery" property into "query", but originally ignored "query" stays in the ignored. My idea is to unignore new name of the property during rename phase - this, in my opinion, should introduce least possible behavioural changes.

What do you think?

@cowtowncoder cowtowncoder changed the title Deserialization issue with @JsonIgnore and @JsonCrator/@JsonProperty for same property name Deserialization issue with @JsonIgnore and @JsonCreator + @JsonProperty for same property name Apr 20, 2018
@cowtowncoder
Copy link
Member

@jskierbi In case of constructor parameter/argument this is fully defined since that is consider "implicit" name, similar to name detected from "getX()": explicit annotation does have precedence.
Same is not as clear between annotations (for example, if same accessor has both @JsonIgnore and @JsonProperty, which one wins? Ignoral, I think, but that's judgment call).
However, coupled with "closer" scope I could see how it can be resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants