Skip to content

Conversation

@robertvoinescu-work
Copy link
Contributor

This change introduces new overloads to the that allow specifying the credential type as a string parameter. This provides a way to create credentials from JSON data without having to specify the type statically.

This change introduces new overloads to the  that allow specifying the credential type as a string parameter. This provides a way to create credentials from JSON data without having to specify the type statically.
@robertvoinescu-work robertvoinescu-work requested a review from a team as a code owner October 28, 2025 19:24
@gemini-code-assist
Copy link

Summary of Changes

Hello @robertvoinescu-work, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant enhancements to the CredentialFactory by adding new method overloads that allow specifying the credential type as a string parameter. This change provides a more flexible and dynamic way to create credentials from JSON data, removing the need for static type declarations in certain scenarios. It also improves the robustness of JSON deserialization by centralizing error handling and includes thorough unit tests to validate the new functionality.

Highlights

  • Dynamic Credential Creation: New CredentialFactory overloads (FromFileAsync, FromFile, FromStreamAsync, FromStream, FromJson) now accept a credentialType string, enabling dynamic specification of credential types from JSON data.
  • Centralized Error Handling: Introduced HandleDeserializationErrors and HandleDeserializationErrorsAsync methods to consolidate and improve error handling for JSON deserialization operations within the CredentialFactory.
  • Explicit Type Validation: An internal FromJsonParameters overload was added to enforce explicit validation of the credentialType against the type found in the JSON data, preventing mismatches.
  • Code Clarity: The internal helper method CreateCredential was renamed to CreateCredentialFromParameters for better descriptive clarity.
  • Comprehensive Testing: Extensive unit tests have been added to GoogleCredentialTests.cs to cover the new CredentialFactory overloads, including both successful credential creation and expected failure scenarios.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

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

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 introduces new overloads to CredentialFactory that allow specifying the credential type as a string, which is a useful enhancement. The implementation is clean, with good refactoring to reduce code duplication, particularly in error handling for JSON deserialization. The accompanying tests are thorough, covering both success and failure cases for the new functionality. I have one high-severity comment regarding a missing null check that could lead to an unhandled exception.

Comment on lines +181 to +186
internal static GoogleCredential FromJsonParameters(JsonCredentialParameters credentialParameters, string credentialType)
{
if (credentialParameters.Type != credentialType)
{
throw new InvalidOperationException($"Json data has type = '{credentialParameters.Type}', but type = '{credentialType}' was expected.");
}

Choose a reason for hiding this comment

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

high

This method does not handle the case where credentialParameters is null. If a null value is passed (which can happen with empty or "null" JSON input), an unhandled NullReferenceException will be thrown when accessing credentialParameters.Type.

To prevent this, you should add a null check at the beginning of the method. Using credentialParameters.ThrowIfNull(nameof(credentialParameters)) would be consistent with other methods in this class and provide a more informative ArgumentNullException.

    internal static GoogleCredential FromJsonParameters(JsonCredentialParameters credentialParameters, string credentialType)
    {
        credentialParameters.ThrowIfNull(nameof(credentialParameters));
        if (credentialParameters.Type != credentialType)
        {
            throw new InvalidOperationException($"Json data has type = '{credentialParameters.Type}', but type = '{credentialType}' was expected.");
        }

@@ -15,6 +15,7 @@ limitations under the License.
*/

using System;
using System.Diagnostics;
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this for?

/// Thrown if the <paramref name="credentialType"/> is unrecognized,
/// or if the credential data is incompatible with the requested type.
/// </exception>
internal static GoogleCredential FromJsonParameters(JsonCredentialParameters credentialParameters, string credentialType)
Copy link
Contributor

Choose a reason for hiding this comment

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

This can be private.

{
return deserializationFunc();
}
catch (Exception e) when (e is Newtonsoft.Json.JsonException || e is IOException)
Copy link
Contributor

Choose a reason for hiding this comment

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

We were not filtering exceptions earlier. And we were catching them for everything. That last part was wrong, even with respect to the methods that we deprecated which were only catching the exceptions for the deserialization itself. We need to add tests to make certain that we don't mask other errors with this one.

Also, I'm not a fan of these methods, if I'm honest. I think they make the code harder to read than if we had the explicit try catch everywhere we needed it (and just wrapping the actual deserialization). I think if we are worried about reusing the exception message everywhere, we can put that in a constant.

And if these methods are staying aroung, they shouldn't be called Handle..., they should be called Wrap....

{
if (credentialParameters.Type != credentialType)
{
throw new InvalidOperationException($"Json data has type = '{credentialParameters.Type}', but type = '{credentialType}' was expected.");
Copy link
Contributor

Choose a reason for hiding this comment

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

This exception message should be the same as the one we use on the generic version of the methods.

/// <param name="credentialType">The type of credential to be loaded. Valid strings can be found in <see cref="JsonCredentialParameters"/>.</param>
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
/// <returns>A task that will be completed with the created credential.</returns>
public static async Task<GoogleCredential> FromStreamAsync(Stream stream, string credentialType, CancellationToken cancellationToken) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm very tempted to keep these private. We only need the FromFile ones for Spanner et al.

/// <param name="json">The string that contains the JSON credential data.</param>
/// <param name="credentialType">The type of credential to be loaded.</param>
/// <returns>The created credential.</returns>
public static GoogleCredential FromJson(string json, string credentialType) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need these at the moment. We should remove them and just add them if there's a user need.

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.

2 participants