Skip to content

Commit fc05f1c

Browse files
authored
fix: embedded struct handling (#630)
1 parent b31a4bb commit fc05f1c

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

jsoninfo/field_info.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ iteration:
3535

3636
// See whether this is an embedded field
3737
if f.Anonymous {
38-
if f.Tag.Get("json") == "-" {
38+
jsonTag := f.Tag.Get("json")
39+
if jsonTag == "-" {
3940
continue
4041
}
41-
fields = AppendFields(fields, index, f.Type)
42-
continue iteration
42+
if jsonTag == "" {
43+
fields = AppendFields(fields, index, f.Type)
44+
continue iteration
45+
}
4346
}
4447

4548
// Ignore certain types

openapi3gen/openapi3gen_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import (
1818

1919
func ExampleGenerator_SchemaRefs() {
2020
type SomeOtherType string
21+
type Embedded struct {
22+
Z string `json:"z"`
23+
}
24+
type Embedded2 struct {
25+
A string `json:"a"`
26+
}
2127
type SomeStruct struct {
2228
Bool bool `json:"bool"`
2329
Int int `json:"int"`
@@ -38,6 +44,10 @@ func ExampleGenerator_SchemaRefs() {
3844
Y string
3945
} `json:"structWithoutFields"`
4046

47+
Embedded `json:"embedded"`
48+
49+
Embedded2
50+
4151
Ptr *SomeOtherType `json:"ptr"`
4252
}
4353

@@ -54,16 +64,27 @@ func ExampleGenerator_SchemaRefs() {
5464
}
5565
fmt.Printf("schemaRef: %s\n", data)
5666
// Output:
57-
// g.SchemaRefs: 15
67+
// g.SchemaRefs: 16
5868
// schemaRef: {
5969
// "properties": {
70+
// "a": {
71+
// "type": "string"
72+
// },
6073
// "bool": {
6174
// "type": "boolean"
6275
// },
6376
// "bytes": {
6477
// "format": "byte",
6578
// "type": "string"
6679
// },
80+
// "embedded": {
81+
// "properties": {
82+
// "z": {
83+
// "type": "string"
84+
// }
85+
// },
86+
// "type": "object"
87+
// },
6788
// "float64": {
6889
// "format": "double",
6990
// "type": "number"

0 commit comments

Comments
 (0)