Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: embedded struct handling #630

Merged
merged 1 commit into from Oct 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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