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
33 changes: 33 additions & 0 deletions docparse/docparse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,39 @@ func TestGetReference(t *testing.T) {
}
}

// Regression: an embedded qualified pointer type tagged json:"-" (for
// example *mail.Address with a json:"-" tag) previously caused a
// nil-pointer panic because the first field-walk pass tried to resolve
// the embed before checking the tag. The fix is to honor json:"-" in
// that pass too. We also verify the embed is absent from the resulting
// Reference.Fields and the referenced external type is not pulled into
// prog.References.
func TestGetReference_EmbedJSONDash(t *testing.T) {
prog := NewProgram(false)
prog.Config.StructTag = "json"
out, err := GetReference(prog, "req", false, "testEmbedJSONDash", ".")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if out == nil {
t.Fatal("nil reference")
}

var names []string
for _, f := range out.Fields {
names = append(names, f.Name)
}
wantFields := []string{"ID"}
if !reflect.DeepEqual(names, wantFields) {
t.Errorf("fields: got %v, want %v", names, wantFields)
}

if _, ok := prog.References["mail.Address"]; ok {
t.Errorf("mail.Address was resolved despite json:\"-\" on embed")
}
}

func TestParseResponse(t *testing.T) {
tests := []struct {
in string
Expand Down
9 changes: 9 additions & 0 deletions docparse/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ func GetReference(prog *Program, context string, isEmbed bool, lookup, filePath
continue
}

// Skip embedded fields explicitly excluded from the schema
// (e.g. `json:"-"`). The nested-walk loop below applies the
// same skip; doing it here too avoids resolving a type we
// will not emit, which matters for embeds whose underlying
// type would otherwise pull in a large external graph.
if goutil.TagName(f, tagName) == "-" {
continue
}

switch t := f.Type.(type) {
case *ast.Ident:
err = resolveType(prog, context, false, t, "", pkg)
Expand Down
14 changes: 14 additions & 0 deletions docparse/test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package docparse

import "net/mail"

// For tests. We don't parse test files.

// testObject general documentation.
Expand All @@ -16,3 +18,15 @@ type testObject struct {

Bar []string
}

// testEmbedJSONDash covers the case where a struct embeds a qualified
// pointer type purely for Go-level method promotion and excludes it from
// the schema with `json:"-"`. The embed must not be resolved.
//
//nolint:unused
type testEmbedJSONDash struct {
*mail.Address `json:"-"`

// ID documentation {required}.
ID int
}
Loading