Skip to content

Fix #4938 Allow @JsonCreator to return null #4948

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

Merged
merged 17 commits into from
Mar 14, 2025

Conversation

JooHyukKim
Copy link
Member

resolves #4938

Copy link
Member

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to address case of properties other than those matched as Creator properties: ones collected before Creator called and ones that may come after it's called.

.without(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.readValue(in);
// Check if read all the bytes
assertEquals(-1, in.read());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach does not work: Jackson parsers buffer content so all content being read from InputStream does not actually necessarily mean all has been consumed.
There is a method in JsonParser for pushing back "unread" content but...

Instead of this, you could just enable DeserializationFeature.FAIL_ON_TRAILING_TOKENS to verify there is no more content to be read (tokens). That should catch the issue.

@JooHyukKim JooHyukKim marked this pull request as draft February 8, 2025 03:37
@JooHyukKim JooHyukKim marked this pull request as ready for review February 11, 2025 14:33
@JooHyukKim
Copy link
Member Author

JooHyukKim commented Feb 11, 2025

@cowtowncoder If you see current build failure there are two tests failing trying to handle unknown properties in following ways...

  • One failing because of FAIL_ON_UNKNOWN_PROPERTIES enabled.
  • The other because creator returned null and trying to set any-setter property on null bean

Both reaches BeanDeserializer.handleUnknownVanilla(), could it be what we already want?

Or are you saying we need the execution to continue to go on further down, to like ...

image

... here?

@cowtowncoder
Copy link
Member

@cowtowncoder If you see current build failure there are two tests failing trying to handle unknown properties in following ways...

* One failing because of `FAIL_ON_UNKNOWN_PROPERTIES` enabled.

* The other because creator returned `null` and trying to set any-setter property on `null` bean

Both reaches BeanDeserializer.handleUnknownVanilla(), could it be what we already want?

Or are you saying we need the execution to continue to go on further down, to like ...

... here?

I think that if null is returned, we should probably do forced skipping and ignoring all properties until END_OBJECT.

But that handling should be extracted to a helper method just to keep logic cleaner and possibly change. And pass already collected information.

@JooHyukKim
Copy link
Member Author

JooHyukKim commented Feb 14, 2025

@cowtowncoder Just some updates,

  • I added void testDeserializeReadingUntilEndObject() test to verify we read all the way to END_OBJECT.
  • Still haven't managed to think of the RIGHT place to insert new method to handle unknown props 🥲

@JooHyukKim
Copy link
Member Author

Alright, I think I'm stuck at trying to find the proper place/timing for method extraction.
May it be because current implementation doesn't have much changes tbh.

No rush for this work to be done, but any opinion much appreciated! 😆

@cowtowncoder
Copy link
Member

Alright, I think I'm stuck at trying to find the proper place/timing for method extraction. May it be because current implementation doesn't have much changes tbh.

No rush for this work to be done, but any opinion much appreciated! 😆

I'm happy to have a look -- might take a while as I'm going for 1 week vacation tomorrow. But will help when I get back (unless I have extra time today).

Copy link
Member

@cowtowncoder cowtowncoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cowtowncoder
Copy link
Member

@JooHyukKim Finally had time to get back to this one -- changed things slightly, the way I think it should work, and I think this is ready to be merged. Will leave open, merge tomorrow, unless you want to change things further.

@cowtowncoder
Copy link
Member

Assuming this is fine, merging.

@cowtowncoder cowtowncoder merged commit d16420f into FasterXML:2.19 Mar 14, 2025
8 checks passed
@JooHyukKim
Copy link
Member Author

Sorry late reply, yes, seems okay to merge!

@JooHyukKim JooHyukKim deleted the 4938-feature branch March 17, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow @JsonCreator annotated Creator to return null
3 participants