Skip to content

Commit

Permalink
add inline support and ref struct
Browse files Browse the repository at this point in the history
  • Loading branch information
fifteen-clement authored and schmurfy committed Dec 26, 2022
1 parent 7c72c24 commit 102e77f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
24 changes: 24 additions & 0 deletions builder/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ import (
"github.com/stretchr/testify/require"
)

type Parent struct {
Inline
Field3 string
Field4 int
}

type Inline struct {
Field1 string
Field2 int
}

func TestResponse(t *testing.T) {
g := goblin.Goblin(t)

Expand Down Expand Up @@ -128,5 +139,18 @@ func TestResponse(t *testing.T) {
require.Nil(g, mediaType)
})

g.It("should embed Inline struct", func() {
req := struct {
response.JsonEncoder
Response Parent
}{}

err := b.generateResponseDoc(ctx, b.swagger, op, &req, reflect.TypeOf(req), nil)
require.NoError(g, err)

require.NotNil(g, b.swagger.Components.Schemas[reflect.TypeOf(req.Response).String()])
require.Len(g, b.swagger.Components.Schemas[reflect.TypeOf(req.Response).String()].Value.Properties, 4)
require.NotNil(g, b.swagger.Components.Schemas[reflect.TypeOf(req.Response).String()].Value.Properties["Field1"])
})
})
}
17 changes: 10 additions & 7 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@ func (s *Schema) generateSchemaFor(ctx context.Context, doc *openapi3.T, t refle
}
}

if doc.Components.Schemas != nil {
cached, found := doc.Components.Schemas[fullName]
if found {
return cached, nil
}
}

schema := &openapi3.SchemaRef{}

// test pointed value for pointers
Expand Down Expand Up @@ -224,6 +217,16 @@ func (s *Schema) generateStructureSchema(ctx context.Context, doc *openapi3.T, t
continue
}

//Detect if field is anonymous, look into the schemas and use the same property
if f.Anonymous && fieldSchema.Ref != "" && doc.Components.Schemas[f.Type.String()] != nil && doc.Components.Schemas[f.Type.String()].Value != nil {
for name, property := range doc.Components.Schemas[f.Type.String()].Value.Properties {
ret.WithPropertyRef(name, property)
}

//Ignore the anonymous field
continue
}

if fieldSchema.Ref != "" {
if (tag.Nullable != nil) && *tag.Nullable {
// nullableSchemaRef := openapi3.NewSchemaRef("", &openapi3.Schema{
Expand Down

0 comments on commit 102e77f

Please sign in to comment.