Skip to content

Commit fba0a14

Browse files
Add support for embedded struct pointers (#396)
1 parent 7fd2ca1 commit fba0a14

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

jsoninfo/field_info.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type FieldInfo struct {
2121
}
2222

2323
func AppendFields(fields []FieldInfo, parentIndex []int, t reflect.Type) []FieldInfo {
24+
if t.Kind() == reflect.Ptr {
25+
t = t.Elem()
26+
}
2427
// For each field
2528
numField := t.NumField()
2629
iteration:

openapi3gen/openapi3gen_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,36 @@ func TestEmbeddedStructs(t *testing.T) {
8585
require.Equal(t, true, ok)
8686
}
8787

88+
func TestEmbeddedPointerStructs(t *testing.T) {
89+
type EmbeddedStruct struct {
90+
ID string
91+
}
92+
93+
type ContainerStruct struct {
94+
Name string
95+
*EmbeddedStruct
96+
}
97+
98+
instance := &ContainerStruct{
99+
Name: "Container",
100+
EmbeddedStruct: &EmbeddedStruct{
101+
ID: "Embedded",
102+
},
103+
}
104+
105+
generator := NewGenerator(UseAllExportedFields())
106+
107+
schemaRef, err := generator.GenerateSchemaRef(reflect.TypeOf(instance))
108+
require.NoError(t, err)
109+
110+
var ok bool
111+
_, ok = schemaRef.Value.Properties["Name"]
112+
require.Equal(t, true, ok)
113+
114+
_, ok = schemaRef.Value.Properties["ID"]
115+
require.Equal(t, true, ok)
116+
}
117+
88118
func TestCyclicReferences(t *testing.T) {
89119
type ObjectDiff struct {
90120
FieldCycle *ObjectDiff

0 commit comments

Comments
 (0)