Skip to content

Commit

Permalink
fix: use tag to determine if to flatten or not an embedded anonymous …
Browse files Browse the repository at this point in the history
…field (#85)
  • Loading branch information
vladshub authored Sep 6, 2022
1 parent 9301be1 commit 36d2229
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,10 @@ func (g *Generator) flattenStructSchema(t, parent reflect.Type, schema *Schema)
ft = ft.Elem()
}
isUnexported := f.PkgPath != ""
mediaTag := mediaTags[tonic.MediaType()]
_, hasTag := f.Tag.Lookup(mediaTag)

if f.Anonymous {
if f.Anonymous && !hasTag {
if isUnexported && ft.Kind() != reflect.Struct {
// Ignore embedded fields of unexported non-struct types.
continue
Expand Down Expand Up @@ -1038,7 +1040,7 @@ func (g *Generator) flattenStructSchema(t, parent reflect.Type, schema *Schema)
// Ignore unexported non-embedded fields.
continue
}
fname := fieldNameFromTag(f, mediaTags[tonic.MediaType()])
fname := fieldNameFromTag(f, mediaTag)
if fname == "" {
// Field has no name, skip it.
continue
Expand Down
8 changes: 8 additions & 0 deletions openapi/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type (
*u
uu *u // ignored, unexported field
q // ignored, embedded field of non-struct type
*Q
*V `json:"data"`
}
Y struct {
H float32 `validate:"required"`
Expand All @@ -62,6 +64,12 @@ type (
M int `json:"-"`
}
Z map[string]*Y
Q struct {
NnNnnN string `json:"nnNnnN"`
}
V struct {
L int
}
)

func (*X) TypeName() string { return "XXX" }
Expand Down
6 changes: 6 additions & 0 deletions testdata/schemas/X.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@
"S": {
"type": "integer",
"format": "int32"
},
"nnNnnN":{
"type":"string"
},
"data": {
"$ref": "#/components/schemas/V"
}
},
"required": [
Expand Down

0 comments on commit 36d2229

Please sign in to comment.