Skip to content

feat(gapic-generator-java): Extract resource name heuristicly#12207

Open
blakeli0 wants to merge 18 commits intomainfrom
feature/heuristic-filter
Open

feat(gapic-generator-java): Extract resource name heuristicly#12207
blakeli0 wants to merge 18 commits intomainfrom
feature/heuristic-filter

Conversation

@blakeli0
Copy link
Copy Markdown
Contributor

@blakeli0 blakeli0 commented Mar 26, 2026

Extract resource name heuristically if there are no resource_reference configured for a request.

This PR adds logic to apply the heuristic resource name extraction if the proto package matches certain patterns:

  • google.cloud.compute.**
  • google.cloud.sql.**
  • google.cloud.bigquery.**

The exact logic is described in go/client-libraries:destination-resource-name. In a nutshell, a canonical resource name is extracted from the template by finding the version literal, then finding the last binding that is a literal/binding pair or named binding, and then extracting the segments between the version literal and the last binding.

The logic is mostly implemented in PathTemplate, and used by AbstractTransportServiceStubClassComposer#createResourceNameExtractorClassInstance.

The generated code would be called in the same way as a regular resourceNameExtractor by TracedUnaryCallable.

@blakeli0 blakeli0 requested a review from a team as a code owner March 26, 2026 04:19
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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 implements a new heuristic for extracting resource names during GAPIC client library generation, allowing for more intelligent resource path construction. This heuristic is selectively enabled for specific proto packages to ensure controlled application. Additionally, the changes improve the robustness of endpoint context parsing, preventing failures when encountering malformed server address or port information.

Highlights

  • Heuristic Resource Name Extraction: Introduced a new heuristic for extracting resource names during GAPIC client library generation, allowing for more intelligent resource path construction based on known resource literals.
  • Proto Package Filtering: Implemented filtering for the new heuristic resource name extraction, enabling it only for specific proto packages (e.g., google.cloud.compute, google.cloud.sql, google.cloud.bigquery) to ensure controlled application.
  • PathTemplate Enhancements: Added new utility methods to PathTemplate to identify resource literals and construct canonical resource names, supporting the new heuristic extraction logic.
  • Endpoint Context Robustness: Enhanced the EndpointContext parsing logic to gracefully handle invalid server address or port formats, preventing failures and improving client stability.
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.

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.

@blakeli0 blakeli0 changed the title Filter heuristic resource name extraction by proto package feat(gapic-generator-java): filter heuristic resource name extraction by proto package Mar 26, 2026
Copy link
Copy Markdown
Contributor

@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 functionality to the PathTemplate class, adding getResourceLiterals and getCanonicalResourceName methods to extract resource literals and canonical resource names from path templates, including support for complex patterns and named bindings. The GAPIC generator has been updated to leverage these new PathTemplate capabilities, enabling automatic generation of resource name extractors for client stubs, especially for methods in heuristic-enabled packages that lack explicit resource references. This involved defining AIP standard methods and heuristic-enabled packages, and updating associated golden files and proto definitions. Additionally, the EndpointContext in GAX was improved to handle invalid endpoint parsing for server address and port more gracefully, treating these as non-critical for client requests. Feedback includes refactoring the complex getCanonicalResourceName method, removing a dead keepBinding variable, correcting typos in test method names, addressing a debugging test method, fixing a typo in service.protoPakkage(), and improving the robustness of binding variable checks in createResourceNameExtractorClassInstance from String.contains() to a more precise regex.

@blakeli0 blakeli0 changed the title feat(gapic-generator-java): filter heuristic resource name extraction by proto package feat(gapic-generator-java): Extract resource name heuristicly Mar 26, 2026
Copy link
Copy Markdown
Contributor

@diegomarquezp diegomarquezp left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Added a few comments.

Comment on lines +315 to +320
if (inBinding) {
canonicalSegments.add(value);
} else if (i + 1 < segments.size() && segments.get(i + 1).kind() == SegmentKind.BINDING) {
canonicalSegments.add(value);
}
}
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.

I'm probably wrong, but what I read here is two ways to add a literal to the canonical segments:

  • previous node is a binding (inBinding) and we are now in a literal
  • next node is a binding and we are now in a literal

From the javadoc, A resource literal is a literal followed by a binding. Why are we also considering binding first then literal as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added more comments. It is for a special case named binding, which looks like {name=projects/*/locations/*}

* literal/binding pair or named binding, and then extracting the segments between the version
* literal and the last binding.
*/
// For example, projects/{project} is a literal/binding pair. {bar=projects/*/locations/*/bars/*}
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.

Can we also add a comment line saying what would be the output given an exemplary template?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added

if (seg.kind() == SegmentKind.LITERAL) {
String value = seg.value();
if (value.matches("^v\\d+[a-zA-Z0-9]*$")) {
startIndex = i + 1;
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.

Is this if statement guaranteed to end up setting the start index?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, if there are not versions found, the start index would be 0.

int literalCountInBinding = 0;
int currentBindingStartIndex = -1;

for (int i = 0; i < segments.size(); i++) {
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.

+1 to gemini's comment on complexity of this method and splitting into private helpers. We could also add comments explaining the purpose of each if or nested for loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I significantly simplified the logic and added more comments now. PTAL

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