Repository navigation
doc: avoid x-ref instantiation and deserialziation - #418
Conversation
| String toDeserialize = "{ \"publicField\" : \"Test String\", \"publicSetter\" : \"Test String\", \"extraField\" : \"Test String\", \"extraSetter\" : \"Test String\" }"; | ||
|
|
||
| CreatorWithNameConflictContainer unmarshalledObject = jsonb.fromJson(toDeserialize, CreatorWithNameConflictContainer.class); | ||
| assertThat("Failed to instantiate type using JsonbCreator annotated constructor during unmarshalling.", |
There was a problem hiding this comment.
should be lower case since the setter will override the constructor no? keeping the model just callbacks avoids specific rules for fields vs methods, constructor vs factories, all these cases + deserializers or adapters IMHO
There was a problem hiding this comment.
I am testing the behavior as I clarified it in the specification here:
And as I thought we agreed in the issue that if a person is defining a @JsonbConstructor and using the @JsonbProperty on a parameter of the constructor or static factory, then their intention is that the JSON field will NOT be bound again after instantiation.
There was a problem hiding this comment.
@KyleAure not sure I agreed, I said that it can makes sense but then it makes a specific case - it will not behave the same for setters for examples, and that I think it doesn't make sense to make a particular case for fields and it is more valuable to keep everything consistent (field == methods in terms of behavior) so I would still call it until flagged as undesired with a new API. Best practise stays to not use public fields too, even for dump DTO, while there is a framework handling the instances.
There was a problem hiding this comment.
I guess I'm having a hard time understanding what user would want the behavior you are describing. From the users perspective the only steps they care about are serialization and deserialization. To a user deserialization is just one unit of work, whereas we as implementations know that it's really two; we first need to instantiate an object, then parse and set.
Therefore, as a user if I took the time to define a @JsonbCreator custom constructor or factory method then I'd expect during deserialization of a JSON object that some values are passed to the custom constructor or factory, some values are set using setters, and some values are set using public fields. (in that exclusive priority order)
Each of which is a "special case" but in order to get the behavior expected by a user we need to do the hard work to make that happen. And as a specification even if it is not best practice we need to document what happens when users do not use best practices.
There was a problem hiding this comment.
To a user deserialization is just one unit of work
this can be true or not, what does the end user see? an object with a constructor and methods, exactly as with plain java so I guess the assumption it does behave the same is not crazy.
And as a specification even if it is not best practice we need to document what happens when users do not use best practices.
Agree on that and I think we do: custom instantiation ([1]) - and note it is instantiation and NOT deserialization which is aligned with what I'm saying, and the field access strategy ([2], both parts totally define the behavior I do describe.
What I do really like is that everything is simple understandable, remindable and manageable from end user perspective whereas starting to make constructor+public field an exception is weirdish as rule - even if I get the "we can".
[1] https://github.com/jakartaee/jsonb-api/blob/6d2c7b82734c9a1a8449d3655a269672b18c9b12/spec/src/main/asciidoc/jsonb.adoc#45-custom-instantiation
[2] https://github.com/jakartaee/jsonb-api/blob/6d2c7b82734c9a1a8449d3655a269672b18c9b12/spec/src/main/asciidoc/jsonb.adoc#371-scope-and-field-access-strategy
There was a problem hiding this comment.
@rmannibucau Thanks for all your input on this and sorry for the late reply.
I think for the time being the best course of action might be to "agree to disagree" and leave the language in the spec the way it is.
My preference would be for the two sections (custom-instantiation and scope-and-field-access-strategy) to reference one another and define required behavior when fields from the custom constructor/factory method overlap with fields/setters that are referenced during deserialization. My preference for that behavior would be to help the end user who is likely not to read/understand the full spec and to just write a @JsonbCreator method without having to think about deserialization as two distinct steps. Which has already been implemented in Yasson and taking that convenience away from users would be a disadvantage.
But, also understand from your point of view that the way the spec was written in the past kept the two sections (custom-instantiation and scope-and-field-access-strategy) mutually exclusive and that the result of that is the behavior seen in Johnzon. Maybe in another major release we can hash this out again and come to a resolution that makes migrating between our two implementations easier. 😆
It would be helpful if a member or two from the Apache Johnzon team were committers to this spec and future disagreements could be resolved via traditional voting procedures. If you or anyone else on your teams would like to become committers to have more say in the direction of this spec let me know and I'd be happy to nominate. The more code reviewers we have the better.
I have refactored this PR to reflect the "agree to disagree" stance. Please give it a review and let me know if you agree? (did some light edits to the spec to make it more human readable/editable in the future)
There was a problem hiding this comment.
My preference for that behavior would be to help the end user who is likely not to read/understand the full spec and to just write a @JsonbCreator method without having to think about deserialization as two distinct steps. Which has already been implemented in Yasson and taking that convenience away from users would be a disadvantage.
well you can totally say the reversed sentence for johnzon users too, this is really "make it easy to understand" vs "try to make it clever but surprising" topic (to be honest we solved it more than 10 years ago in johnzon and nothing significative changed since that time so I have a hard time to see the default changing).
Maybe in another major release we can hash this out again and come to a resolution that makes migrating between our two implementations easier.
hopefully ASAP else JSON-B is kind of pointless since only value of JakartaEE is to be a common contract and there we will not do the job - ok for a few minors not for next major I guess.
It would be helpful if a member or two from the Apache Johnzon team were committers to this spec
that said "force" votes are not great solution IMHO
note for future: think to ignore public fields as being part of the contract, guess it will be the sense of the story with records and can solve this issue too + will enable some nice features like lazyness (a bit like ORM)
There was a problem hiding this comment.
If you are a committer why do your PR reviews show as non-binding?
(that is why I assumed you were not a committer sorry!)
Let me see if I can get that fixed.
that said "force" votes are not great solution IMHO
I don't like it either, but sometimes it is the only way to move topics forward otherwise we will go in circles forever in situations like this where there is no clear way forward.
There was a problem hiding this comment.
where there is no clear way forward
I'm kind of confident this is only due to the minor aspect of the change there, if we make it configurable by end user or better define callbacks of the full lifecycle it becomes more straight forward so just an early stage thing (this is why I accept the status quo and let it for next "big" major 🤞 ) - hope this makes sense, start to be late there ;)
There was a problem hiding this comment.
@rmannibucau -- FYI - if you want your code reviews to be blocking/binding in the projects you are a committer for you need to link your github account with your eclipse account. https://accounts.eclipse.org/users/rmannibucau/linked-accounts
b3870c0 to
6ed6957
Compare
Fixes #347
- Updates spec to clarify behavior when there are name conflicts in@JsonbCreatorconstructors/factories- Creates new tests to verify the behavior of name conflicts in@JsonbCreator