From 640f858c6b0bb932be8a4f8b6e71d48b32aa6a93 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:19:23 +0000 Subject: [PATCH 1/2] test: generated tests check each union's null, and that nothing is dropped Every union gets a generated Null check: holding no alternative, it encodes as null, and null decodes as it, as an API sends a member it leaves unset. apitest.RoundTrip, bumped, now also checks each round-tripped variant refuses or keeps a member no type declares, never drops it. In notion, these reach the null and error paths of every union's decoder, which no test did: coverage, as devtool measures it, goes from 77.8% to 83.6%. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- go.mod | 2 +- go.sum | 4 +- ir/roundtrip.go | 14 +++++ ir/roundtrip_test.go | 15 +++++ render/templates/types.gen_test.go.tmpl | 12 +++- testdata/debug/pkg/debug/types.gen_test.go | 6 ++ testdata/golden/pkg/golden/types.gen_test.go | 25 +++++++++ vendor/github.com/go-api-libs/api/.gitignore | 4 ++ .../go-api-libs/api/apitest/apitest.go | 56 ++++++++++++++++++- .../github.com/go-api-libs/api/devtool.json | 4 +- vendor/modules.txt | 2 +- 11 files changed, 135 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index e069cfc8..957076a4 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/MarkRosemaker/openapi-flatten v0.0.0-20261115155612-0c3e32687df2 github.com/MarkRosemaker/ordmap v0.0.0-20261115155501-75016eeac082 github.com/ettle/strcase v0.2.0 - github.com/go-api-libs/api v0.0.0-20261115030148-02c29b673d3a + github.com/go-api-libs/api v0.0.0-20261115171803-1c01d6b4ebb9 github.com/spf13/afero v1.15.0 mvdan.cc/gofumpt v0.12.0 ) diff --git a/go.sum b/go.sum index 1ae4571a..ab73e562 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,8 @@ github.com/MarkRosemaker/yaml2json v0.0.0-20261115155441-455cb115d85f h1:RkHc4G9 github.com/MarkRosemaker/yaml2json v0.0.0-20261115155441-455cb115d85f/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-20261115030148-02c29b673d3a h1:Thu2LNeFzxiWHfVKhRmIu16/N7Rf6vUo5jghCroFc+k= -github.com/go-api-libs/api v0.0.0-20261115030148-02c29b673d3a/go.mod h1:rzjWUX+9lLTr+YF9mMw1rvNNDcnYpEHPGl7OA9qj9ZQ= +github.com/go-api-libs/api v0.0.0-20261115171803-1c01d6b4ebb9 h1:isSiznoAMquyShP+g2XGuBykSRBpaufRhKeRgz7i2ek= +github.com/go-api-libs/api v0.0.0-20261115171803-1c01d6b4ebb9/go.mod h1:4Ls/f+97MK0jpUbKa713C/itwrFlVvHwxt+CWLXdsr0= github.com/go-api-libs/types v0.0.0-20261115155441-34096c707114 h1:34HwNaBU9wBypPiBH7ljbwFZMpRlbpy47+M+ioCH08s= github.com/go-api-libs/types v0.0.0-20261115155441-34096c707114/go.mod h1:z0z6qkZc45AvoRnluq7PkVSlSSF25sT0diJ+vrubYfA= github.com/go-quicktest/qt v1.102.0 h1:HSQxCeh5YZH3EL3W39ixjtyaEhcWSXQHtHnMBzSs474= diff --git a/ir/roundtrip.go b/ir/roundtrip.go index c12425df..559312e0 100644 --- a/ir/roundtrip.go +++ b/ir/roundtrip.go @@ -64,6 +64,20 @@ func (doc Document) UnionRoundTrips() []UnionRoundTrip { return out } +// NullUnions are the unions a generated test checks encode as null while they hold no alternative, and decode null as +// holding none. +func (doc Document) NullUnions() []string { + var out []string + + for _, s := range doc.Schemas { + if s.Kind == SchemaKindUnion && s.Tagged == nil && s.Unimplemented == "" { + out = append(out, s.Name) + } + } + + return out +} + // TaggedRoundTrip is a struct told apart by a tag a generated test sets each value of in turn, with the members that // value requires, encodes and decodes strictly: the tag must come back, and the struct encode the same. type TaggedRoundTrip struct { diff --git a/ir/roundtrip_test.go b/ir/roundtrip_test.go index c428ff37..f215e83c 100644 --- a/ir/roundtrip_test.go +++ b/ir/roundtrip_test.go @@ -168,3 +168,18 @@ func TestDocument_RoundTripsBuildNestedValues(t *testing.T) { t.Errorf("got %+v, want Value's number tag with its number set", tagged) } } + +func TestDocument_NullUnions(t *testing.T) { + t.Parallel() + + doc := ir.Document{Schemas: []ir.Schema{ + {Name: "Pet", Kind: ir.SchemaKindUnion}, + {Name: "Block", Kind: ir.SchemaKindUnion, Tagged: &ir.Tagged{}}, // a struct, whose zero is an object + {Name: "Odd", Kind: ir.SchemaKindUnion, Unimplemented: "an allOf of two unions"}, + {Name: "Item", Kind: ir.SchemaKindStruct}, + }} + + if got := doc.NullUnions(); !slices.Equal(got, []string{"Pet"}) { + t.Errorf("NullUnions = %v, want [Pet]", got) + } +} diff --git a/render/templates/types.gen_test.go.tmpl b/render/templates/types.gen_test.go.tmpl index 96e2e135..d1060e92 100644 --- a/render/templates/types.gen_test.go.tmpl +++ b/render/templates/types.gen_test.go.tmpl @@ -1,4 +1,4 @@ -{{ if or .Enums .UnionRoundTrips .TaggedRoundTrips -}} +{{ if or .Enums .UnionRoundTrips .TaggedRoundTrips .NullUnions -}} // This file provides tests for the types. // // Code generated by MarkRosemaker/openapi-codegen. DO NOT EDIT. @@ -40,6 +40,16 @@ func TestUnions_RoundTrip(t *testing.T) { {{- end }} } {{- end }} +{{- with .NullUnions }} + +// TestUnions_Null checks that each union holding no alternative encodes as null, and that null decodes as one holding +// none, as for a member an API sends as null. +func TestUnions_Null(t *testing.T) { + {{- range . }} + apitest.Null[{{ . }}](t, jsonOpts) + {{- end }} +} +{{- end }} {{- with .TaggedRoundTrips }} // TestTagged_RoundTrip sets each tag of each struct told apart by one in turn, with the members the tag requires, diff --git a/testdata/debug/pkg/debug/types.gen_test.go b/testdata/debug/pkg/debug/types.gen_test.go index d6f2b9d8..da9cc082 100644 --- a/testdata/debug/pkg/debug/types.gen_test.go +++ b/testdata/debug/pkg/debug/types.gen_test.go @@ -67,6 +67,12 @@ func TestUnions_RoundTrip(t *testing.T) { }) } +// TestUnions_Null checks that each union holding no alternative encodes as null, and that null decodes as one holding +// none, as for a member an API sends as null. +func TestUnions_Null(t *testing.T) { + apitest.Null[Pet](t, jsonOpts) +} + // TestTagged_RoundTrip sets each tag of each struct told apart by one in turn, with the members the tag requires, // encodes it and decodes it strictly: the tag must come back, and the struct encode as before. func TestTagged_RoundTrip(t *testing.T) { diff --git a/testdata/golden/pkg/golden/types.gen_test.go b/testdata/golden/pkg/golden/types.gen_test.go index 35c77c35..ebfb9e9a 100644 --- a/testdata/golden/pkg/golden/types.gen_test.go +++ b/testdata/golden/pkg/golden/types.gen_test.go @@ -327,6 +327,31 @@ func TestUnions_RoundTrip(t *testing.T) { }) } +// TestUnions_Null checks that each union holding no alternative encodes as null, and that null decodes as one holding +// none, as for a member an API sends as null. +func TestUnions_Null(t *testing.T) { + apitest.Null[AgentModel](t, jsonOpts) + apitest.Null[Bot](t, jsonOpts) + apitest.Null[Entry](t, jsonOpts) + apitest.Null[Filter](t, jsonOpts) + apitest.Null[GroupBy](t, jsonOpts) + apitest.Null[IconResponse](t, jsonOpts) + apitest.Null[MaybeMember](t, jsonOpts) + apitest.Null[MemberAllOf1](t, jsonOpts) + apitest.Null[Node](t, jsonOpts) + apitest.Null[Order](t, jsonOpts) + apitest.Null[Owner](t, jsonOpts) + apitest.Null[PageOrPartial](t, jsonOpts) + apitest.Null[Pairs](t, jsonOpts) + apitest.Null[PeopleArrayBasedPropertyValueResponsePeopleItem](t, jsonOpts) + apitest.Null[Position](t, jsonOpts) + apitest.Null[Tags](t, jsonOpts) + apitest.Null[UserValueResponse](t, jsonOpts) + apitest.Null[ValueAllOf1](t, jsonOpts) + apitest.Null[ViewFilter](t, jsonOpts) + apitest.Null[ViewGroupBy](t, jsonOpts) +} + // TestTagged_RoundTrip sets each tag of each struct told apart by one in turn, with the members the tag requires, // encodes it and decodes it strictly: the tag must come back, and the struct encode as before. func TestTagged_RoundTrip(t *testing.T) { diff --git a/vendor/github.com/go-api-libs/api/.gitignore b/vendor/github.com/go-api-libs/api/.gitignore index f725515b..86290681 100644 --- a/vendor/github.com/go-api-libs/api/.gitignore +++ b/vendor/github.com/go-api-libs/api/.gitignore @@ -23,6 +23,10 @@ bin/ # macOS files *.DS_Store + +# What npm installs for a web app's smoke test +/smoke/node_modules/ +/smoke/package-lock.json # end generated by MarkRosemaker/devtool # Test binary, built with 'go test -c' diff --git a/vendor/github.com/go-api-libs/api/apitest/apitest.go b/vendor/github.com/go-api-libs/api/apitest/apitest.go index 9ef131e4..fe892d45 100644 --- a/vendor/github.com/go-api-libs/api/apitest/apitest.go +++ b/vendor/github.com/go-api-libs/api/apitest/apitest.go @@ -8,6 +8,8 @@ import ( "net/http" "net/http/httptest" "net/url" + "reflect" + "strings" "testing" ) @@ -52,7 +54,10 @@ type RoundTripFunc func(*http.Request) (*http.Response, error) func (f RoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } // unknownMember is a member no type declares. -const unknownMember = `"not_a_member_of_any_type": true` +const ( + unknownMemberName = `"not_a_member_of_any_type"` + unknownMember = unknownMemberName + `: true` +) // WithUnknownMember is v as JSON, encoded as opts say, with a member no type declares added to it. func WithUnknownMember(t testing.TB, v any, opts json.Options) []byte { @@ -84,7 +89,9 @@ func MustDecode[T any](t testing.TB, data string, opts json.Options) T { } // RoundTrip encodes in and decodes it strictly into a new value, both as opts say. set must report that the new value -// holds the variant in did, and it must encode as in did. +// holds the variant in did, and it must encode as in did. Where in encodes as an object, decoding it with a member no +// type declares must fail, or keep the member, as a type of additional properties does: a strict decoder drops +// nothing. func RoundTrip[T any](t testing.TB, in T, set func(T) bool, opts json.Options) { t.Helper() @@ -110,4 +117,49 @@ func RoundTrip[T any](t testing.TB, in T, set func(T) bool, opts json.Options) { if string(again) != string(raw) { t.Errorf("encoded %s, then %s", raw, again) } + + // a member no type declares is refused, or kept by a type that keeps what it does not declare: never dropped + if len(raw) == 0 || raw[0] != '{' { + return + } + + extra := WithUnknownMember(t, &got, opts) + + var with T + if err := json.Unmarshal(extra, &with, opts); err != nil { + return + } + + kept, err := json.Marshal(&with, opts) + if err != nil { + t.Fatal(err) + } + + if !strings.Contains(string(kept), unknownMemberName) { + t.Errorf("%s decoded, dropping the member no type declares: %s", extra, kept) + } +} + +// Null checks the zero T, a union holding no alternative, encodes as null, and null decodes as a T holding none, as +// opts say. +func Null[T any](t testing.TB, opts json.Options) { + t.Helper() + + var zero T + + raw, err := json.Marshal(&zero, opts) + if err != nil { + t.Fatal(err) + } + + if string(raw) != "null" { + t.Errorf("the zero %T encoded as %s, want null", zero, raw) + } + + var got T + if err := json.Unmarshal([]byte("null"), &got, opts); err != nil { + t.Errorf("null: %v", err) + } else if !reflect.ValueOf(got).IsZero() { + t.Errorf("null decoded as %+v", got) + } } diff --git a/vendor/github.com/go-api-libs/api/devtool.json b/vendor/github.com/go-api-libs/api/devtool.json index a26fdd18..3caee087 100644 --- a/vendor/github.com/go-api-libs/api/devtool.json +++ b/vendor/github.com/go-api-libs/api/devtool.json @@ -1,6 +1,6 @@ { - "coverage": 90.5, - "devtoolVersion": "v0.0.0-20261114134623-f0f0252835dc", + "coverage": 90.9, + "devtoolVersion": "v0.0.0-20261115154222-36316578e5dc", "kind": "apilib", "tools": { "golangci-lint": "v2.15.0", diff --git a/vendor/modules.txt b/vendor/modules.txt index 0d13c676..59938426 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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-20261115030148-02c29b673d3a +# github.com/go-api-libs/api v0.0.0-20261115171803-1c01d6b4ebb9 ## explicit; go 1.27 github.com/go-api-libs/api github.com/go-api-libs/api/apitest From 06ca7521ce6d4467b4a9c13c0d93182ae35acaf6 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 15 Nov 2026 17:22:12 +0000 Subject: [PATCH 2/2] chore: bump go-api-libs/api to master, with apitest.Null merged Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_019QCb1AfnATvpBQCH3PrX7Y --- go.mod | 2 +- go.sum | 4 ++-- vendor/modules.txt | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 957076a4..166590af 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/MarkRosemaker/openapi-flatten v0.0.0-20261115155612-0c3e32687df2 github.com/MarkRosemaker/ordmap v0.0.0-20261115155501-75016eeac082 github.com/ettle/strcase v0.2.0 - github.com/go-api-libs/api v0.0.0-20261115171803-1c01d6b4ebb9 + github.com/go-api-libs/api v0.0.0-20261115172114-130f6fc73195 github.com/spf13/afero v1.15.0 mvdan.cc/gofumpt v0.12.0 ) diff --git a/go.sum b/go.sum index ab73e562..fef8e9e2 100644 --- a/go.sum +++ b/go.sum @@ -30,8 +30,8 @@ github.com/MarkRosemaker/yaml2json v0.0.0-20261115155441-455cb115d85f h1:RkHc4G9 github.com/MarkRosemaker/yaml2json v0.0.0-20261115155441-455cb115d85f/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-20261115171803-1c01d6b4ebb9 h1:isSiznoAMquyShP+g2XGuBykSRBpaufRhKeRgz7i2ek= -github.com/go-api-libs/api v0.0.0-20261115171803-1c01d6b4ebb9/go.mod h1:4Ls/f+97MK0jpUbKa713C/itwrFlVvHwxt+CWLXdsr0= +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/types v0.0.0-20261115155441-34096c707114 h1:34HwNaBU9wBypPiBH7ljbwFZMpRlbpy47+M+ioCH08s= github.com/go-api-libs/types v0.0.0-20261115155441-34096c707114/go.mod h1:z0z6qkZc45AvoRnluq7PkVSlSSF25sT0diJ+vrubYfA= github.com/go-quicktest/qt v1.102.0 h1:HSQxCeh5YZH3EL3W39ixjtyaEhcWSXQHtHnMBzSs474= diff --git a/vendor/modules.txt b/vendor/modules.txt index 59938426..26fd0081 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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-20261115171803-1c01d6b4ebb9 +# github.com/go-api-libs/api v0.0.0-20261115172114-130f6fc73195 ## explicit; go 1.27 github.com/go-api-libs/api github.com/go-api-libs/api/apitest