Skip to content

fix(aip-123): prevent panic in getParentIDVariable with single-variable patterns#1565

Merged
noahdietz merged 2 commits into
googleapis:mainfrom
eddiesoller:fix/aip123-panic
Nov 13, 2025
Merged

fix(aip-123): prevent panic in getParentIDVariable with single-variable patterns#1565
noahdietz merged 2 commits into
googleapis:mainfrom
eddiesoller:fix/aip123-panic

Conversation

@eddiesoller

Copy link
Copy Markdown
Contributor

Summary

Fixes a panic in getParentIDVariable when processing resource patterns with only one variable that aren't root-level or singleton patterns.

Problem

The function attempted to access variables[len(variables)-2], which results in index -1 (panic) when there's only one variable. This occurred with patterns like "nonCollectionPrefix/projects/{project}".

Solution

Added a bounds check to return empty string when there are fewer than 2 variables. Invalid patterns are still caught by the resource-name-components-alternate rule.

Testing

  • Added integration test to verify no panic on invalid patterns
  • Added unit tests for validation rule coverage
  • All existing tests pass

Changes

  • Add bounds check in getParentIDVariable() (aip0123.go)
  • Add integration test TestAIP0123_InvalidPatternsDontPanic
  • Add unit tests in TestResourceNameComponentsAlternate for invalid patterns

…le patterns

Fixes a panic when processing resource patterns with only one variable
that aren't root-level or singleton patterns (e.g., "prefix/collection/{id}").

The getParentIDVariable function attempted to access variables[len(variables)-2],
resulting in index -1 when there's only one variable. Added bounds check to
return empty string for these cases.

Invalid patterns are caught by the resource-name-components-alternate rule,
so this fix allows validation to complete without crashing.

Changes:
- Add bounds check in getParentIDVariable() (aip0123.go)
- Add integration test TestAIP0123_InvalidPatternsDontPanic
- Add unit tests in TestResourceNameComponentsAlternate for invalid patterns
@eddiesoller

Copy link
Copy Markdown
Contributor Author

@gemini-code-assist 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 effectively addresses a panic in getParentIDVariable that occurred with specific single-variable resource patterns. The solution, which involves adding a bounds check, is correct and prevents the index out-of-bounds error. The accompanying tests are well-written; the new integration test ensures the fix prevents panics, and the new unit tests correctly verify that the problematic patterns are flagged by another validation rule. Overall, this is a solid contribution. I have one minor suggestion to improve a comment for future clarity.

Comment thread rules/aip0123/aip0123.go Outdated
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@eddiesoller
eddiesoller marked this pull request as ready for review November 13, 2025 00:13
@eddiesoller
eddiesoller requested a review from a team November 13, 2025 00:13

@noahdietz noahdietz 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.

Thanks @eddiesoller

@noahdietz
noahdietz enabled auto-merge (squash) November 13, 2025 18:43
@noahdietz
noahdietz disabled auto-merge November 13, 2025 18:43
@noahdietz
noahdietz enabled auto-merge (squash) November 13, 2025 18:43
@noahdietz
noahdietz merged commit a09770e into googleapis:main Nov 13, 2025
5 checks passed
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