Fix #1028: Gson.getDelegateAdapter not working correctly for @JsonAdapter field #1690
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.
Previously
TypeAdapterFactory
classes specified using the@JsonAdapter
annotation for fields were only be able to get type adapters created by the enum and reflection-based factories when callingGson.getDelegateAdapter(...)
.This pull request changes it so they are able to get type adapters created by all factories registered on the Gson instance.
The behavior for
@JsonAdapter
on types (in contrast to fields) remains unchanged.Note that this is an backwards incompatible change in case someone relies on
Gson.getDelegateAdapter(...)
returning a reflection-based type adapter. However, this might be a rare corner case and it appears keeping this behavior while also fixing #1028 is not possible.Also note that this solution breaks if the created
TypeAdapterFactory
leaks a reference to itself, e.g. by storing a reference to itself in a static field or adding it to a static collection, and then later callinggetDelegateAdapter
. Or ifTypeAdapterFactory.create(...)
callsgetDelegateAdapter
from a separate thread. Though both cases are likely rare corner cases.