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

@JsonProperty(access = READ_ONLY) together with generated constructor (lombok) causes JsonMappingException: Could not find creator property with name [...] #1345

Closed
Raniz85 opened this issue Aug 23, 2016 · 7 comments
Milestone

Comments

@Raniz85
Copy link

Raniz85 commented Aug 23, 2016

The following class fails to deserialise with a com.fasterxml.jackson.databind.JsonMappingException: Could not find creator property with name 's' (in class LombokObject):

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@Getter
@Setter
@NoArgsConstructor
public class LombokObject {

    private String s;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Integer i;
}

Whereas the following class - which is functionally identical but with constructors, getters and setters in the code - can be deserialised:

import com.fasterxml.jackson.annotation.JsonProperty;

public class ManualObject {

    private String s;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Integer i;

    public ManualObject() {}

    public ManualObject(final String s, final Integer i) {
        this.s = s;
        this.i = i;
    }
    public String getS() {
        return s;
    }
    public void setS(final String s) {
        this.s = s;
    }
    public Integer getI() {
        return i;
    }
    public void setI(final Integer i) {
        this.i = i;
    }
}

The exception is

com.fasterxml.jackson.databind.JsonMappingException: Could not find creator property with name 's' (in class LombokObject)
 at [Source: {"s":"test"}; line: 1, column: 1]

    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
    at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1233)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:552)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:226)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:141)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:406)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:475)
    at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3890)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3785)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2833)
@Raniz85
Copy link
Author

Raniz85 commented Aug 23, 2016

I made a sample project to illustrate the issue.

@cowtowncoder
Copy link
Member

Thank you for reporting this. I think this is part of a known problem related to resolution of creator properties vs "regular" properties, where effectively the two can be out of sync. Unfortunately solving that issue is likely to require a major rewrite of property resolution, which is why the problem persists (in some form it has affected version from 2.6 to 2.8 I think).

@cowtowncoder cowtowncoder added the lombok Issue (likely) related to use of Lombok label Sep 9, 2016
@cowtowncoder
Copy link
Member

I think this might be due to #1383 that I just fixed (to be included in 2.7.8 / 2.8.4).
If anyone can test with 2.7.8-SNAPSHOT / 2.8.4-SNAPSHOT, that'd be great. If not, I hope to find time soon to test with sample project.

@cowtowncoder cowtowncoder changed the title @JsonProperty(access = READ_ONLY) together with generated constructor (lombok) causes JsonMappingException: Could not find creator property with name [...] @JsonProperty(access = READ_ONLY) together with generated constructor (lombok) causes JsonMappingException: Could not find creator property with name [...] Oct 5, 2016
@benneq
Copy link

benneq commented Jan 22, 2017

I'm using Jackson 2.8.6 and get this error, too. Here's my test code:

@AllArgsConstructor
public static class Foo {
    @JsonProperty(access=Access.READ_ONLY)
    public String id;
    public String name;
}
ObjectMapper om = new ObjectMapper();
om.readValue("{\"name\":\"test\"}", Foo.class);
com.fasterxml.jackson.databind.JsonMappingException: Could not find creator property with name 'name'

The code works fine when deleting Access.READ_ONLY. Even WRITE_ONLY works fine.

@cowtowncoder
Copy link
Member

@benneq Thank you, I can reproduce this.

cowtowncoder added a commit that referenced this issue Mar 30, 2017
@cowtowncoder
Copy link
Member

Interesting. For some reason Creator properties are not being collected at all...

@cowtowncoder
Copy link
Member

Ah ok. Due to one of parameters being READ_ONLY, constructor is not recognized as Creator -- all parameters must be recognized as valid properties. I will change code slightly not to do consistency check in this case (since there is no Creator to match to), which should allow usage to work. Important thing here is to retain no-args constructor.

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

3 participants