Skip to content
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

Record with no-arg constructor with does not deserialise correctly #549

Closed
dansiviter opened this issue Apr 23, 2022 · 6 comments · Fixed by #552
Closed

Record with no-arg constructor with does not deserialise correctly #549

dansiviter opened this issue Apr 23, 2022 · 6 comments · Fixed by #552
Assignees
Labels
bug Something isn't working right

Comments

@dansiviter
Copy link
Contributor

dansiviter commented Apr 23, 2022

Describe the bug
If a record has a no-arg constructor when deserialised the optional is always <empty>.

To Reproduce
An example is available here.

Expected behavior
The correct constructor for the record is selected.

System information:

  • OS: Windows 11
  • Java Version: 17
  • Yasson Version: 3.0.0-RC1

Additional context
N/A

@dansiviter dansiviter added the bug Something isn't working right label Apr 23, 2022
@dansiviter dansiviter changed the title Record with no-arg constructor doesn't sets empty Optional Record with no-arg constructor with Optional field does not deserialise correctly Apr 23, 2022
@dansiviter dansiviter changed the title Record with no-arg constructor with Optional field does not deserialise correctly Record with no-arg constructor with does not deserialise correctly Apr 23, 2022
@dansiviter
Copy link
Contributor Author

Updates as I originally thought it was due to Optional but appears it happens every time.

@Verdent Verdent self-assigned this Apr 25, 2022
@Verdent
Copy link
Member

Verdent commented Apr 25, 2022

Hi Dan, this is happening due to default empty constructor you are having in all of the records. Yasson is trying to find @JsonbCreator annotated constructor/factory and finds none, tries to find not annotated constructor to use, but there are multiple of them and it is not clear which one should be used. But since there is default constructor, if backs to use that. If you remove empty constructor, everything will work as expected. Or if you need that default constructor for some reason, you can overwrite the parametrized one and mark it as @JsonbCreator and Yasson will prioritize this one. This is possible workaround for now.

Now I am thinking whether we should throw an exception in cases when record do have multiple constructors (including the default one) and none of them is marked as @JsonbCreator. It might possibly make sense for records. Record processing is already failing when handling multiple parametrized constructors and have none annotated as creator.

@dansiviter
Copy link
Contributor Author

From RecordTest.java:L69 it does suggest that it should throw an exception but I'm not seeing that behaviour in 3.0.0-RC1. 🤔

@dansiviter
Copy link
Contributor Author

I've just checked and it's due to default no-args constructor behaviour of records. I'd argue a no-arg constructor is a pointless record so should throw an exception regardless.

@Verdent
Copy link
Member

Verdent commented Apr 26, 2022

Exactly my thought when I was analyzing it yesterday. I will make the needed change :-)

@Verdent
Copy link
Member

Verdent commented Apr 26, 2022

It does fail for multiple constructors, but since it is taking nearly the same processing path as regular class, I forgot to make it to fail when default constructor is present. It backs to use that and does not fail in this case. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working right
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants