Skip to content

docs(auth): fix broken Javadoc snippets in IdToken, Jwt, and Impersonated credentials - #15425

Draft
lqiu96 wants to merge 1 commit into
googleapis:mainfrom
lqiu96:docs/auth-0a-fix-javadoc-snippets
Draft

lqiu96 wants to merge 1 commit into
googleapis:mainfrom
lqiu96:docs/auth-0a-fix-javadoc-snippets

Conversation

@lqiu96

@lqiu96 lqiu96 commented Dec 3, 2026

Copy link
Copy Markdown
Member

No description provided.

…ated credentials

Wave 1 Auth library Javadoc snippets (AUTH-0a, separated from VerifyGoogleIdToken sample):
- Fix 6 compile/doc bugs in IdTokenCredentials Javadoc snippet.
- Fix uncompilable JwtCredentials and JwtClaims snippets and ImpersonatedCredentials snippet scope.

@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 updates and corrects Javadoc documentation and code examples across several classes, including IdTokenCredentials, ImpersonatedCredentials, JwtClaims, and JwtCredentials. The feedback suggests improving the Javadoc code snippets further: in IdTokenCredentials, reusing a single tokenCredential variable to prevent unused variable warnings and make the snippet fully copy-pasteable; and in JwtCredentials, using the specific JwtCredentials type instead of the generic Credentials to be more idiomatic.

Comment on lines +68 to +108
* IdTokenCredentials tokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider((IdTokenProvider) adcCreds)
* .setTargetAudience(targetAudience)
* .build();
*
* // for ServiceAccountCredentials
* ServiceAccountCredentials saCreds = ServiceAccountCredentials.fromStream(new FileInputStream(credPath));
* saCreds = (ServiceAccountCredentials) saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/iam"));
* IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder()
* .setIdTokenProvider(saCreds)
* .setTargetAudience(targetAudience).build();
* // For ServiceAccountCredentials
* ServiceAccountCredentials saCreds =
* ServiceAccountCredentials.fromStream(new FileInputStream(credPath));
* saCreds =
* (ServiceAccountCredentials)
* saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
* IdTokenCredentials saTokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider(saCreds)
* .setTargetAudience(targetAudience)
* .build();
*
* // for ComputeEngineCredentials
* ComputeEngineCredentials caCreds = ComputeEngineCredentials.create();
* IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder()
* .setIdTokenProvider(caCreds)
* .setTargetAudience(targetAudience)
* .setOptions(Arrays.asList(ComputeEngineCredentials.ID_TOKEN_FORMAT_FULL))
* .build();
* // For ComputeEngineCredentials
* ComputeEngineCredentials ceCreds = ComputeEngineCredentials.create();
* IdTokenCredentials ceTokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider(ceCreds)
* .setTargetAudience(targetAudience)
* .setOptions(Arrays.asList(IdTokenProvider.Option.FORMAT_FULL))
* .build();
*
* // for ImpersonatedCredentials
* ImpersonatedCredentials imCreds = ImpersonatedCredentials.create(saCreds,
* "impersonated-account@project.iam.gserviceaccount.com", null,
* Arrays.asList("https://www.googleapis.com/auth/cloud-platform"), 300);
* IdTokenCredentials tokenCredential = IdTokenCredentials.newBuilder()
* .setIdTokenProvider(imCreds)
* .setTargetAudience(targetAudience)
* .setOptions(Arrays.asList(ImpersonatedCredentials.INCLUDE_EMAIL))
* .build();
* // For ImpersonatedCredentials
* ImpersonatedCredentials imCreds =
* ImpersonatedCredentials.create(
* saCreds,
* "impersonated-account@project.iam.gserviceaccount.com",
* null,
* Arrays.asList("https://www.googleapis.com/auth/cloud-platform"),
* 300);
* IdTokenCredentials imTokenCredential =
* IdTokenCredentials.newBuilder()
* .setIdTokenProvider(imCreds)
* .setTargetAudience(targetAudience)
* .setOptions(Arrays.asList(IdTokenProvider.Option.INCLUDE_EMAIL))
* .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.

medium

In this Javadoc snippet, multiple distinct variables are declared (saTokenCredential, ceTokenCredential, imTokenCredential) but never used, while tokenCredential is declared once at the beginning and then used at the end of the snippet. This can lead to unused variable warnings or compilation errors if a developer copy-pastes the entire block. Reusing a single tokenCredential variable across all examples makes the snippet cleaner, more cohesive, and fully copy-pasteable.

 * IdTokenCredentials tokenCredential;
 *
 * // For Application Default Credentials (as ServiceAccountCredentials)
 * // export GOOGLE_APPLICATION_CREDENTIALS=/path/to/svc.json
 * GoogleCredentials adcCreds = GoogleCredentials.getApplicationDefault();
 * if (!(adcCreds instanceof IdTokenProvider)) {
 *   throw new IllegalStateException("Credentials do not support ID tokens.");
 * }
 *
 * tokenCredential =
 *     IdTokenCredentials.newBuilder()
 *         .setIdTokenProvider((IdTokenProvider) adcCreds)
 *         .setTargetAudience(targetAudience)
 *         .build();
 *
 * // For ServiceAccountCredentials
 * ServiceAccountCredentials saCreds =
 *     ServiceAccountCredentials.fromStream(new FileInputStream(credPath));
 * saCreds =
 *     (ServiceAccountCredentials)
 *         saCreds.createScoped(Arrays.asList("https://www.googleapis.com/auth/cloud-platform"));
 * tokenCredential =
 *     IdTokenCredentials.newBuilder()
 *         .setIdTokenProvider(saCreds)
 *         .setTargetAudience(targetAudience)
 *         .build();
 *
 * // For ComputeEngineCredentials
 * ComputeEngineCredentials ceCreds = ComputeEngineCredentials.create();
 * tokenCredential =
 *     IdTokenCredentials.newBuilder()
 *         .setIdTokenProvider(ceCreds)
 *         .setTargetAudience(targetAudience)
 *         .setOptions(Arrays.asList(IdTokenProvider.Option.FORMAT_FULL))
 *         .build();
 *
 * // For ImpersonatedCredentials
 * ImpersonatedCredentials imCreds =
 *     ImpersonatedCredentials.create(
 *         saCreds,
 *         "impersonated-account@project.iam.gserviceaccount.com",
 *         null,
 *         Arrays.asList("https://www.googleapis.com/auth/cloud-platform"),
 *         300);
 * tokenCredential =
 *     IdTokenCredentials.newBuilder()
 *         .setIdTokenProvider(imCreds)
 *         .setTargetAudience(targetAudience)
 *         .setOptions(Arrays.asList(IdTokenProvider.Option.INCLUDE_EMAIL))
 *         .build();
References
  1. When calling createScoped on ServiceAccountCredentials, the returned object is of type GoogleCredentials. A cast to ServiceAccountCredentials is required if you need to access subclass-specific methods such as createWithUseJwtAccessWithScope.

* .setSubject("some-subject@example.com")
* .build();
* Credentials = JwtCredentials.newBuilder()
* Credentials credentials = JwtCredentials.newBuilder()

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.

medium

Using the specific type JwtCredentials instead of the generic Credentials is more idiomatic, matches the builder type, and avoids requiring an extra import for com.google.auth.Credentials when developers copy-paste this snippet.

Suggested change
* Credentials credentials = JwtCredentials.newBuilder()
* JwtCredentials credentials = JwtCredentials.newBuilder()

This branch has not been deployed

No deployments
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.

1 participant