Fix eager runtime TypeAdapter lookup discarding usable declared adapters - #3123
jaideeppyne wants to merge 3 commits into
Conversation
Do not let JsonIOException from building an unused reflective runtime-type adapter escape when a non-reflective declared-type adapter would be preferred. Fixes google#3122.
|
Can you please run |
|
Ran |
Marcono1234
left a comment
There was a problem hiding this comment.
Thanks for the changes!
I have added a few comments with additional suggestions; hopefully they are useful. But the project members might add additional review comments.
There has been further discussion in #3122 after you had created the PR here, but the proposed changes there will probably have to be discussed with a project member first.
If you want feel free to include those changes, or leave them out for now and then in the future they can be included in a follow-up PR (by you or anyone else).
|
There seems to be a general issue with this exception catching approach (see #3122 (comment) and the previous comment): |
|
Thanks for flagging this. I agree the current broad JsonIOException catch cannot distinguish a direct ReflectiveTypeAdapterFactory failure for the requested runtime type from a nested reflective failure reached through a user factory; silently falling back in the latter case would be wrong. The issue discussion now has two materially different designs (a typed creation failure carrying its TypeToken, or a dedicated adapter lookup that stops before the reflective factory), and either expands this PR beyond the original fix. I am leaving that design choice to a Gson project member rather than selecting a new internal/public contract unilaterally. The current tests and cleanup suggestions are addressed; I can revise the implementation once maintainers choose the intended lookup contract. |
Purpose
Fixes #3122 —
TypeAdapterRuntimeTypeWrappereagerly callscontext.getAdapter(...)for the value's runtime type even when that adapter would be discarded in favor of a non-reflective declared-type adapter. If reflective construction of the runtime-type adapter fails (e.g. inaccessible JDK-internal implementation classes under the module system), the exception escapes and serialization of arrays/collections fails even though a registered interface adapter would have worked.Description
Wrap the runtime-type
getAdaptercall and only rethrowJsonIOExceptionwhen the declared-type delegate is reflective (i.e. when the runtime-type adapter would actually have been chosen). Selection preference order is unchanged.Adds a regression test using a registered non-reflective
WildcardTypeadapter with array/collection serialization — the scenario from the issue report.Locally verified:
mvn -pl gson test -Dtest=TypeAdapterRuntimeTypeWrapperTest(8 tests, including the new one). The new test fails on unfixed code with the sameJsonIOExceptiondescribed in #3122.Checklist
null@since $next-version$mvn clean verify javadoc:jarpasses without errors (ran targeted unit test suite locally)Note: Google CLA will be required for this contribution (https://cla.developers.google.com/).