-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Nulls
property metadata not working for records
#2970
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
Sorry, this is how it should work. However, I wold like to do something like
Is there any way to achieve this with records? |
@cocorossello Hmmh. Interesting; since properties are passed via constructor arguments, I think the null coercion should actually work -- if you created POJO with I hope I'll find some time to look into this; code is not necessarily easy to follow but the method to look for in I would be happy to merge a PR for failing test under |
Sorry, I think I didn't explain well. That test does work and is the same behaviour with regular classes, the issue is not valid (see my second comment), I should have reopened another issue with the question. The problem is that I would like to have always non null collection fields. I can achieve this easily with regular classes:
The problem is that I'm not able to achieve this with records. How can I do that? |
@cocorossello Hmmmh. For Or do you mean that |
Oh, on implementation: |
Ok, thanks, I missed the constructor options for records. So I can make it work like:
That's enough for me, sorry for the confusion and thanks for your response. |
@cocorossello No problem! I am just eager to find out if there are edge cases, given that |
Hi, I see that you have pushed a failing test. To be honest I didn't do it because I think it's the same behaviour with classes, so it shouldn't be a bug, right?. This one also fails:
|
@cocorossello Ah. But that is actually not the same. That was what I was trying to say earlier: there is difference between using constructor as setter vs using field or setter method -- only constructor acts on "missing" value (because it must have SOME value to pass anyway, so it keeps track of value). So if you change the test to have constructor that takes values (annotation may be on field, setter or constructor parameter, as long as names match [and you use parameter-names module or Note that there is #230 to request solving the problem of field/setter method not enforcing I hope this makes more sense? Thank you for reporting the first case! |
Ok, I understood. The thing that confused me is that even using parameter-names module (and ensuring that class files has the parameter names), I still need the @JsonCreator annotation, I was trying without it and couldn't get to work (and it is using the constructor)
Anyway, thank you very much for the explanation, I'll try to look into it. |
@cocorossello I think I know this one -- if you do NOT annotate constructor, it can still be found BUT since linking between field and constructor does not exist (longer discussion why; it's a bug but probably cannot fix for 2.x; only for 3.0 after full rewrite of introspection), and as such annotations are not transferred. Because of this, just in this case you MUST add annotation on constructor parameter; they are not merged from field. If you do that, null-handling will work. |
I can see that the problem with records is that _beanProperties has the correct nullProvider and creatorProps does not. So one dirty fix (and for sure it will not work in all cases) is to merge that nullProvider. Something like:
But of course this does not cover all cases.. |
Describe the bug
Nulls.AS_EMPTY does not work for JDK14 records, it deserializes null as null and should be an empty list
Version information
2.12.0
To Reproduce
Expected behavior
mapper.readValue("{}", SomeRecord.class).someCollection() should be an empty list
Additional context
I think it's a bug, I've tried to dig a bit in the code but I have no clue. If you can point me in some direction maybe I can try to arrange a PR
The text was updated successfully, but these errors were encountered: