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

Add support for embedded struct pointers #396

Merged
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
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