Fix state mutation bug in Get-AutotaskAPIResource#75
Open
BackupNerd wants to merge 2 commits intoKelvinTegelaar:masterfrom
Open
Fix state mutation bug in Get-AutotaskAPIResource#75BackupNerd wants to merge 2 commits intoKelvinTegelaar:masterfrom
BackupNerd wants to merge 2 commits intoKelvinTegelaar:masterfrom
Conversation
- Fixed line 57 which mutated global $Script:Queries table - Changed from direct property mutation to object cloning - Prevents POST operations from failing with 404 after GET calls - Affects all resources with /query endpoints (Products, etc.) - Backward compatible - identical behavior without side effects - Includes comprehensive test suite (Test-ModuleFix.ps1)
- Test-ModuleFix.ps1 validates the bug fix - Tests verify $Script:Queries table remains unchanged after GET - Tests confirm POST operations succeed after GET operations - Includes before/after comparison of Queries table - All tests passed (product ID 29683482 created successfully)
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.
Pull Request: Fix State Mutation Bug in Get-AutotaskAPIResource
Summary
Fixes a critical bug where
Get-AutotaskAPIResourcemutates the global$Script:Queriestable, causing subsequent calls toNew-AutotaskAPIResourceto fail with HTTP 404 errors.Problem
Symptom
New-AutotaskAPIResource -Resource Productsfails with 404 error after callingGet-AutotaskAPIResource -Resource Products.Root Cause
In
Get-AutotaskAPIResource.ps1line 57, the code directly modifies the$Script:Queriestable:This changes the global
Nameproperty from/V1.0/Products/queryto/V1.0/Products/{PARENTID}permanently.When
New-AutotaskAPIResourcelater queries the same table:It now selects
/V1.0/Products/{PARENTID}(first match) instead of/V1.0/Products/query, which after-replace '/query', ''results in an invalid endpoint.Impact
/queryendpointsSolution
Clone the object before modification instead of mutating the shared reference:
Testing
Test Case 1: Verify No Table Corruption
Test Results
$Script:Queriestable unchanged after GET operationsSee
Test-ModuleFix.ps1for complete test suite.Files Changed
Public/Get-AutotaskAPIResource.ps1(lines 53-62)Backward Compatibility
This fix has no breaking changes:
Additional Notes
This same pattern may exist in other cmdlets. Recommend audit of:
Set-AutotaskAPIResource.ps1Remove-AutotaskAPIResource.ps1New-AutotaskBody.ps1Any code that modifies
$Script:Queriesentries should clone before mutation.Verification Steps for Reviewer
Test-ModuleFix.ps1Issue: State mutation causing 404 errors in New-AutotaskAPIResource
Fix: Clone objects instead of mutating shared
$Script:QueriestableRisk: Low - Identical behavior, better isolation
Testing: Comprehensive test suite included