Skip to content

Commit

Permalink
Add support for embedded struct pointers (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerekStrickland committed Aug 7, 2021
1 parent 7fd2ca1 commit fba0a14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions jsoninfo/field_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type FieldInfo struct {
}

func AppendFields(fields []FieldInfo, parentIndex []int, t reflect.Type) []FieldInfo {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
// For each field
numField := t.NumField()
iteration:
Expand Down
30 changes: 30 additions & 0 deletions openapi3gen/openapi3gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ func TestEmbeddedStructs(t *testing.T) {
require.Equal(t, true, ok)
}

func TestEmbeddedPointerStructs(t *testing.T) {
type EmbeddedStruct struct {
ID string
}

type ContainerStruct struct {
Name string
*EmbeddedStruct
}

instance := &ContainerStruct{
Name: "Container",
EmbeddedStruct: &EmbeddedStruct{
ID: "Embedded",
},
}

generator := NewGenerator(UseAllExportedFields())

schemaRef, err := generator.GenerateSchemaRef(reflect.TypeOf(instance))
require.NoError(t, err)

var ok bool
_, ok = schemaRef.Value.Properties["Name"]
require.Equal(t, true, ok)

_, ok = schemaRef.Value.Properties["ID"]
require.Equal(t, true, ok)
}

func TestCyclicReferences(t *testing.T) {
type ObjectDiff struct {
FieldCycle *ObjectDiff
Expand Down

0 comments on commit fba0a14

Please sign in to comment.