fix(data): respect caller Content-Type; clean up source comments#51
Open
wenchy wants to merge 7 commits into
Open
fix(data): respect caller Content-Type; clean up source comments#51wenchy wants to merge 7 commits into
wenchy wants to merge 7 commits into
Conversation
Tighten and standardize the doc comments across options.go: fix
grammar, use concise phrasing ('from key-value pairs', 'sets
Content-Type to'), name the actual parameter where it clarifies
behavior, and add doc links matching the existing style. Behavior is
unchanged.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #51 +/- ##
==========================================
+ Coverage 83.05% 83.19% +0.14%
==========================================
Files 7 7
Lines 360 363 +3
==========================================
+ Hits 299 302 +3
Misses 38 38
Partials 23 23 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Extend the comment cleanup beyond options.go to the rest of the
package: request.go, response.go, client.go, method.go,
interceptor.go, and default.go. Tighten prose, fix grammar, and
standardize phrasing ('wraps [http.X]', 'sends an HTTP GET request',
'sets Content-Type to'). Also correct a couple of misleading inline
comments (e.g. the do() body-close note, the StatusText return).
Behavior is unchanged.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Reflect the actual evaluation order: io.Reader is matched first (on the value as passed), non-reader pointers are dereferenced, then the []byte/struct-map-slice/default rules apply. Document the *[]byte raw case and the typed-nil-pointer footgun ((*T)(nil) is sent as "<nil>"). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Since 13db74d, requestData unconditionally called opts.Headers.Set("Content-Type", ...), overwriting any Content-Type the caller set via Headers/HeaderPairs. JSON already had the same force-override behavior. This restores the pre-13db74d capability for Data and aligns JSON with it: Content-Type is only set when the caller has not provided one. Form and Files keep forced override, since their Content-Type carries the boundary the body actually depends on. Adds setContentTypeIfAbsent helper, documents the precedence in the Data/JSON/Form/Files option comments, and pins the behavior with TestContentTypeOverride. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
JSON, Form, and Files all produce a body of known format, so they force their Content-Type (overriding any caller value) for consistency and to prevent a caller from declaring a mismatched type. Only Data respects a caller-supplied Content-Type, since its body format is deduced (guessed) and the caller may know better. For custom JSON-based media types (e.g. application/ld+json), Data with an explicit Content-Type covers it — Data marshals structs/maps the same way JSON does. Reverts the JSON respect-override added in the previous commit; JSON keeps its pre-existing force behavior. Updates the JSON comment and TestContentTypeOverride accordingly. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Frame Data's behavior around issue #48's two cases: a caller-supplied Content-Type (via Headers/HeaderPairs) is used as-is; otherwise it is deduced from the data type. Note the deduction is a heuristic (e.g. XML is detected as text/xml, not application/xml), so set it explicitly when the exact type matters. Document Body as the raw/streamed path that sets no Content-Type, and add a README example for sending xml/octet-stream with an explicit Content-Type. Add TestDataStringVsBytesEquivalent to pin #48's requirement that Data(string) and Data([]byte) with the same content produce the same HTTP request. No behavior change. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Owner
Author
|
Follow-up tying this work to #48: issue #48's two-case model (caller-supplied Content-Type wins; otherwise deduce from type) is now both implemented and documented.
Refs #48. |
Prefer HeaderPairs("Content-Type", "application/xml") over
Headers(map[string]string{"Content-Type": "..."}) when setting a
single Content-Type, in the README example and TestContentTypeOverride.
Cleaner and consistent with the existing HeaderPairs idiom.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
A doc-cleanup pass surfaced a real regression, which is fixed here alongside the comment work.
Behavior fix — Content-Type handling
Since
13db74d(auto-deduce content type),requestDataunconditionally ranopts.Headers.Set("Content-Type", ...), overwriting anyContent-Typethe caller set viaHeaders/HeaderPairs. Pre-13db74d,Datanever touchedContent-Type, soData(x)+ a customContent-Typeworked. This restores that capability forData.The split is now principled — based on whether the body format is known or guessed:
DataJSON/Form/FilesForm/Filesmust force because theirContent-Typecarries the boundary the body depends on.JSONforces for the same reason the body format is known — and because custom JSON media types (application/ld+json,application/problem+json, …) are already covered byData+ an explicitContent-Type(Datamarshals structs/maps viajson.Marshaljust likeJSON). So onlyDataneeds to defer to the caller.Net behavior delta vs
master: onlyDatachanges (regains override support).JSON/Form/Fileskeep their existing force behavior.Comment cleanup across source files
Standardized doc comments for clarity/conciseness (
wraps [http.X],sends an HTTP GET request,sets/forces Content-Type to, etc.) and corrected a few misleading comments. TheDatacomment now accurately describesdeduceContentTypeAndBody's real evaluation order (io.Reader first, then pointer deref, then kind rules), the*[]byteraw-bytes case, and the typed-nil-pointer ((*T)(nil)→"<nil>") footgun.Files
request.go,options.go,response.go,client.go,method.go,interceptor.go,default.go,request_test.goVerification
go build ./...✅go vet ./...✅go test ./...✅ — includingTestContentTypeOverride: Data deduces when unset and respects a caller value; JSON/Form force their Content-Type.🤖 Generated with Claude Code