Skip to content

fix: return an error instead of panicking in GetBatchResponseById - #450

Open
0z5a (0z5a) wants to merge 1 commit into
microsoftgraph:mainfrom
0z5a:fix/batch-response-by-id-panic
Open

0z5a (0z5a) wants to merge 1 commit into
microsoftgraph:mainfrom
0z5a:fix/batch-response-by-id-panic

Conversation

@0z5a

Copy link
Copy Markdown

Overview

Fixes #449.

GetBatchResponseById panicked in two cases:

  • The item has no body ("body": null or no body field), as for a 204 or 302 item. GetObjectValue returns nil, nil, and result.(T) on a nil interface panics.
  • The id is not in the batch response. GetResponseById returns nil, and *item.GetStatus() dereferences it.

With this change, an item without a body returns the zero value of T and no error, and an unknown id returns the error no response found for id: <id>. The parse error from GetObjectValue is now returned before the type assertion.

How I found it: I was reading batch_requests.go and noticed the unchecked type assertion, then ran the 204 and 302 items from the existing getDummyJSON() fixture through it.

Where this can show up in practice: a batch of DELETE requests gets 204 items back, and code that calls GetBatchResponseById for each of its request ids panics on the first one. This is my estimate; I have not seen a field report.

Notes

  • For a 204, returning nil, nil matches what the request adapter's Send returns for a 204 outside of a batch (I checked this with an httptest server).
  • For an unknown id, I chose an error rather than a zero value, because a zero value with no error could not be told apart from a 204 item. The Python core also raises in this case (No response found for id: ...).
  • A 302 item is handled like a 204 here (zero value, no error), because the function already treats every status below 400 as success.

Testing

  • Two new tests in batch_request_test.go, using the existing getDummyJSON() fixture:
    • TestGetBatchResponseByIdWithoutBody: items "4" (204) and "1" (302) return nil and no error, without panicking.
    • TestGetBatchResponseByIdUnknownId: id "99" returns no response found for id: 99, without panicking.
  • go test ./...: without the change in batch_requests.go, both new tests fail with the panics interface conversion: interface is nil, not msgraphgocore.Userable and runtime error: invalid memory address or nil pointer dereference; with it, all packages pass. TestGetResponseByIdForSuccessfulRequest and the two TestGetResponseByIdFailedRequest* tests still pass.
  • gofmt -l . is empty and go vet ./... is clean.

GetBatchResponseById panicked when the item had no body (a 204 or 302
item) because of the type assertion on a nil result, and when the id
was not in the batch response because of a nil dereference. Return the
zero value for items without a body, and an error for unknown ids.
@0z5a
0z5a (0z5a) requested a review from a team as a code owner November 23, 2026 11:52
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.

GetBatchResponseById panics for items without a body (204/302) and for unknown ids

1 participant