-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Duplicate creator property "b" (index 0 vs 1) on simple java record #5049
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
Comments
Full reproduction test case import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
public class JacksonTest {
record Dummy(
@JsonProperty(value = "a", access = JsonProperty.Access.READ_ONLY) String a,
@JsonProperty(value = "b", access = JsonProperty.Access.READ_ONLY) String b) {
}
@Test
void testWithDummy() throws Exception {
ObjectMapper objectMapper = JsonMapper.builder().build();
String string = objectMapper.writeValueAsString(new Dummy ("hello", "world"));
objectMapper.readerFor(Dummy.class).readValue(string);
}
} |
Thank you for reporting this @richard-melvin -- looks like a straight bug. Although your Record definition looks odd: what is the goal here wrt |
I guess the unit test woudl fail anyway if it didn't hit the bug - pretend it was based on a literal JSON string rather than a round trip. The code is a redacted version of somehting much more complex that does have reasons for being that way... |
While it only explodes starting from UPDATEOh this is that edge case that I reported before: #4628. It only happens if record Dummy(
@JsonProperty(/* value = "a",*/ access = JsonProperty.Access.READ_ONLY) String a,
@JsonProperty(/* value = "b",*/ access = JsonProperty.Access.READ_ONLY) String b) {
} (In that ticket, I wrote "seems to be an edge case that user wouldn't/shouldn't encounter"... LOL 😭) |
Best to close as dup of #4628 |
Hmmh. Not sure -- while #4628 is older, this seems better written, for actually encountered issue? |
@richard-melvin just to be clear, Records +
So:
|
Agreed. I still don't quite get what the intent here was anyway: it would seem that deserialization would only succeed passing |
It comes from code generated from an openapi spec, using the 'read-only' feature: https://swagger.io/docs/specification/v3_0/data-models/data-types/ For such a record,m round-trip serialisation obviously wouldn't wortk, but it doesn;t need to; REST in ingherently directional. A read-only field is only set by the server, and read by the client. |
@richard-melvin in Jersey/OpenAPI scenario, what values should be put in the record for the read-only fields? Do you expect nulls? Are you going to handle merging the input from the client and merge it with the server side value? |
The use case is for a java openapi client. When serialising, you omit the values annotated as read-on;ly. When deserialising, they must be present. Sp[ecifically, it is what is generated by https://openapiprocessor.io/oap/home.html Seems straightforward rto me, what else is the annotation supposed to for? |
@richard-melvin If so, I think this is incorrect usage, what you really want is
Naming here is bit unfortunate as read/write direction depends on how you look at it -- and I can see how Jackson's choice may be non-intuitive. Regardless annotation usage seems incorrect. Beside that, error message is bad and behavior should be to pass in |
Not actually sure this is same as #4628, re-opening, to add failing test. |
Just in case this wasn't clear: equivalent POJO case works as expected, no exception, |
Adding test via #5061 -- this is tricky as almost all the way parameters "a" and "b" are passed just fine -- until at some point there are two "b"s. Very odd... |
Added test; could see where name duplication is but not where it comes from. Yet. |
With: record Dummy(
@JsonProperty(value = "a", access = READ_ONLY) String a,
@JsonProperty(value = "b", access = READ_ONLY) String b) {
}
@richard-melvin you said:
I don't really understand what you mean, but do check the behaviours documented in the table above - specifically Deserialization + |
@yihtserns It seemed to be his use of |
@cowtowncoder not entirely sure about that since I can see a few different ways to interpret his statements... 😓 I can't afford to spend time trying to get on the same page as him, so decided to do a "here's the outcome, please check for yourself #SelfServicePlease". |
@yihtserns Fair. At any rate, underlying issue from Jackson perspective is resolved as I understand. |
yeah it looks fine |
The fix is included in 2.19.0-rc2 released yesterday, fwtw. Will be in 2.19.0 as well as eventual 2.18.4 Also: |
Search before asking
Describe the bug
Using jackson-databind 2.18.3 and a java record like:
then reading a value fails with:
Changing most things about the recod causes it to work . This is a minimised version of a fuller realistic case.
Version Information
No response
Reproduction
<-- Any of the following
-->
// Your code here
Expected behavior
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: