jackson-databind: @JsonProperty(access = Access.READ_ONLY) - unexpected behaviour
Hey,
I was hoping to make use of @JsonProperty(access = Access.READ_ONLY), but failed.
Assume this class:
public class TestPojo
{
private String firstName;
private String lastName;
@JsonProperty(access = Access.READ_ONLY)
public String getFullName()
{
return firstName + " " + lastName;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
I couldn’t find a way to stop the deserializer from attempting to deserialize the field “fullName”.
The only thing that helps is to create a setter and annotate it with @JsonIgnore
. However, that setter does not make sense and I don’t want to have it. Is this a bug in behaviour or am I missing something? Thanks
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 4
- Comments: 29 (14 by maintainers)
Commits related to this issue
- Add a (failing) test for #935 — committed to FasterXML/jackson-databind by cowtowncoder 9 years ago
- Fix #935 — committed to FasterXML/jackson-databind by cowtowncoder 7 years ago
- fixed with skipping ignored properties only working with jackson 2.8.7-SNAPSHOT see https://github.com/FasterXML/jackson-databind/issues/935 — committed to mduesterhoeft/spring-data-rest by deleted user 7 years ago
- skip all ignored json properties to avoid to accidentally set them to null Revert "fixed with skipping ignored properties" This reverts commit 8d85c612192f0c5076ac347c0ebf2f464a3f7b11. fixed with ... — committed to mduesterhoeft/spring-data-rest by deleted user 7 years ago
Hi,
is there some easy workaround, until this is fixed? I mean instead of implementing a noop setter method?
The annotation @JsonIgnoreProperties sounds like it could help here. But it also has no effect, if I add it to a getter, regardless which values I choose for allowGetters or allowSetters.
Update: It seems that I found a solution by adding
@JsonIgnoreProperties(value="some_field",
allowGetters = true, allowSetters =false)
to the class. So this could be a solution, if we don’t need the @JsonIgnoreProperties annotation on class level for other purposes.
Also: my earlier explanation was exactly backwards. Javadocs state it correctly (if not well):
READ_ONLY
refers to POJO being handled in read-only way, andWRITE_ONLY
opposite. So Java-centric, not JSON-centric.I really should have named values differently (GETTER_ONLY, SETTER_ONLY, perhaps).
@cowtowncoder
I think this is still not fixed. I have the same problem with 2.10.3 My getter is annotated with
@JsonProperty(value = "t", access = JsonProperty.Access.READ_ONLY)
TheBeanDeserializerBase
tries to deserialize it which results intoCaused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "t"
Here is the stacktrace:
FYI:
_removeUnwantedAccessor
is not called at all.I can reproduce this with 2.6. Looks like
READ_ONLY
does actually hide/remove setter method (my test fails with “unknown property”), but will not mark it as ignorable, which it probably should do.