-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Fix Enum
deserialization with JsonFormat.Shape.OBJECT
using both DELEGATING
and PROPERTIES
creator modes
#3851
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15e34c3
Fix
JooHyukKim 5b01c7c
add comment
JooHyukKim c5add48
Reduce cognitive complexity
JooHyukKim fb7c843
Synchronize JsonParser
JooHyukKim 8e38787
Remove transient keyword
JooHyukKim 3bc0de2
construct _propCreatorRef lazily
JooHyukKim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package com.fasterxml.jackson.databind.deser.std; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import com.fasterxml.jackson.core.JacksonException; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
|
@@ -40,10 +41,12 @@ class FactoryBasedEnumDeserializer | |
|
||
/** | ||
* Lazily instantiated property-based creator. | ||
* Introduced in 2.8 and wrapped with {@link AtomicReference} in 2.15 | ||
* | ||
* @since 2.15 | ||
* | ||
* @since 2.8 | ||
*/ | ||
private transient PropertyBasedCreator _propCreator; | ||
private AtomicReference<PropertyBasedCreator> _propCreatorRef = new AtomicReference<>(null); | ||
|
||
public FactoryBasedEnumDeserializer(Class<?> cls, AnnotatedMethod f, JavaType paramType, | ||
ValueInstantiator valueInstantiator, SettableBeanProperty[] creatorProps) | ||
|
@@ -132,18 +135,21 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx | |
// 30-Mar-2020, tatu: For properties-based one, MUST get JSON Object (before | ||
// 2.11, was just assuming match) | ||
if (_creatorProps != null) { | ||
if (!p.isExpectedStartObjectToken()) { | ||
if (p.isExpectedStartObjectToken()) { | ||
_propCreatorRef.compareAndSet(null, | ||
PropertyBasedCreator.construct(ctxt, _valueInstantiator, _creatorProps, | ||
ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES))); | ||
|
||
p.nextToken(); | ||
return deserializeEnumUsingPropertyBased(p, ctxt, _propCreatorRef.get()); | ||
} | ||
// If value cannot possibly be delegating-creator, | ||
if (!_valueInstantiator.canCreateFromString()) { | ||
final JavaType targetType = getValueType(ctxt); | ||
ctxt.reportInputMismatch(targetType, | ||
"Input mismatch reading Enum %s: properties-based `@JsonCreator` (%s) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.%s", | ||
ClassUtil.getTypeDescription(targetType), _factory, p.currentToken()); | ||
} | ||
if (_propCreator == null) { | ||
_propCreator = PropertyBasedCreator.construct(ctxt, _valueInstantiator, _creatorProps, | ||
ctxt.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)); | ||
"Input mismatch reading Enum %s: properties-based `@JsonCreator` (%s) expects JSON Object (JsonToken.START_OBJECT), got JsonToken.%s", | ||
ClassUtil.getTypeDescription(targetType), _factory, p.currentToken()); | ||
} | ||
p.nextToken(); | ||
return deserializeEnumUsingPropertyBased(p, ctxt, _propCreator); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Summary here,
|
||
|
||
// 12-Oct-2021, tatu: We really should only get here if and when String | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But wouldn't this create (and try set) a new instance every time? :-D
(instead of trying to
get()
and only create-and-set ifnull
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you check again? Take below as an example. Invoking
get()
andset
separately wouldn't be atomic anymore 🤔.According to
compareAndSet
documentation,There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or we can do good-ol' double-checked locking like ones done in
EnumDeserializer
below.jackson-databind/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java
Lines 432 to 445 in 68ba588
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, my problem is that instead of only creating it if needed, it creates new instance every time and sets -- and never uses value. You are correct in that get + set is not atomic, but I am not worried about atomicity here; if instance gets created 2 or 3 times that's fine. They are all identical. Goal is just to avoid always creating instance eagerly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And no need to do double-locking with synchronized: just do
get()
from reference; if (and only if)null
returned, construct instance,set()
(orcompareAndSet()
againstnull
). Use the props gotten or created.Or leave as-is and I can add that as well, not a big deal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I think I understand now! Thank you for the explanation! 🙏🏼🙏🏼.
The code creates newly instance in
compareAndSet(null, PropertyBasedCreator.construct())
. The new intance will be ignored, but created "everytime" when_propCreator
is not null anymore.