Java record components should dictate written fields - #142
Conversation
|
reviewed and am-ed |
|
I am concerned with this PR because we do intentionally enable virtual field so this is a regression side note: think we can upgrade to java 17 as a baseline and avoid asm workaround tip: solution can be to make the tostring/hashcode hardcoded set confgiurable in rg.apache.johnzon.mapper.access.MethodAccessMode#doFindReaders, will work for both mapper and jsonb impl and cover this issue feature edit: created #144 as an alternative which doesn't break current usage, any hope you have a look? |
|
On cell so can check out the reference later. What do we think about a JsonbConfig flag? |
|
But So as far as I interpret it the spec says fields, record components (that's record fields only, not it's methods!) and Java Bean property. A method But That said, Romain is correct to point out that we should have a configuration option to have applications switch back to our old Record handling for some time (maybe even mark it with a deprecation warning?) for compatibility reasons. |
|
@struberg to be honest i'm fine not respecting the spec there and aligning on the POJO side since otherwise it is hard to handle virtual fields and forces to materialize all values which is quite dumb. Also it is our behavior since years (since we do support records) for that exact reason so I would promote it as main behavior, add the toggle as in my PR, and make it to the spec instead of regressing and adding it to the spec later. |
|
What about that option: have that flag, but always enable the spec compliant version (to not serialise Record methods, except if they are annotated with That way we would keep Mapper compatible with the old version by default, and JsonB compatible with the spec by default. |
the feature is used for JSON-B models so it would be a breaking change I don't see much benefit from - and solution to ignore the additional accessors is trivial ( not sure it was obvious but with years the mapper feature was promoted to our JSON-B implementation so I wouldn't assume we can decouple them now. |
|
I've run a few tests with latest yasson (JSON-B reference implementation), and they behave like the JSON-B spec rather clearly defines: results in But results in I don't like this result from the RI neither, as the attribute name is clearly wrong. it should not be getOtherName but only So our old behaviour is clearly a bug and we have to fix it! |
100% disagree on that and on the assumption the RI is right - there are a ton of converter/serializer area we do behave differently so the RI runtime is not a proof. opened jakartaee/jsonb-api#399 |
|
There've been a few references to JSON-B spec. The spec doesn't say the word "record" anywhere, which is likely a huge gap we should close. Circling back, would we be willing to allow a configuration flag? Yes, Transient is great, but doesn't help me as the unwanted method are generated code (toBuilder() method from lombok). We use it in 100+ records and do control the config. Any "do it this way instead" suggestions welcome as long as I do get feedback on if a config option would be welcome (disabled by default of course, for backwards compatibility). |
|
The newest spec draft does in section 3.7 and others. So your use case is you use Lombok and it generates a We might look into configuring a list of excluded methods. We already do this hardcoded for |
|
@dblevins guess you might take over #144 but the historical design and solution for that (which exists since day 0) was to set a custom access mode, potentially decorating the jsonb default one, works well and solves your issue in a few seconds of code (maybe minutes if you do use an agent to do it ;)). This is the common way to solve lombok toBuilder pattern (the side note being a nicer pattern can be to use a static method there to not break the data side of the record but it is more a design thing, technically you're already covered) |
|
I think there might be even an easier workaround which also works with older Johnzon versions. Just use a custom That way you can exclude the We will still have to address this properly in Johnzon, but I would like to wait for what we do in the upcoming specification before we create a hack which is broken soon again. |
|
@struberg would be preferred if using jsonb but not mapper only but agree |
|
Once we know how the spec intends to solve it then we can also look if we want to adopt the same strategy in Mapper. But I'd prefer to not do things prematurely in Mapper and then have to implement a completely different route in Json-B. |
|
@struberg we'll likely not change mapper but my point was not to do anything since we do support David's feature since the first version |
Given a record like this, only the name and age fields should be written.
https://issues.apache.org/jira/browse/JOHNZON-432