diff --git a/README.md b/README.md index 49d54453..8fb21bd7 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,9 @@ How the specification maps onto Go: - **Ignored header parameters** — a header parameter named `Accept`, `Content-Type` or `Authorization` is ignored, as OpenAPI 3.1 says: the client sends the request body's media type and the credential of the security scheme. +- **Accept** — each request sends the media types its operation's responses + declare, each once, such as `application/json`, or none where no response has + a body. - **Unknown formats** — a format the generator does not know, which OpenAPI 3.1 allows, gives the plain type: an `int`, a `float64` or a `string`. - **Empty dates** — an empty string, which an API may send for a date or diff --git a/README/features.md b/README/features.md index e3760e3e..cf7d1f90 100644 --- a/README/features.md +++ b/README/features.md @@ -161,6 +161,9 @@ How the specification maps onto Go: - **Ignored header parameters** — a header parameter named `Accept`, `Content-Type` or `Authorization` is ignored, as OpenAPI 3.1 says: the client sends the request body's media type and the credential of the security scheme. +- **Accept** — each request sends the media types its operation's responses + declare, each once, such as `application/json`, or none where no response has + a body. - **Unknown formats** — a format the generator does not know, which OpenAPI 3.1 allows, gives the plain type: an `int`, a `float64` or a `string`. - **Empty dates** — an empty string, which an API may send for a date or diff --git a/go.mod b/go.mod index e6d05cf0..13f66082 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/MarkRosemaker/openapi-codegen go 1.27 require ( - github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a + github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d github.com/MarkRosemaker/errpath v0.0.0-20261118230255-99e20cb3445a github.com/MarkRosemaker/openapi v0.0.0-20261124050252-52a60c76de04 github.com/MarkRosemaker/openapi-compress v0.0.0-20261124050556-094155f0b064 @@ -11,7 +11,7 @@ require ( github.com/MarkRosemaker/openapi-flatten v0.0.0-20261124050336-684f3610d8d7 github.com/MarkRosemaker/ordmap v0.0.0-20261124050216-f3096eb875be github.com/ettle/strcase v0.2.0 - github.com/go-api-libs/api v0.0.0-20261115172114-130f6fc73195 + github.com/go-api-libs/api v0.0.0-20261125182139-0f507257dcd7 github.com/spf13/afero v1.15.0 mvdan.cc/gofumpt v0.12.0 ) diff --git a/go.sum b/go.sum index 5b960f62..474a8e67 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.123.0 h1:2NAUJwPR47q+E35uaJeYoNhuNEM9kM8SjgRgdeOJUSE= cloud.google.com/go v0.123.0/go.mod h1:xBoMV08QcqUGuPW65Qfm1o9Y4zKZBpGS+7bImXLTAZU= -github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a h1:iMV+JecKObVuGDQw0YhgsChj9Q8JZXVAk0tsyw1vCq0= -github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a/go.mod h1:mIy1SlNqKDRALYBqZGjj+MWPU9dASkyP1d1X+I1HP6k= +github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d h1:QJ5PKBI67moq/DdVbA2yxn+TC+MKNAnGqA/KPaHp0TM= +github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d/go.mod h1:mIy1SlNqKDRALYBqZGjj+MWPU9dASkyP1d1X+I1HP6k= github.com/MarkRosemaker/errpath v0.0.0-20261118230255-99e20cb3445a h1:C7Oyg94gPC6/nfCR8ev9FqyxQb5d+GEpU4Oz+rGcoww= github.com/MarkRosemaker/errpath v0.0.0-20261118230255-99e20cb3445a/go.mod h1:VnXpUlsS3n1gSToedcQl2FCARGoA16CyitbCanUIv+I= github.com/MarkRosemaker/json2yaml v0.0.0-20261118230255-853c75cf2bf9 h1:NVTLiOVHTQ/PSq3ihG3kT/P1UqNjJiBHHCGbgNq2rP8= @@ -30,8 +30,8 @@ github.com/MarkRosemaker/yaml2json v0.0.0-20261118230255-06c4fe76a5b8 h1:D4cujGs github.com/MarkRosemaker/yaml2json v0.0.0-20261118230255-06c4fe76a5b8/go.mod h1:DxNA9SDFrzw+RHsnxjIxjUzHy0I4+3hZg7R13u3SBvE= github.com/ettle/strcase v0.2.0 h1:fGNiVF21fHXpX1niBgk0aROov1LagYsOwV/xqKDKR/Q= github.com/ettle/strcase v0.2.0/go.mod h1:DajmHElDSaX76ITe3/VHVyMin4LWSJN5Z909Wp+ED1A= -github.com/go-api-libs/api v0.0.0-20261115172114-130f6fc73195 h1:+QFVvV0eav8ZKBJiyJhjkp46SkuRg8C+RVUvm/D4/MQ= -github.com/go-api-libs/api v0.0.0-20261115172114-130f6fc73195/go.mod h1:4Ls/f+97MK0jpUbKa713C/itwrFlVvHwxt+CWLXdsr0= +github.com/go-api-libs/api v0.0.0-20261125182139-0f507257dcd7 h1:BMgkSz7tviqSK2Gg3ZQeyuMYgi4G33RK/UcqiozHfoE= +github.com/go-api-libs/api v0.0.0-20261125182139-0f507257dcd7/go.mod h1:dHyiZnevRGCtsIQZlq9LKKJyNQGizbsac/IEG7/qeSo= github.com/go-api-libs/types v0.0.0-20261118230255-0e16a3d7de5d h1:WJpz1GJEMd/4zdJ1ElOXCSswW8BQzsWGRz27Y1TivrI= github.com/go-api-libs/types v0.0.0-20261118230255-0e16a3d7de5d/go.mod h1:z0z6qkZc45AvoRnluq7PkVSlSSF25sT0diJ+vrubYfA= github.com/go-quicktest/qt v1.102.0 h1:HSQxCeh5YZH3EL3W39ixjtyaEhcWSXQHtHnMBzSs474= diff --git a/ir/ir.go b/ir/ir.go index fc626789..a7e9c47e 100644 --- a/ir/ir.go +++ b/ir/ir.go @@ -656,6 +656,20 @@ type Response struct { BodyDescription string `json:"bodyDescription,omitzero"` } +// Accept is the Accept header the operation sends: the media types its responses declare, in their order, or "" where +// none has a body. +func (op Operation) Accept() string { + var types []string + + for _, r := range op.Responses { + if r.ContentType != "" && !slices.Contains(types, r.ContentType) { + types = append(types, r.ContentType) + } + } + + return strings.Join(types, ", ") +} + // SuccessDescription is what the specification says beside the reference to the schema of the body the operation // returns, if anything. func (op Operation) SuccessDescription() string { diff --git a/ir/ir_test.go b/ir/ir_test.go index 50dee4dc..b7701be0 100644 --- a/ir/ir_test.go +++ b/ir/ir_test.go @@ -30,3 +30,20 @@ func TestZeroVal(t *testing.T) { } } } + +// An operation accepts the media types its responses declare, each once, in their order; one without a body adds none. +func TestOperation_Accept(t *testing.T) { + op := Operation{Responses: Responses{ + {StatusCode: "200", ContentType: "application/json"}, + {StatusCode: "204"}, + {StatusCode: "400", ContentType: "application/problem+json"}, + {StatusCode: "500", ContentType: "application/json"}, + }} + if got, want := op.Accept(), "application/json, application/problem+json"; got != want { + t.Errorf("Accept() = %q, want %q", got, want) + } + + if got := (Operation{Responses: Responses{{StatusCode: "204"}}}).Accept(); got != "" { + t.Errorf("no body: Accept() = %q, want none", got) + } +} diff --git a/render/templates/client.gen.go.tmpl b/render/templates/client.gen.go.tmpl index 1efe28bd..b9b36401 100644 --- a/render/templates/client.gen.go.tmpl +++ b/render/templates/client.gen.go.tmpl @@ -332,6 +332,9 @@ func (c *Client) {{.Name}}(ctx context.Context{{range .PathParams}}, {{.GoName}} {{ range .FixedHeaders -}} "{{ .JSONName }}": []string{ {{- .Value -}} }, {{ end -}} + {{ with .Accept -}} + "Accept": []string{ {{- printf "%q" . -}} }, + {{ end -}} "User-Agent": []string{c.userAgent}, {{ if and .RequestBody .RequestBody.Required -}} "Content-Type": []string{ {{printf "%q" .RequestBody.ContentType}} }, diff --git a/testdata/apikey/pkg/apikey/client.gen.go b/testdata/apikey/pkg/apikey/client.gen.go index 4f434e26..25fcaab6 100644 --- a/testdata/apikey/pkg/apikey/client.gen.go +++ b/testdata/apikey/pkg/apikey/client.gen.go @@ -118,6 +118,7 @@ func (c *Client) GetQuote(ctx context.Context) (*Quote, error) { u := c.baseURL.JoinPath("quote") req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, diff --git a/testdata/auth/pkg/auth/client.gen.go b/testdata/auth/pkg/auth/client.gen.go index 7250bff8..ced33dbf 100644 --- a/testdata/auth/pkg/auth/client.gen.go +++ b/testdata/auth/pkg/auth/client.gen.go @@ -141,6 +141,7 @@ func (c *Client) ListItems(ctx context.Context) ([]string, error) { "Authorization": []string{c.bearer}, "X-Api-Key": []string{c.apiKey}, "X-Client": []string{c.client}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -190,6 +191,7 @@ func (c *Client) GetFeed(ctx context.Context) ([]string, error) { "Authorization": []string{c.bearer}, "X-Api-Key": []string{c.apiKey}, "X-Client": []string{c.client}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, diff --git a/testdata/debug/pkg/debug/client.gen.go b/testdata/debug/pkg/debug/client.gen.go index d19a74eb..76530c93 100644 --- a/testdata/debug/pkg/debug/client.gen.go +++ b/testdata/debug/pkg/debug/client.gen.go @@ -122,6 +122,7 @@ func (c *Client) GetItem(ctx context.Context) (*Item, error) { req := (&http.Request{ Header: http.Header{ "Authorization": []string{c.bearer}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -187,6 +188,7 @@ func (c *Client) ListItems(ctx context.Context) ([]Item, error) { req := (&http.Request{ Header: http.Header{ "Authorization": []string{c.bearer}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -237,6 +239,7 @@ func (c *Client) GetBlock(ctx context.Context) (*Block, error) { req := (&http.Request{ Header: http.Header{ "Authorization": []string{c.bearer}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -287,6 +290,7 @@ func (c *Client) GetPet(ctx context.Context) (*Pet, error) { req := (&http.Request{ Header: http.Header{ "Authorization": []string{c.bearer}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -339,6 +343,7 @@ func (c *Client) Ping(ctx context.Context) error { req := (&http.Request{ Header: http.Header{ "Authorization": []string{c.bearer}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, diff --git a/testdata/golden/pkg/golden/client.gen.go b/testdata/golden/pkg/golden/client.gen.go index dfae5183..969a7fdb 100644 --- a/testdata/golden/pkg/golden/client.gen.go +++ b/testdata/golden/pkg/golden/client.gen.go @@ -86,6 +86,7 @@ func (c *Client) GetPage(ctx context.Context, id string) (*Page, error) { u := c.baseURL.JoinPath("pages", escapePathSegment(id)) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -157,6 +158,7 @@ func (c *Client) Search(ctx context.Context, body PropertyFilter) ([]PageOrParti pr, pw := io.Pipe() req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, "Content-Type": []string{"application/json"}, }, @@ -205,6 +207,7 @@ func (c *Client) GetTrial(ctx context.Context, id string) (*Trial, error) { u := c.baseURL.JoinPath("trials", escapePathSegment(id)) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -246,6 +249,7 @@ func (c *Client) UpdateUser(ctx context.Context, id string, body User) (*User, e pr, pw := io.Pipe() req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, "Content-Type": []string{"application/json"}, }, @@ -294,6 +298,7 @@ func (c *Client) PatchUser(ctx context.Context, id string, body *User) error { u := c.baseURL.JoinPath("users", escapePathSegment(id)) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -358,6 +363,7 @@ func (c *Client) QueryEntries(ctx context.Context, body QueryEntries) (*QueryEnt pr, pw := io.Pipe() req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, "Content-Type": []string{"application/json"}, }, @@ -438,6 +444,7 @@ func (c *Client) ListEntries(ctx context.Context, params *ListEntriesParams) (*L req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -507,6 +514,7 @@ func (c *Client) CreateEntry(ctx context.Context, body Entry) (*Entry, error) { pr, pw := io.Pipe() req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, "Content-Type": []string{"application/json"}, }, @@ -555,6 +563,7 @@ func (c *Client) GetValue(ctx context.Context, id string) (*Value, error) { u := c.baseURL.JoinPath("values", escapePathSegment(id)) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -595,6 +604,7 @@ func (c *Client) GetBlock(ctx context.Context, id string) (*Block, error) { u := c.baseURL.JoinPath("blocks", escapePathSegment(id)) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -636,6 +646,7 @@ func (c *Client) CreateDatabase(ctx context.Context, body CreateDatabaseParent) pr, pw := io.Pipe() req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, "Content-Type": []string{"application/json"}, }, @@ -682,6 +693,7 @@ func (c *Client) GetProperty(ctx context.Context, id string) (*NestedValue, erro u := c.baseURL.JoinPath("properties", escapePathSegment(id)) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -766,6 +778,7 @@ func (c *Client) ListReports(ctx context.Context, params ListReportsParams) (Rep req := (&http.Request{ Header: http.Header{ "X-Version": []string{params.XVersion}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -808,6 +821,7 @@ func (c *Client) ListPeople(ctx context.Context) (*ListPeopleOk, error) { u := c.baseURL.JoinPath("people") req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -849,6 +863,7 @@ func (c *Client) CreateSchedule(ctx context.Context, body Schedule) (*CreateSche pr, pw := io.Pipe() req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, "Content-Type": []string{"application/json"}, }, @@ -910,6 +925,7 @@ func (c *Client) DownloadReport(ctx context.Context, format ScheduleFormat) (io. u := c.baseURL.JoinPath("reports", escapePathSegment(string(format))) req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/pdf"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -957,6 +973,7 @@ func (c *Client) DeleteSchedule(ctx context.Context, id string) error { req := (&http.Request{ Header: http.Header{ "Api-Version": []string{"2"}, + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -1027,6 +1044,7 @@ func (c *Client) GetRanges(ctx context.Context) ([]string, error) { u := c.baseURL.JoinPath("ranges") req := (&http.Request{ Header: http.Header{ + "Accept": []string{"application/json"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, @@ -1072,6 +1090,7 @@ func (c *Client) GetStatus(ctx context.Context) ([]byte, error) { u := c.baseURL.JoinPath("status") req := (&http.Request{ Header: http.Header{ + "Accept": []string{"text/plain"}, "User-Agent": []string{c.userAgent}, }, Host: u.Host, diff --git a/vendor/github.com/MarkRosemaker/cassette/replay.go b/vendor/github.com/MarkRosemaker/cassette/replay.go index 141c8198..874d02b9 100644 --- a/vendor/github.com/MarkRosemaker/cassette/replay.go +++ b/vendor/github.com/MarkRosemaker/cassette/replay.go @@ -209,6 +209,7 @@ type MatchOption func(*matcher) type matcher struct { headers bool ignore map[string]bool + optional map[string]bool optionalAuth func(*http.Request) bool } @@ -246,6 +247,21 @@ func OptionalAuthorization(optional func(*http.Request) bool) MatchOption { return func(m *matcher) { m.optionalAuth = optional } } +// OptionalHeaders makes [MatchRequest] match a recording without the headers named to a request that has them, and +// compare them where the recording has them: a header a client has begun to send, such as Accept, is missing from what +// was recorded before. +func OptionalHeaders(names ...string) MatchOption { + return func(m *matcher) { + if m.optional == nil { + m.optional = map[string]bool{} + } + + for _, name := range names { + m.optional[http.CanonicalHeaderKey(name)] = true + } + } +} + func (m *matcher) match(req *http.Request, rec Request) error { got, err := NewRequest(req) if err != nil { @@ -302,6 +318,12 @@ func (m *matcher) matchHeaders(req *http.Request, got http.Header, rec Request) want.Del(name) } + for name := range m.optional { + if _, ok := want[name]; !ok { + got.Del(name) + } + } + got.Del("Authorization") want.Del("Authorization") diff --git a/vendor/github.com/go-api-libs/api/README.md b/vendor/github.com/go-api-libs/api/README.md index 206f95ab..ccea4355 100644 --- a/vendor/github.com/go-api-libs/api/README.md +++ b/vendor/github.com/go-api-libs/api/README.md @@ -1,5 +1,5 @@ -[![Go Reference](https://pkg.go.dev/badge/github.com/go-api-libs/api.svg)](https://pkg.go.dev/github.com/go-api-libs/api) ![Code Coverage](https://img.shields.io/badge/coverage-90%25-brightgreen) [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE) +[![Go Reference](https://pkg.go.dev/badge/github.com/go-api-libs/api.svg)](https://pkg.go.dev/github.com/go-api-libs/api) ![Code Coverage](https://img.shields.io/badge/coverage-91%25-brightgreen) [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE) ![Code Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen) diff --git a/vendor/github.com/go-api-libs/api/apitest/replay.go b/vendor/github.com/go-api-libs/api/apitest/replay.go index f6b8e997..70b74813 100644 --- a/vendor/github.com/go-api-libs/api/apitest/replay.go +++ b/vendor/github.com/go-api-libs/api/apitest/replay.go @@ -21,9 +21,10 @@ func OptionalAuth(ctx context.Context) context.Context { } // Replay is a transport that answers calls with the interactions recorded in the file at path. Every call must be the -// next recording, with its headers as recorded but the User-Agent, which names whatever tool made the recording; a -// call marked with [OptionalAuth] may have credentials its recording lacks. A request body may hold a tag its -// recording leaves out, which a client fills in (see jsonSameButTags). The test fails if a recording was never called. +// next recording, with its headers as recorded but the User-Agent, which names whatever tool made the recording, and an +// Accept its recording lacks, as one made before clients sent it; a call marked with [OptionalAuth] may have credentials +// its recording lacks. A request body may hold a tag its recording leaves out, which a client fills in (see +// jsonSameButTags). The test fails if a recording was never called. func Replay(t testing.TB, path string) http.RoundTripper { t.Helper() @@ -34,6 +35,7 @@ func Replay(t testing.TB, path string) http.RoundTripper { match := cassette.MatchRequest( cassette.CompareHeaders("User-Agent"), + cassette.OptionalHeaders("Accept"), cassette.OptionalAuthorization(func(req *http.Request) bool { return req.Context().Value(optionalAuthKey{}) != nil }), ) diff --git a/vendor/github.com/go-api-libs/api/devtool.json b/vendor/github.com/go-api-libs/api/devtool.json index 3caee087..390a1787 100644 --- a/vendor/github.com/go-api-libs/api/devtool.json +++ b/vendor/github.com/go-api-libs/api/devtool.json @@ -1,10 +1,10 @@ { - "coverage": 90.9, - "devtoolVersion": "v0.0.0-20261115154222-36316578e5dc", + "coverage": 91.1, + "devtoolVersion": "v0.0.0-20261118133811-97246f04510d", "kind": "apilib", "tools": { "golangci-lint": "v2.15.0", - "govulncheck": "v1.9.0", - "pkgsite": "v0.6.0" + "govulncheck": "v1.10.0", + "pkgsite": "v0.7.0" } } diff --git a/vendor/modules.txt b/vendor/modules.txt index b3ec4d4f..ee419488 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,7 +1,7 @@ # cloud.google.com/go v0.123.0 ## explicit; go 1.24.0 cloud.google.com/go/civil -# github.com/MarkRosemaker/cassette v0.0.0-20261124170238-3b0b504cc01a +# github.com/MarkRosemaker/cassette v0.0.0-20261125180804-407fe91b7b7d ## explicit; go 1.27 github.com/MarkRosemaker/cassette # github.com/MarkRosemaker/errpath v0.0.0-20261118230255-99e20cb3445a @@ -40,7 +40,7 @@ github.com/MarkRosemaker/yaml2json # github.com/ettle/strcase v0.2.0 ## explicit; go 1.12 github.com/ettle/strcase -# github.com/go-api-libs/api v0.0.0-20261115172114-130f6fc73195 +# github.com/go-api-libs/api v0.0.0-20261125182139-0f507257dcd7 ## explicit; go 1.27 github.com/go-api-libs/api github.com/go-api-libs/api/apitest