Skip to content

docs: fix example outputs and claims that differ from actual behavior - #4914

Open
yxshee wants to merge 1 commit into
gin-gonic:masterfrom
yxshee:fix/doc-claimed-outputs
Open

yxshee wants to merge 1 commit into
gin-gonic:masterfrom
yxshee:fix/doc-claimed-outputs

Conversation

@yxshee

@yxshee yxshee commented Oct 26, 2026

Copy link
Copy Markdown

I ran the examples in docs/doc.md against current master with net/http/httptest and compared the results with what the docs say. Five places do not match. This PR changes only docs/doc.md.

What I changed

  1. Parameters in path. The comment says "Routes starting with /user/groups are never interpreted as /user/:name/... routes". That is not true. With the routes from the example, GET /user/groups/admin is served by /user/:name/*action and returns groups is /admin. Only the exact path /user/groups goes to the static route. I rewrote the comment to say that.
  2. Map as querystring or postform parameters. The output shows ids: map[b:hello a:1234]; names: map[second:tianou first:thinkerou]. fmt prints map keys in sorted order (since Go 1.12), so the real output is ids: map[a:1234 b:hello]; names: map[first:thinkerou second:tianou]. This does not depend on the environment.
  3. JSONP. The comment says the output is x({\"foo\":\"bar\"}). The real body is x({"foo":"bar"});, with plain quotes and a trailing semicolon. The escaped form looks like it was copied from the Go string literal in context_test.go.
  4. Model binding, "Must bind" behavior. The docs say a failed Bind sets Content-Type to text/plain; charset=utf-8. It does not. AbortWithError(400, err) writes the headers right away with an empty body and no Content-Type. Over a real httptest.NewServer, the response headers are only Content-Length: 0 and Date.
  5. Set and get a cookie. The first example uses SetCookieData and http.Cookie but does not import net/http or time, so it does not compile (undefined: http). It is also the same code as the SetCookieData example right after it, which is introduced with "You can also use the SetCookieData method". Before feat(context): add SetCookieData #4240 the first example used c.SetCookie(...). I put that call back. It now compiles, and it sends Set-Cookie: gin_cookie=test; Path=/; Domain=localhost; Max-Age=3600; HttpOnly.

How I checked

For each item I copied the example into a small test module that points at this repo with a replace directive, ran it with go test -v (Go 1.26.0), and printed the response. For the cookie examples I put each code block into its own main package and ran go vet. The first one failed before this change and passes after it. The second one passes both before and after.

Pull Request Checklist

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions). (This is a docs-only change.)
  • Tests are added or modified as needed to cover code changes. (No code changes.)
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md. (Not a feature.)

Each of these was checked by running the example against the current
master branch with net/http/httptest.

- Parameters in path: the comment said routes starting with /user/groups
  are never interpreted as /user/:name/... routes. GET /user/groups/admin
  is served by /user/:name/*action with name "groups". Only the exact
  path /user/groups goes to the static route.
- Map as querystring or postform parameters: fmt prints map keys in
  sorted order, so the output is map[a:1234 b:hello] and
  map[first:thinkerou second:tianou].
- JSONP: the response body is x({"foo":"bar"}); with plain quotes and a
  trailing semicolon. The escaped quotes were copied from a Go string
  literal in the tests.
- Model binding: when Bind fails, AbortWithError(400, err) sends the
  headers with an empty body and no Content-Type. It does not set
  text/plain; charset=utf-8.
- Set and get a cookie: the first example used SetCookieData and
  http.Cookie without importing net/http and time, so it did not
  compile, and it duplicated the SetCookieData example that follows it.
  Restore the SetCookie call it used before SetCookieData was added.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NQDmA3mwGfL83pbg9WQ6ey
@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.71%. Comparing base (0f09c3a) to head (8ea227b).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4914      +/-   ##
==========================================
+ Coverage   98.65%   98.71%   +0.06%     
==========================================
  Files          48       48              
  Lines        3188     3188              
==========================================
+ Hits         3145     3147       +2     
+ Misses         43       41       -2     
Flag Coverage Δ
--ldflags="-checklinkname=0" -tags sonic 98.64% <ø> (ø)
-tags go_json 98.57% <ø> (ø)
-tags nomsgpack 98.69% <ø> (+0.06%) ⬆️
go-1.26 98.40% <ø> (+0.06%) ⬆️
go-1.27 98.67% <ø> (ø)
macos-latest 98.65% <ø> (ø)
ubuntu-latest 98.71% <ø> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant