fix(aip-123): prevent panic in getParentIDVariable with single-variable patterns#1565
Merged
Merged
Conversation
…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
Contributor
Author
|
@gemini-code-assist review |
Contributor
There was a problem hiding this comment.
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.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
eddiesoller
marked this pull request as ready for review
November 13, 2025 00:13
noahdietz
enabled auto-merge (squash)
November 13, 2025 18:43
noahdietz
disabled auto-merge
November 13, 2025 18:43
noahdietz
enabled auto-merge (squash)
November 13, 2025 18:43
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a panic in
getParentIDVariablewhen 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-alternaterule.Testing
Changes
getParentIDVariable()(aip0123.go)TestAIP0123_InvalidPatternsDontPanicTestResourceNameComponentsAlternatefor invalid patterns