Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/MarkRosemaker/openapi-flatten v0.0.0-20261112181512-46b453b2a7e8
github.com/MarkRosemaker/ordmap v0.0.0-20261112181350-ce24cb2b5e89
github.com/ettle/strcase v0.2.0
github.com/go-api-libs/api v0.0.0-20261112190634-bde25c574d51
github.com/go-api-libs/api v0.0.0-20261113130130-277524695333
github.com/spf13/afero v1.15.0
mvdan.cc/gofumpt v0.12.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ github.com/MarkRosemaker/yaml2json v0.0.0-20261109120453-1b901dbf7e8f h1:Z3r19i6
github.com/MarkRosemaker/yaml2json v0.0.0-20261109120453-1b901dbf7e8f/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-20261112190634-bde25c574d51 h1:JVrl63yl8vSasO3y4K81aIOYjvyuprdZzKJD8PqStnY=
github.com/go-api-libs/api v0.0.0-20261112190634-bde25c574d51/go.mod h1:nJuMPHY4qEhbWBvTdDFOPK+LC3Uo8RM9kPhIQfzXv4s=
github.com/go-api-libs/api v0.0.0-20261113130130-277524695333 h1:jHic3tOf6/vtJHZKB5niLbzXcXwl1kcIq1424Wc9xJc=
github.com/go-api-libs/api v0.0.0-20261113130130-277524695333/go.mod h1:nJuMPHY4qEhbWBvTdDFOPK+LC3Uo8RM9kPhIQfzXv4s=
github.com/go-api-libs/types v0.0.0-20261109120451-6cd1daca4971 h1:SAGMMY0sujlZjekNUL4cvoACSOKdOj55CrikCTFzAdU=
github.com/go-api-libs/types v0.0.0-20261109120451-6cd1daca4971/go.mod h1:z0z6qkZc45AvoRnluq7PkVSlSSF25sT0diJ+vrubYfA=
github.com/go-quicktest/qt v1.102.0 h1:HSQxCeh5YZH3EL3W39ixjtyaEhcWSXQHtHnMBzSs474=
Expand Down
1 change: 1 addition & 0 deletions golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// needs. These keep those modules in go.mod and in vendor.
_ "github.com/MarkRosemaker/cassette"
_ "github.com/go-api-libs/api"
_ "github.com/go-api-libs/api/apitest"
_ "github.com/go-api-libs/api/server"
_ "github.com/go-api-libs/api/union"
)
Expand Down
6 changes: 3 additions & 3 deletions interactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func timeLiteral(t time.Time) string {
// generated test reads like a value a person wrote, not a blob to decode.
// Anything it doesn't recognize (maps, unions, oneOf/anyOf, or a mismatch
// between v and what goType expects) falls back to decoding v's own compact
// JSON into goType at test time via mustDecodeBody, which is always correct
// JSON into goType at test time via apitest.MustDecode, which is always correct
// even where a literal is not worth hand-rolling.
func bodyLiteral(doc *ir.Document, goType string, v any) string {
switch {
Expand Down Expand Up @@ -667,7 +667,7 @@ func bodyLiteral(doc *ir.Document, goType string, v any) string {
return lit
}

return fmt.Sprintf("mustDecodeBody[%s](t, %s)", goType, fallbackJSON(v))
return fmt.Sprintf("apitest.MustDecode[%s](t, %s, jsonOpts)", goType, fallbackJSON(v))
}

// findSchema returns doc's named component schema for goType, or nil if
Expand Down Expand Up @@ -815,7 +815,7 @@ func scalarLiteral(goType string, v any) (string, bool) {
}

// fallbackJSON returns v's compact JSON encoding as a quoted Go string
// literal, for embedding in a mustDecodeBody call. Object members are sorted
// literal, for embedding in an apitest.MustDecode call. Object members are sorted
// by name: v holds maps, so that generating twice writes the same code.
func fallbackJSON(v any) string {
data, err := json.Marshal(v, json.Deterministic(true))
Expand Down
8 changes: 4 additions & 4 deletions interactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,10 @@ func TestBodyLiteral(t *testing.T) {
{"Flag", `true`, "FlagTrue"},
{"Color", `"dark red"`, "ColorDarkRed"},
// a value the enum does not declare is decoded at test time, so the test shows the mismatch
{"Level", `7`, `mustDecodeBody[Level](t, "7")`},
{"Color", `3`, `mustDecodeBody[Color](t, "3")`},
{"Level", `7`, `apitest.MustDecode[Level](t, "7", jsonOpts)`},
{"Color", `3`, `apitest.MustDecode[Color](t, "3", jsonOpts)`},
{"Score", `9.5`, "Score(9.5)"},
{"Tags", `{"a":"b"}`, `mustDecodeBody[Tags](t, "{\"a\":\"b\"}")`},
{"Tags", `{"a":"b"}`, `apitest.MustDecode[Tags](t, "{\"a\":\"b\"}", jsonOpts)`},
{"Pet", `{"name":"Rex","level":3}`, "Pet{\nName: \"Rex\",\nLevel: LevelThree,\n}"},
{"Pet", `{}`, "Pet{}"},
{"*Pet", `null`, "nil"},
Expand All @@ -671,7 +671,7 @@ func TestBodyLiteral(t *testing.T) {
{"*float64", `2.5`, "new(2.5)"},
{"*Score", `9.5`, "new(Score(9.5))"},
{"[]Level", `[1,3]`, "[]Level{LevelOne, LevelThree}"},
{"[]string", `"x"`, `mustDecodeBody[[]string](t, "\"x\"")`},
{"[]string", `"x"`, `apitest.MustDecode[[]string](t, "\"x\"", jsonOpts)`},
{"bool", `false`, "false"},
} {
var v any
Expand Down
28 changes: 1 addition & 27 deletions ir/ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,8 @@ type Document struct {
InteractionCalls InteractionCalls `json:"-"`
}

func (d Document) NeedMustDecodeBody() bool {
return d.InteractionCalls.UseMustDecodeBody()
}

type InteractionCalls []InteractionCall

func (ics InteractionCalls) UseMustDecodeBody() bool {
for _, ic := range ics {
if ic.UsesMustDecodeBody() {
return true
}
}

return false
}

// InteractionCall is one operation call extracted from a recorded interaction.
type InteractionCall struct {
Op *Operation // matched operation
Expand All @@ -84,19 +70,13 @@ type InteractionCall struct {
// BodyLiteral is a Go expression for the recorded request body, set
// whenever Op.RequestBody is non-nil: a composite literal of the
// request body's type when every field could be expressed that way,
// falling back to a runtime JSON decode (mustDecodeBody) for values a
// falling back to a runtime JSON decode (apitest.MustDecode) for values a
// literal can't cleanly represent. Either way the replayed request
// carries the same body the interaction was recorded with, instead of
// a zero value.
BodyLiteral string
}

// UsesMustDecodeBody reports whether the body literal decodes a value at test time, anywhere in it: a field of a
// struct literal may, where the rest of it is written out.
func (ic InteractionCall) UsesMustDecodeBody() bool {
return strings.Contains(ic.BodyLiteral, "mustDecodeBody[")
}

// InteractionParam is one query param with its Go literal value.
type InteractionParam struct {
FieldName string // PascalCase field name on the params struct
Expand Down Expand Up @@ -717,12 +697,6 @@ func (doc Document) HasTagged() bool {
return slices.ContainsFunc(doc.Schemas, func(s Schema) bool { return s.Tagged != nil })
}

// HasOptionalAuthCalls reports whether an interaction calls an operation whose credentials are optional, whose
// recording may lack them.
func (d Document) HasOptionalAuthCalls() bool {
return slices.ContainsFunc(d.InteractionCalls, func(ic InteractionCall) bool { return ic.Op.Auth != "" && ic.Op.AuthOptional })
}

// EncodesItself reports whether the type has a MarshalJSONTo method of its own.
func (s Schema) EncodesItself() bool {
return s.Tagged != nil || s.AllOfUnion != nil || s.Unimplemented != ""
Expand Down
15 changes: 0 additions & 15 deletions ir/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,3 @@ func (op Operation) BodilessFailures() Responses {

return rs
}

// NeedsBodyServer reports whether a generated test answers with a body of its choosing, see newBodyServer.
func (doc Document) NeedsBodyServer() bool {
return doc.HasErrorResponses() || doc.HasStructSuccess() || slices.ContainsFunc(doc.Operations, func(op Operation) bool {
return len(op.BodilessFailures()) > 0 || doc.DebugDecoded(op) != nil
})
}

// NeedsUnknownMember reports whether a generated test sends a value with a member no type declares, see
// withUnknownMember.
func (doc Document) NeedsUnknownMember() bool {
return doc.HasStructSuccess() || slices.ContainsFunc(doc.Operations, func(op Operation) bool {
return slices.ContainsFunc(op.ErrorResponses(), doc.LenientError)
})
}
8 changes: 0 additions & 8 deletions ir/testcases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,4 @@ func TestDocument_DebugAndBodilessResponses(t *testing.T) {
if got := op.BodilessFailures(); len(got) != 1 || got[0].StatusCode != "409" {
t.Errorf("got %+v, want the 409 alone", got)
}

if !debug.NeedsBodyServer() || !debug.NeedsUnknownMember() {
t.Error("want both helpers")
}

if (ir.Document{}).NeedsBodyServer() || (ir.Document{}).NeedsUnknownMember() {
t.Error("no operations: want neither helper")
}
}
1 change: 1 addition & 0 deletions render/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var importPaths = map[string]string{
"uuid": "uuid",

"api": "github.com/go-api-libs/api",
"apitest": "github.com/go-api-libs/api/apitest",
"cassette": "github.com/MarkRosemaker/cassette",
"civil": "cloud.google.com/go/civil",
"jsonutil": "github.com/MarkRosemaker/jsonutil",
Expand Down
9 changes: 3 additions & 6 deletions render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,9 @@ func TestFiles_ClientTest(t *testing.T) {
t.Error("missing package declaration")
}

if !contains(content, "func newTestServer(") {
t.Error("missing newTestServer helper")
}

if !contains(content, "type roundTripFunc func(*http.Request) (*http.Response, error)") || !contains(content, "RoundTrip(") {
t.Error("missing testRoundTripper")
// the helpers come from apitest, not from the generated file
if !contains(content, "apitest.NewServer(t, ") || !contains(content, "apitest.RoundTripFunc(") || contains(content, "func newTestServer(") {
t.Error("not using the helpers of apitest")
}

if !contains(content, `"ListPets"`) {
Expand Down
Loading
Loading