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-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-20261115172114-130f6fc73195
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-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-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=
Expand Down
14 changes: 14 additions & 0 deletions ir/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions ir/roundtrip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 11 additions & 1 deletion render/templates/types.gen_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions testdata/debug/pkg/debug/types.gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions testdata/golden/pkg/golden/types.gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/go-api-libs/api/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 54 additions & 2 deletions vendor/github.com/go-api-libs/api/apitest/apitest.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/go-api-libs/api/devtool.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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-20261115172114-130f6fc73195
## explicit; go 1.27
github.com/go-api-libs/api
github.com/go-api-libs/api/apitest
Expand Down
Loading