Skip to content

@JsonProperty(access = Access.READ_ONLY) does not work #2779

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

Closed
T3rm1 opened this issue Jul 1, 2020 · 7 comments
Closed

@JsonProperty(access = Access.READ_ONLY) does not work #2779

T3rm1 opened this issue Jul 1, 2020 · 7 comments
Milestone

Comments

@T3rm1
Copy link

T3rm1 commented Jul 1, 2020

Fails to work with 2.11.1

Test to reproduce the issue:

Bug.java

public class Bug {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Bean bean = new Bean();
        bean.setWorks("works");

        String json = objectMapper.writeValueAsString(bean);
        Bean newBean = objectMapper.readValue(json, Bean.class);
    }
}

Bean.java

import com.fasterxml.jackson.annotation.JsonProperty;

public class Bean {

    private String works;

    @JsonProperty(value = "t", access = JsonProperty.Access.READ_ONLY)
    public String getDoesntWork() {
        return "pleaseFixThisBug";
    }

    public String getWorks() {
        return works;
    }

    public void setWorks(String works) {
        this.works = works;
    }
}

Exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "t" (class Bean), not marked as ignorable (one known property: "works"])
 at [Source: (String)"{"works":"works","t":"pleaseFixThisBug"}"; line: 1, column: 23] (through reference chain: Bean["t"])
	at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:61)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:855)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1212)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1604)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1582)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:299)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:156)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
	at Bug.main(Bug.java:11)

Expected behaviour:
The property should only be serialized. However, it is being tried to deserialized.

This bug was supposed to be fixed with #935

@cowtowncoder
Copy link
Member

To be precise, you would like field to be quietly ignored, and not trigger exception. That makes sense.
Test for earlier fix was slightly different it seems, using field (not accessors) which might explain why that test passes, this one does not.

@T3rm1
Copy link
Author

T3rm1 commented Jul 1, 2020

My workaround right now is @JsonIgnoreProperties(ignoreUnknown = true). This only works on class level and not on method level (might be another bug).

Yes, the field should be ignored when deserializing.

@cowtowncoder
Copy link
Member

cowtowncoder commented Jul 1, 2020

I can reproduce the issue with 2.11.1, but it is actually fixed for 2.12, most likely wrt #2719.
Difference to #935 is the renaming of property: ignoral was earlier added just for implicit name because removal of accessor occurs before renaming of properties.

Due to non-trivial changes needed by fix I will not try to backport this to earlier versions; fix will be in 2.12.0. I will add a test to guard against regression however.

@cowtowncoder cowtowncoder added this to the 2.12.0 milestone Jul 1, 2020
cowtowncoder added a commit that referenced this issue Jul 1, 2020
@RakshithVP
Copy link

RakshithVP commented Aug 10, 2020

import com.fasterxml.jackson.annotation.JsonProperty;

public class Bean {
     @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String works;
}

I have a similar bean and in response I get output as expected that works is serialized properly.
But while consuming this I'm initializing to a list and READ_ONLY properties alone are NULL.

List<Bean> l = null;
l = service.getBeans();
l.get(0).getBean().getworks(); -> Here it is null.

In postman it works fine.

@cowtowncoder
Copy link
Member

@RakshithVP please note that this is fixed for 2.12 but there is no 2.12.0 release yet -- 2.11.2 does not have the fix. If you can use 2.12.0-SNAPSHOT version (either via sonatype OSS repo or using local build) you may want to see if that is fixed.
If not, a new issue should be filed; I do not usually re-open closed issues as that complicates release notes and often follow-up issues are slightly different from the original ones.

@RakshithVP
Copy link

May I know when is the release of 2.12.0 planned?

@cowtowncoder
Copy link
Member

@RakshithVP earlier I was hoping to get release out by September 2020, but that may slip -- I still hope the first release candidate would be out in september.

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