-
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
Add NullSafeTypeAdapter to prevent TypeAdapter.nullSafe() from returning nested null-safe type adapters (#2729) #2731
Conversation
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.
Thanks a lot!
I have added a few comments, but feel free to consider them only suggestions. Hopefully they are useful.
Also, don't worry too much about the Git history of this PR branch; the commits will be squashed on merge anyway.
} else { | ||
TypeAdapter.this.write(out, value); | ||
} | ||
if (!NullSafeTypeAdapter.class.isInstance(this)) { |
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.
Minor, but why not !(this instanceof NullSafeTypeAdapter)
?
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.
Yeah, I had instanceof
in mind, but not sure how it all works as of today. The rule org.apache.maven.enforcer.rules.version.RequireJavaVersion
fails with
Detected JDK version 21 (JAVA_HOME=/opt/.bin/jdk-21) is not in the allowed range [11,18)
If the type check is changed to !(this instanceof NullSafeTypeAdapter)
, then:
- for JDK 11, it fails with
/src/main/java/com/google/gson/TypeAdapter.java:[292,27] illegal generic type for instanceof
; - for JDK 17, it fails with
/src/main/java/com/google/gson/TypeAdapter.java:[292,16] reifiable types in instanceof are not supported in -source 7
.
The Class.instanceOf
check works for all. However, according to the source code, it can work with TypeAdapter<?>
at least in GsonBuilder
.
I seem to have some lack of the Java language knowledge. Please suggest.
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.
Ah sorry, it seems !(this instanceof NullSafeTypeAdapter)
is not possible because NullSafeTypeAdapter
is a non-static inner class (and the enclosing class TypeAdapter<T>
is generic?).
The compiler error message is really not helpful, but in Eclipse IDE the error message included the suggested fix, qualify with the enclosing type: TypeAdapter.NullSafeTypeAdapter
. Can you please try this instead?
if (!NullSafeTypeAdapter.class.isInstance(this)) { | |
if (!(this instanceof TypeAdapter.NullSafeTypeAdapter)) { |
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.
I've never encountered this error before preferring static inner classes to capture as few as possible, and now I see there are still "good-to-knows". But no doubt, isAssignable
looked really weird and unnatural. Thank you!
} | ||
|
||
private static final TypeAdapter<String> assertionErrorAdapter = | ||
new TypeAdapter<String>() { |
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.
Minor: String
can probably be omitted here since it can be inferred
new TypeAdapter<String>() { | |
new TypeAdapter<>() { |
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.
If you're really fine with changing from TypeAdapter<String>
to TypeAdapter<>
, I'll change <String>
to <>
.
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.
Minor:
String
can probably be omitted here since it can be inferred
My bad: I seem to have missed the line change I made accidentally. Also I removed the assertionErrorAdapter
TypeAdapter
type from <String>
to <>
-- I saw the diamond operator has been in use at least in this test class.
new TypeAdapter<>() { | ||
new TypeAdapter<String>() { |
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.
I think this is not needed since we compile the test code with Java 11.
Though IDEs sometimes have problems detecting this, and erroneously assume Java 7 is used for test compilation as well.
return this; | ||
} | ||
|
||
private final class NullSafeTypeAdapter extends TypeAdapter<T> { |
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.
As suggested in #2729 (comment), do you want to take the chance and implement toString
as well. I think at least some other adapters implement it, and it would then make it easier to see which adapter is wrapped by this one.
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 seem to have misterstood your toString()
suggestion earlier leaving it out of scope (I mistakenly assumed it leads to a stack overflow error, but it doesn't).
Could you please suggest the NullSafeTypeAdapter.toString()
pattern you'd like to see?
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.
As suggested in #2729 (comment), do you want to take the chance and implement
toString
as well. I think at least some other adapters implement it, and it would then make it easier to see which adapter is wrapped by this one.
I applied the NullSafeTypeAdapter[%s]
pattern you suggested in #2729 (comment).
And could you please edit the description of the pull request to say "Closes #2729"? Otherwise GitHub does not understand this, and will keep the issue open after merge, unless they are linked manually. |
Do you mean to add Maybe I'm misusing git but |
Yes, just edit the description of your pull request text and add that anywhere. See for example #2704 which used "Resolves #...". GitHub recognizes certain keywords and in that case will automatically close the linked issue. You can also link issue and pull request manually1, but it is easier if the creator of the pull request directly does this themselves through the keywords. Otherwise it is easy to forget manually linking the issue and pull request. That works as well if you write it in the commit message (and the commit is then merged), but personally I avoid this (unless the contribution guide of a project requests it) since for each commit this already shows in the GitHub UI for the issue a "added a commit to ... that referenced this issue" message, which can be rather distracting. So I prefer to only add it to the pull request description.
It seems that just creates a regular "mention" without having any other effect. See for example #2729 which you referenced: (No worries though about the multiple commit mentions there.) Footnotes
|
The funny thing is that I thought you meant the git commit message, not the PR description, and this is why I referenced the git-rev-list command output that produced empty result. And now I also see why it is not a good idea for GitHub, kind of shame on GitHub from this perspective. Thank you for the comprehensive explanation! |
from returning nested null-safe type adapters (#2729)
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.
@eamonnmcmanus, what do you think about this pull request?
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.
Thanks for doing this!
…ip ci] Bumps [com.google.code.gson:gson](https://github.com/google/gson) from 2.11.0 to 2.12.1. Release notes *Sourced from [com.google.code.gson:gson's releases](https://github.com/google/gson/releases).* > Gson 2.11.1 > ----------- > > The only difference between this release and 2.11.0 is that OSGi declarations in the Gson jar now specify that `com.google.errorprone.annotations` is an optional dependency, not a required one. If you do not use OSGi then there is no effective change. > > Gson 2.12.0 > ----------- > > What's Changed > -------------- > > The biggest change is that we no longer support Java 7. People who still need to run on Java 7 will need to use an earlier version of Gson. > > Other changes: > > * Allow registering adapters for `JsonElement` again by [`@Marcono1234`](https://github.com/Marcono1234) in [google/gson#2789](https://github.com/google/gson/pull/2789) > * Add nesting limit for `JsonReader` by [`@Marcono1234`](https://github.com/Marcono1234) in [google/gson#2588](https://github.com/google/gson/pull/2588) > * Add `@CheckReturnValue` to our packages. by [`@cpovirk`](https://github.com/cpovirk) in [google/gson#2693](https://github.com/google/gson/pull/2693) > * Add NullSafeTypeAdapter to prevent TypeAdapter.nullSafe() from returning nested null-safe type adapters ([#2729](https://github.com/google/gson/issues/2729)) by [`@lyubomyr-shaydariv`](https://github.com/lyubomyr-shaydariv) in [google/gson#2731](https://github.com/google/gson/pull/2731) > * Support Properties subclasses in GsonTypes.getMapKeyAndValueTypes by [`@panic08`](https://github.com/panic08) in [google/gson#2758](https://github.com/google/gson/pull/2758) > * Enforce rawType to be a Class in ParameterizedTypeImpl by [`@panic08`](https://github.com/panic08) in [google/gson#2759](https://github.com/google/gson/pull/2759) > * Remove `AccessController` usage for enum adapter by [`@Marcono1234`](https://github.com/Marcono1234) in [google/gson#2704](https://github.com/google/gson/pull/2704) > * Fix typeArguments array not being cloned when resolving ParameterizedType with changed owner by [`@TBlueF`](https://github.com/TBlueF) in [google/gson#2706](https://github.com/google/gson/pull/2706) > * Remove duplicated declaration of required OSGi execution environment by [`@HannesWell`](https://github.com/HannesWell) in [google/gson#2711](https://github.com/google/gson/pull/2711) > * Move bnd.bnd file configuration into 'bnd' element of bnd-maven-plugin by [`@HannesWell`](https://github.com/HannesWell) in [google/gson#2712](https://github.com/google/gson/pull/2712) > * Move enum and `JsonElement` adapter classes to separate class files by [`@Marcono1234`](https://github.com/Marcono1234) in [google/gson#2727](https://github.com/google/gson/pull/2727) > * EnumTypeAdapter constructor optimization by [`@esaulpaugh`](https://github.com/esaulpaugh) in [google/gson#2734](https://github.com/google/gson/pull/2734) > * OSGi / bnd: Remove the self-Import of gson.annotations by [`@chrisrueger`](https://github.com/chrisrueger) in [google/gson#2735](https://github.com/google/gson/pull/2735) > > New Contributors > ---------------- > > * [`@cpovirk`](https://github.com/cpovirk) made their first contribution in [google/gson#2693](https://github.com/google/gson/pull/2693) > * [`@jabagawee`](https://github.com/jabagawee) made their first contribution in [google/gson#2701](https://github.com/google/gson/pull/2701) > * [`@TBlueF`](https://github.com/TBlueF) made their first contribution in [google/gson#2706](https://github.com/google/gson/pull/2706) > * [`@HannesWell`](https://github.com/HannesWell) made their first contribution in [google/gson#2711](https://github.com/google/gson/pull/2711) > * [`@esaulpaugh`](https://github.com/esaulpaugh) made their first contribution in [google/gson#2734](https://github.com/google/gson/pull/2734) > * [`@chrisrueger`](https://github.com/chrisrueger) made their first contribution in [google/gson#2735](https://github.com/google/gson/pull/2735) > * [`@panic08`](https://github.com/panic08) made their first contribution in [google/gson#2756](https://github.com/google/gson/pull/2756) > > **Full Changelog**: <google/gson@gson-parent-2.11.0...gson-parent-2.12.0> Commits * [`29e3d1d`](google/gson@29e3d1d) [maven-release-plugin] prepare release gson-parent-2.12.1 * [`be456cf`](google/gson@be456cf) Make the import of com.google.errorprone optional ([#2795](https://github.com/google/gson/issues/2795)) * [`b2e26fa`](google/gson@b2e26fa) Bump the github-actions group with 3 updates ([#2785](https://github.com/google/gson/issues/2785)) * [`10bdd6d`](google/gson@10bdd6d) Simplify collection type adapters slightly. ([#2791](https://github.com/google/gson/issues/2791)) * [`ab9c54f`](google/gson@ab9c54f) [maven-release-plugin] prepare for next development iteration * [`aaf7a12`](google/gson@aaf7a12) [maven-release-plugin] prepare release gson-parent-2.12.0 * [`a2b1c3c`](google/gson@a2b1c3c) Allow registering adapters for `JsonElement` again ([#2789](https://github.com/google/gson/issues/2789)) * [`e5dce84`](google/gson@e5dce84) Bump the maven group with 8 updates ([#2784](https://github.com/google/gson/issues/2784)) * [`84e5f16`](google/gson@84e5f16) Bump the maven group with 7 updates ([#2777](https://github.com/google/gson/issues/2777)) * [`9f3e577`](google/gson@9f3e577) Bump the github-actions group with 2 updates ([#2778](https://github.com/google/gson/issues/2778)) * Additional commits viewable in [compare view](google/gson@gson-parent-2.11.0...gson-parent-2.12.1) [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- Dependabot commands and options You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually - `@dependabot show ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Add
NullSafeTypeAdapter
to preventTypeAdapter.nullSafe()
from returning nested null-safe type adapters (#2729)Purpose / Description
Resolves #2729.
Checklist
This is automatically checked by
mvn verify
, but can also be checked on its own usingmvn spotless:check
.Style violations can be fixed using
mvn spotless:apply
; this can be done in a separate commit to verify that it did not cause undesired changes.null
@since $next-version$
(
$next-version$
is a special placeholder which is automatically replaced during release)TestCase
)mvn clean verify javadoc:jar
passes without errorsCloses #2729.