fix: handle array-typed org/org_id in AuditEntry JSON unmarshaling#4204
Open
nghiack7 wants to merge 1 commit intogoogle:masterfrom
Open
fix: handle array-typed org/org_id in AuditEntry JSON unmarshaling#4204nghiack7 wants to merge 1 commit intogoogle:masterfrom
nghiack7 wants to merge 1 commit intogoogle:masterfrom
Conversation
GitHub's Enterprise audit-log API sometimes returns 'org' as a JSON array of strings and 'org_id' as a JSON array of integers, deviating from the documented scalar types. This caused an unmarshal error: json: cannot unmarshal array into Go struct field ... of type string Fix UnmarshalJSON to capture 'org' and 'org_id' as json.RawMessage, then decode each as scalar-or-array: - string arrays are joined with ', ' - int64 arrays use the first element Add two helpers (unmarshalStringOrStringArray, unmarshalInt64OrInt64Array) and table-driven tests covering scalar, single-element array, and multi-element array payloads. Fixes google#3488
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4204 +/- ##
==========================================
- Coverage 93.71% 93.67% -0.05%
==========================================
Files 209 209
Lines 19772 19805 +33
==========================================
+ Hits 18529 18552 +23
- Misses 1046 1051 +5
- Partials 197 202 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Problem
The GitHub Enterprise audit-log API sometimes returns
orgas a JSON array of strings andorg_idas a JSON array of integers, deviating from the documented scalar types. This causesGetAuditLog/GetAuditLog(Enterprise) to return an unmarshal error:Reported on v66 and v69. See #3488.
Root cause
AuditEntry.UnmarshalJSONdelegates tojson.Unmarshalvia atype entryAlias AuditEntryalias. When the API returns"org": ["a", "b"]the decoder fails becauseOrgis typed*string.Fix
In
UnmarshalJSON, captureorgandorg_idasjson.RawMessagebefore the main decode pass, then normalise each:stringarrays are joined with", "(preserves all org names)int64arrays use the first element (mirrors how callers would use it)Two unexported helpers (
unmarshalStringOrStringArray,unmarshalInt64OrInt64Array) encapsulate the logic so the core unmarshal flow stays readable.Tests
Added table-driven tests in
orgs_audit_log_test.go:org_as_scalar_string"org":"myorg""myorg"org_as_single_element_array"org":["myorg"]"myorg"org_as_multi_element_array"org":["org1","org2","org3"]"org1, org2, org3"org_id_as_scalar"org_id":4242org_id_as_array"org_id":[42,43,44]42(first element)All existing audit-log tests continue to pass.