-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Multiple invocation of the TypeAdapter.nullSafe
method causes the given type adapter to be wrapped recursively.
#2729
Comments
At least to me that sounds reasonable, thanks for this suggestion! Would you like to implement it? Regarding the unit test, maybe it would be easiest to check something like this: TypeAdapter<?> adapter = ...;
TypeAdapter<?> nullSafeAdapter = adapter.nullSafe();
assertThat(nullSafeAdapter.nullSafe()).isSameInstanceAs(nullSafeAdapter); |
from returning nested null-safe type adapters (#2729)
Problem solved by the feature
Avoid recursive null-safe wrapping.
Feature description
Currently the
TypeAdapter.nullSafe()
method is implemented like this:As seen, the
TypeAdapter.nullSafe
method is not guarded with its own type check, thus invoking theTypeAdapter.nullSafe
method multiple times wraps the type adapter in the null-safe wrapper multiple times.Currently it passes the following unit test(s):
Alternatives / workarounds
The null-safe type adapter class would probably be extracted as a private named class, say
NullSafeTypeAdapter
. Then the null-safe wrapper method would be able to check if the given type adapterinstanceof
s against theNullSafeTypeAdapter
, and if it'strue
simply return it as it is already wrapped in the null-safe type adapter. Otherwise the given type adapter instance would be wrapped in theNullSafeTypeAdapter
class, thus once passing the following assertion:This wouldn't probably be any significant improvement as the use site in most code, I believe, is controlled and uses
TypeAdapter.nullSafe()
once, so there are no recursive calls as it suggested withexpectedSafeCount
in the unit test above. But it would be probably be useful for more advanced uses ofTypeAdapter
(thus its chaining) if any. This change would be also backwards-compatible.The text was updated successfully, but these errors were encountered: