From e961d7eca926678f9fad0234d7a46b258f1d5af5 Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Mon, 10 Oct 2022 09:39:58 +0200 Subject: [PATCH] fix: embedded struct handling --- jsoninfo/field_info.go | 9 ++++++--- openapi3gen/openapi3gen_test.go | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/jsoninfo/field_info.go b/jsoninfo/field_info.go index 2382b731c..6b45f8c69 100644 --- a/jsoninfo/field_info.go +++ b/jsoninfo/field_info.go @@ -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 diff --git a/openapi3gen/openapi3gen_test.go b/openapi3gen/openapi3gen_test.go index a4c2d52e9..bfa3120ec 100644 --- a/openapi3gen/openapi3gen_test.go +++ b/openapi3gen/openapi3gen_test.go @@ -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"` @@ -38,6 +44,10 @@ func ExampleGenerator_SchemaRefs() { Y string } `json:"structWithoutFields"` + Embedded `json:"embedded"` + + Embedded2 + Ptr *SomeOtherType `json:"ptr"` } @@ -54,9 +64,12 @@ func ExampleGenerator_SchemaRefs() { } fmt.Printf("schemaRef: %s\n", data) // Output: - // g.SchemaRefs: 15 + // g.SchemaRefs: 16 // schemaRef: { // "properties": { + // "a": { + // "type": "string" + // }, // "bool": { // "type": "boolean" // }, @@ -64,6 +77,14 @@ func ExampleGenerator_SchemaRefs() { // "format": "byte", // "type": "string" // }, + // "embedded": { + // "properties": { + // "z": { + // "type": "string" + // } + // }, + // "type": "object" + // }, // "float64": { // "format": "double", // "type": "number"