question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 https://github.com/FasterXML/jackson-databind/issues/935

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RakshithVPcommented, Aug 12, 2020

May I know when is the release of 2.12.0 planned?

0reactions
cowtowncodercommented, Aug 12, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JsonProperty.Access.READ_ONLY not work #1805 - GitHub
Then I tried JsonIgnoreProperties, and annotate the class with specified field, jackson ignores the field and works as expected.
Read more >
Jackson read-only property in method - Stack Overflow
I tried @JsonIgnoreProperty , @JsonGetter and @JsonProperty(access = JsonProperty. Access. READ_ONLY) on the method but nothing works.
Read more >
JsonProperty.Access (Jackson-annotations 2.6.0 API)
Access setting that means that the property may only be read for serialization, but not written (set) during deserialization. WRITE_ONLY. public static final ......
Read more >
Jackson Readonly properties and swagger UI
If the property is marking as readonly, it will be ignored when something is posted, but it will be available on the response....
Read more >
Best bits of Jackson 2.6 - cowtowncoder - Medium
AUTO: Default visibility rules are used, setter/getter may or may not be visible (default value for `@JsonProperty.access`. JsonPointer-filtering for JsonParser ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found