-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Exception when deserialization uses a record with a constructor property with access=READ_ONLY
#4119
Comments
you probably need to define the name explicitly if you use |
One of the benefits of |
oh duh, the issue is much more simple. since you define the property as READ_ONLY, it can't be deserialized. the error could be better but there is no functional change needed here imo. |
I tried to put a name explicitly, but didn't work.
Before 2.15.x the result of deserialize or ignoring on deserialization a field with For me this is a regression because with the previous version 2.14.3 it works, the change to 2.15.x is not a major change according semver and this is not compatible with previous versions if it was intended. |
I would not call it a regression, I would call it finally having the right behavior :) AIUI, the record implementation changed a lot 2.14->2.15. It now uses most of the existing bean infrastructure instead of its own special handling. That means aspects that were previously not implemented for records, now are. But once again this is @cowtowncoder's call. |
Thank you @yawkat for the explanation. I'm curious about your reasoning, how would you use the READ_ONLY property instead, only to throw an exception when the JSON input contains a field marked with it? My use case is to use the same value object class to serialize and deserialize, but I want to ignore some JSON fields when deserializing, that's the reason why READ_ONLY is useful in my understanding. |
Hmmh. Semantics of But I agree that at very least exception thrown is not good. |
normal beans behave the same way. this constructor: @JsonCreator
public Bean(@JsonProperty("foo") String foo, @JsonProperty(value = "bar", access = JsonProperty.Access.READ_ONLY) String bar) { fails with
|
Ok. So at least we know handling is the same. Questions would then be:
|
imo it should still throw an exception, just a better one. |
The
So, should READ_ONLY be an exception to this statement? |
in my opinion, the docs read like mutators (including creators) that mutate READ_ONLY properties will not be called. and this leads to the exception you see. |
I have been debugging and the exception happens before the data to be parsed is known. It is my first time inspecting the internals of jackson code, so I could probably be wrong, but when I call a method which need to deserialize a JSON data into a Java object the first action is try to get from the cache a proper deserializer for that VO or create it. This exception happens during deserializer creation if a constructor argument contains a property like READ_ONLY no matters what the data input is. |
Yes, it is not possible for jackson to deserialize an object if the creator/constructor would have to set a READ_ONLY property. That is reasonable behavior, it's just the error message that is awful. |
After upgrading to 2.15 most of my records are broken, I used READ_ONLY a lot 🥲 |
@saulgiordani Please no piling on existing issues unless you have the same problem. Sounds like yours is different so please file a new issue with details. |
@cowtowncoder Sorry, I receive the same exception using the Lombok builder. Something that didn't happen before upgrading. As the exception Is exclty the same I guessed this was the right thread. |
@saulgiordani But you specifically said
and here issue is about Be that as it may, if you are hitting the same problem then you need to stop using |
I'm sorry @cowtowncoder it was my mistake. Will edit the message! |
I understand that the error message is not the best one, but taking into account that versions < 2.14.x were working with READ_ONLY with much probability a lot of apps will break with 2.15.x, so what is the alternative to use READ_ONLY or something similar in records to not deserialize some fields? |
Deciding factor is whether it SHOULD HAVE worked in a way it did -- or was that a bug wrt semantics. Just because earlier version behaves in certain way does not automatically mean it was how it should have. Even in cases where users found that behavior useful. As to "a lot of apps", that is hard to estimate. I do not know, but I am bit surprised if it turns out But as to alternatives, plain Another, similarly awkward possibility would be to add a bogus setter that does nothing: it'd get called but value would be ignored. That setter might require Or, come to think of it, an explicit constructor that calls Writing all of this, I am starting to think maybe we should allow So I would consider PR to support it. |
Ok, looking at test for issue #1890 ( Also: as per earlier comments, not specific to Records: works the same way for regular POJOs (as of 2.16.1) |
Two places that seem relevant. First, in
which is where information will be dropped. Commenting this out will fail 2 tests for #1890 (where we retain property). Second potential place would be in |
Looks like this was working with 3.0 ( |
access=READ_ONLY
Fixed via #4515, will be in 2.18(.0). |
I can confirm this is working again in 2.18.0. Thank you very much! |
Search before asking
Describe the bug
When deserialization uses a record with a constructor property with
access=READ_ONLY
, the following exception is thrown:A sample record is provided bellow.
Version Information
2.15.2
Reproduction
Sample record with the issue:
If I remove the
JsonProperty.Access.READ_ONLY
from order field, a new exception is thrown indicating the argument with the issue is...: Argument #2 of constructor [constructor for...
, that is, the other field with READ_ONLY.The object mapper is:
I also tried with a plain object mapper
JsonMapper.builder().build()
without customizations and the error is still there.Expected behavior
In 2.14.3, it works perfectly.
Additional context
I have tested the application with 2.15.3-SNAPSHOT and 2.16.0-SNAPSHOT and the error is thrown too.
The text was updated successfully, but these errors were encountered: