While using the notification stream in hub4j/github-api, I encountered a GitHub API 422 response:
| |
The fix was merged in PR #1984 and linked to Issue #1985.
Trace the error to the request header
The response explicitly named If-Modified-Since, so the investigation focused on request construction rather than response parsing. In GHNotificationStream.fetch(), the request reused the Last-Modified value from the previous response:
| |
The problem was that lastModified is optional. When it was null, the request layer could send the string "null". That is not a valid HTTP date, so the GitHub API returned 422.
Apply the smallest correct fix
The notification stream did not need a broad refactor. The header should only be set when a value exists:
| |
This preserves the valid path. When GitHub returns Last-Modified, later polls still use a conditional request. When it is absent, the client sends a normal request.
Add a regression test
A one-line code change is not enough evidence that the bug will stay fixed. I added WireMock data for the notification endpoint and extended the existing test with another iteration after a response without Last-Modified.
The test verifies observable behavior rather than a private field: consecutive notification reads must not send an If-Modified-Since header containing "null" and must not receive a 422 response.
Takeaway
An effective open-source fix combines a reproducible failure, a focused code change, and a regression test that matches the project’s existing style.
The same boundary condition applies to many integrations: optional response headers, cursors, continuation tokens, and pagination links must be checked before they are copied into a later request.