refactor(codersdk): use ReadBodyAsJSON in typed endpoints - #27857
Merged
dylanhuff-at-coder merged 1 commit intoAug 5, 2026
Conversation
Contributor
Author
This was referenced Aug 4, 2026
Base automatically changed from
dylan/plat-378-sdk-json-response-decoder
to
main
August 5, 2026 17:42
dylanhuff-at-coder
marked this pull request as ready for review
August 5, 2026 17:44
dylanhuff-at-coder
requested review from
ibetitsmike and
johnstcn
as code owners
August 5, 2026 17:44
dylanhuff-at-coder
force-pushed
the
dylan/plat-378-codersdk-read-body-as-json
branch
from
August 5, 2026 17:52
a08a60d to
0f4d0e0
Compare
dylanhuff-at-coder
marked this pull request as draft
August 5, 2026 18:28
dylanhuff-at-coder
marked this pull request as ready for review
August 5, 2026 19:15
dylanhuff-at-coder
deleted the
dylan/plat-378-codersdk-read-body-as-json
branch
August 5, 2026 20:22
dylanhuff-at-coder
added a commit
that referenced
this pull request
Aug 5, 2026
Migrate chat endpoint response decoding to `ReadBodyAsJSON` and consolidate `ReadBodyAsError` construction through `newResponseError`, so empty-body and non-JSON errors consistently include the request method and URL. Stacked on #27857, with the lint rule following in #27859. Refs #27044. Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
dylanhuff-at-coder
added a commit
that referenced
this pull request
Aug 6, 2026
Add a ruleguard rule forbidding direct `json.NewDecoder(res.Body).Decode(...)` on `*http.Response` in codersdk packages, so new typed endpoints use `codersdk.ReadBodyAsJSON` and keep returning structured errors for non-JSON bodies. The rule matches both the chained call form and decoders assigned to a variable first. Intentional raw-body paths carry documented `//nolint:gocritic` exceptions: the 16 agent-direct HTTP decodes in `workspacesdk/agentconn.go` route through a single `decodeAgentJSON` helper (agent-direct over tailnet, so `ReadBodyAsJSON`'s reverse proxy/SSO error guidance does not apply), and the Azure IMDS attested-document decode in `agentsdk/azure.go` keeps an inline exception. The two `UseNumber` decoders in `licenses.go` are migrated to a new `codersdk.ReadBodyAsJSONUseNumber`, so `coder licenses add/list` also return structured errors for non-JSON bodies instead of `invalid character '<' looking for beginning of value`. Note for local verification: golangci-lint caches results, so run `golangci-lint cache clean` after modifying `scripts/rules.go` or the rule may silently not fire. Final PR of the stack on #27804, #27857, and #27858. Refs #27044. Stack plan Inventory (full-tree audit): 280 migratable call sites across 47 files; 17 excluded (16 agent-direct HTTP sites in `workspacesdk/agentconn.go`, 1 Azure IMDS decode in `agentsdk/azure.go`). 1. **#27857** `refactor(codersdk): use ReadBodyAsJSON in typed endpoints`: mechanical migration of all sites except `chats.go` (224 sites, 46 files). 2. **#27858** `refactor(codersdk): use shared error helpers in chat endpoints`: migrate the 56 `chats.go` sites and consolidate the duplicated `readRawBodyAsError`/`newResponseError` helpers onto the shared `client.go` error path, with regression tests for the 409 usage-limit flow. 3. **#27859** `chore: forbid direct response body JSON decode in codersdk`: ruleguard rule with documented exceptions for the intentional raw-body paths, plus `ReadBodyAsJSONUseNumber` for the `licenses.go` decoders. Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.
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.

This PR migrates 224 typed JSON response sites across 46 files to
codersdk.ReadBodyAsJSON, so invalid 2xx bodies return structured errors while preserving URL credential redaction.It intentionally excludes agent-direct HTTP, Azure IMDS,
UseNumber, and chat paths; stacked on #27804, with chat and lint follow-ups in #27858 and #27859. Refs #27044.Reviewed and updated by Coder Agents on behalf of @dylanhuff-at-coder.