Skip to content

feat(auth): add JSpecify Null annotations to Auth#13842

Draft
nnicolee wants to merge 8 commits into
mainfrom
feat/jspecify-auth
Draft

feat(auth): add JSpecify Null annotations to Auth#13842
nnicolee wants to merge 8 commits into
mainfrom
feat/jspecify-auth

Conversation

@nnicolee

@nnicolee nnicolee commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR migrates the Google Auth Library for Java (google-auth-library-java) to use JSpecify 1.0.0 nullability annotations (@NullMarked and @Nullable), replacing the previous javax.annotation references.

Key Changes

JSpecify 1.0.0 Annotation Adoption in Auth:

  • NullMarked Semantics: Applied @NullMarked at the class and package levels across core auth classes (in credentials, oauth2_http, appengine, and cab-token-generator modules) to define non-null by default semantics.
  • Annotation Replacement: Replaced imports of javax.annotation.Nullable with org.jspecify.annotations.Nullable.
  • Dependency Management: Updated Maven pom.xml files and Bazel BUILD.bazel configurations to declare and manage the org.jspecify:jspecify dependency.

Verification Results

gemini-code-assist[bot]

This comment was marked as off-topic.

@sonarqubecloud

This comment was marked as outdated.

@sonarqubecloud

This comment was marked as outdated.

@nnicolee
nnicolee force-pushed the feat/jspecify-auth branch from 76c939a to afdc8c6 Compare July 21, 2026 04:39
@nnicolee
nnicolee force-pushed the feat/jspecify-auth branch from afdc8c6 to 432c5d0 Compare July 21, 2026 04:43
@nnicolee
nnicolee requested a review from lqiu96 July 21, 2026 15:12
Comment thread google-auth-library-java/pom.xml Outdated
@lqiu96

lqiu96 commented Jul 22, 2026

Copy link
Copy Markdown
Member

/gemini review

gemini-code-assist[bot]

This comment was marked as resolved.

@lqiu96 lqiu96 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 22, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jul 22, 2026
@nnicolee

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request integrates JSpecify nullness annotations (@NullMarked and @Nullable) across multiple modules in the google-auth-library-java repository to enhance static analysis and null safety. The review feedback correctly identifies a critical issue in TokenVerifier.java where @Nullable is applied to type parameters of Guava's ImmutableMap and ImmutableMap.Builder. Because Guava's immutable collections do not allow null values, these changes could lead to runtime NullPointerExceptions. The reviewer advises reverting these map type parameters to PublicKey and adding explicit null checks before inserting elements into the builder.

private final PublicKey publicKey;
private final Clock clock;
private final LoadingCache<String, Map<String, PublicKey>> publicKeyCache;
private final LoadingCache<String, Map<String, @Nullable PublicKey>> publicKeyCache;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since Guava's ImmutableMap does not permit null values, the cached map should not contain @Nullable values. Please keep the type parameter as PublicKey.

Suggested change
private final LoadingCache<String, Map<String, @Nullable PublicKey>> publicKeyCache;
private final LoadingCache<String, Map<String, PublicKey>> publicKeyCache;


/** Custom CacheLoader for mapping certificate urls to the contained public keys. */
static class PublicKeyLoader extends CacheLoader<String, Map<String, PublicKey>> {
static class PublicKeyLoader extends CacheLoader<String, Map<String, @Nullable PublicKey>> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since Guava's ImmutableMap does not permit null values, the map returned by the loader should not contain @Nullable values. Please keep the type parameter as PublicKey.

Suggested change
static class PublicKeyLoader extends CacheLoader<String, Map<String, @Nullable PublicKey>> {
static class PublicKeyLoader extends CacheLoader<String, Map<String, PublicKey>> {


@Override
public Map<String, PublicKey> load(String certificateUrl) throws Exception {
public Map<String, @Nullable PublicKey> load(String certificateUrl) throws Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since Guava's ImmutableMap does not permit null values, the map returned by load should not contain @Nullable values. Please keep the type parameter as PublicKey.

Suggested change
public Map<String, @Nullable PublicKey> load(String certificateUrl) throws Exception {
public Map<String, PublicKey> load(String certificateUrl) throws Exception {

jwks = response.parseAs(JsonWebKeySet.class);

ImmutableMap.Builder<String, PublicKey> keyCacheBuilder = new ImmutableMap.Builder<>();
ImmutableMap.Builder<String, @Nullable PublicKey> keyCacheBuilder = new ImmutableMap.Builder<>();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Guava's ImmutableMap and its builder do not permit null keys or values. Declaring the builder as ImmutableMap.Builder<String, @Nullable PublicKey> is problematic because if buildPublicKey(key) returns null and is added to the builder, it will throw a NullPointerException at runtime. If null values are filtered out before being added to the builder, then the map will never contain null values, and the type parameter should be PublicKey instead of @Nullable PublicKey to accurately reflect the map's contents and avoid type-safety issues. Please revert the map and builder type parameters to PublicKey (without @Nullable).

Suggested change
ImmutableMap.Builder<String, @Nullable PublicKey> keyCacheBuilder = new ImmutableMap.Builder<>();
ImmutableMap.Builder<String, PublicKey> keyCacheBuilder = new ImmutableMap.Builder<>();

}

ImmutableMap<String, PublicKey> keyCache = keyCacheBuilder.build();
ImmutableMap<String, @Nullable PublicKey> keyCache = keyCacheBuilder.build();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since Guava's ImmutableMap does not permit null values, the built map should not contain @Nullable values. Please keep the type parameter as PublicKey.

Suggested change
ImmutableMap<String, @Nullable PublicKey> keyCache = keyCacheBuilder.build();
ImmutableMap<String, PublicKey> keyCache = keyCacheBuilder.build();

}

private PublicKey buildPublicKey(JsonWebKey key)
private @Nullable PublicKey buildPublicKey(JsonWebKey key)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since buildPublicKey is now annotated with @Nullable and can return null, please ensure that the caller explicitly checks for null before adding the returned key to keyCacheBuilder. Guava's ImmutableMap.Builder will throw a NullPointerException if a null value is added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants