Skip to content

Commit

Permalink
fix: embedded struct handling (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma committed Oct 11, 2022
1 parent b31a4bb commit fc05f1c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
9 changes: 6 additions & 3 deletions jsoninfo/field_info.go
Expand Up @@ -35,11 +35,14 @@ iteration:

// See whether this is an embedded field
if f.Anonymous {
if f.Tag.Get("json") == "-" {
jsonTag := f.Tag.Get("json")
if jsonTag == "-" {
continue
}
fields = AppendFields(fields, index, f.Type)
continue iteration
if jsonTag == "" {
fields = AppendFields(fields, index, f.Type)
continue iteration
}
}

// Ignore certain types
Expand Down
23 changes: 22 additions & 1 deletion openapi3gen/openapi3gen_test.go
Expand Up @@ -18,6 +18,12 @@ import (

func ExampleGenerator_SchemaRefs() {
type SomeOtherType string
type Embedded struct {
Z string `json:"z"`
}
type Embedded2 struct {
A string `json:"a"`
}
type SomeStruct struct {
Bool bool `json:"bool"`
Int int `json:"int"`
Expand All @@ -38,6 +44,10 @@ func ExampleGenerator_SchemaRefs() {
Y string
} `json:"structWithoutFields"`

Embedded `json:"embedded"`

Embedded2

Ptr *SomeOtherType `json:"ptr"`
}

Expand All @@ -54,16 +64,27 @@ func ExampleGenerator_SchemaRefs() {
}
fmt.Printf("schemaRef: %s\n", data)
// Output:
// g.SchemaRefs: 15
// g.SchemaRefs: 16
// schemaRef: {
// "properties": {
// "a": {
// "type": "string"
// },
// "bool": {
// "type": "boolean"
// },
// "bytes": {
// "format": "byte",
// "type": "string"
// },
// "embedded": {
// "properties": {
// "z": {
// "type": "string"
// }
// },
// "type": "object"
// },
// "float64": {
// "format": "double",
// "type": "number"
Expand Down

0 comments on commit fc05f1c

Please sign in to comment.