GetBatchResponseById panics for items without a body (204/302) and for unknown ids
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 72/100
Research direction
Start in batch_requests.go at GetBatchResponseById; the two failure points are the unchecked result.(T) after parseNode.GetObjectValue and the unguarded resp.GetResponseById before *item.GetStatus(). Reuse the fixtures and helpers in batch_request_test.go — getDummyJSON() already carries the 302 ("1") and 204 ("4") items with "body": null — to add the three cases from the report. Done means a nil body yields the zero T with no error, an unknown id returns an error instead of panicking, and the batch tests pass. Note the reporter offers to open a PR, so coordinate before duplicating work.
Written by the indexing model from the issue text.
Description
Describe the bug
GetBatchResponseById[T] panics when the batch item it is asked for has no body, which is the normal case for a 204 No Content or a 302 item. It also panics when the id is not in the batch response.
The end of the function is:
result, err := parseNode.GetObjectValue(constructor)
return result.(T), err
For an item whose body is null or missing, GetObjectValue returns nil, nil, and result.(T) on a nil interface panics with:
interface conversion: interface is nil, not msgraphgocore.Userable
For an id that is not in the response, resp.GetResponseById(itemId) returns nil, and *item.GetStatus() panics with:
runtime error: invalid memory address or nil pointer dereference
Expected behavior
- An item without a body returns the zero value of
Tand no error. This is what the request adapter's ownSenddoes for a204outside of a batch: I ranreqAdapter.SendwithCreateUseragainst anhttptestserver that answers204, and it returnednil, nil. - An unknown id returns an error instead of panicking. The Python core raises
ValueError("No response found for id: ...")in this case.
How to reproduce
The repo's own test fixture getDummyJSON() in batch_request_test.go already has a 302 item ("1") and a 204 item ("4"), both with "body": null. With the test helpers from batch_request_test.go:
mockServer := makeMockRequest(200, getDummyJSON())
defer mockServer.Close()
reqAdapter.SetBaseUrl(mockServer.URL + "/$batch")
batch := NewBatchRequest(reqAdapter)
_, _ = batch.AddBatchRequestStep(*getRequestInfo())
resp, _ := batch.Send(context.Background(), reqAdapter)
_, err := GetBatchResponseById[Userable](resp, "4", CreateUser) // panics (204, null body)
_, err = GetBatchResponseById[Userable](resp, "1", CreateUser) // panics (302, null body)
_, err = GetBatchResponseById[Userable](resp, "99", CreateUser) // panics (no such id)
On current main (80c76c3) all three panic with the messages above. A 204 item without any body field ({"id":"5","status":204,"headers":{}}) panics the same way.
SDK Version
v1.4.1 / current main (80c76c3)
Latest version known to work for scenario above?
Not known.
Known Workarounds
Check the status code and the body yourself before calling GetBatchResponseById (for example with resp.GetStatusCodes()), and only call it for items that have a body.
Other information
How I found it: I was reading batch_requests.go and noticed the unchecked type assertion; I then ran the fixture cases above.
Where this can show up in practice: a batch of DELETE requests gets 204 items back, and code that loops over its own request ids and calls GetBatchResponseById for each one will panic on the first of them. An unknown id is a caller mistake, but it still should not panic. This is my estimate; I have not seen a field report.
I have a small fix with tests and can open a PR.
- Dominant language
- Go
- Stars
- 22
- Forks
- 16
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoftgraph/msgraph-sdk-go-core
-
status:waiting-for-triage type:bug
Difficulty 3/5 1-2 days Newbie friendliness 38/100
microsoftgraph/msgraph-sdk-go-core#359 · 1 reaction ·
-
area:authentication goodfirstissue type:enhancement
Difficulty 3/5 1-2 days Newbie friendliness 45/100
-
Improve type validation for PageIteratorMay be free again @rkodev claimed this 1013 days ago, and no pull request is open. Openenhancement
microsoftgraph/msgraph-sdk-go-core#267 · 11 comments · 1 reaction · 1 assignee ·
-
Batch "response" semantics are confusingMay be free again @rkodev claimed this 1020 days ago, and no pull request is open. Open
microsoftgraph/msgraph-sdk-go-core#250 · 1 comment · 1 assignee ·
-
Default Client OptionsMay be free again @rkodev claimed this 1187 days ago, and no pull request is open. Openenhancement
microsoftgraph/msgraph-sdk-go-core#220 · 10 comments · 1 assignee ·
All issues in microsoftgraph/msgraph-sdk-go-core
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 62/100
slavakurilyak/awesome-ai-agents#1026 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
JuliusBrussee/caveman#1567 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug postgresql
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 1 day