Skip to content

Commit 47bb0b2

Browse files
Panic with customizer and embedded structs (#434)
1 parent 9b46ae7 commit 47bb0b2

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

openapi3gen/openapi3gen.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ func (g *Generator) generateSchemaRefFor(parents []*jsoninfo.TypeInfo, t reflect
103103
return ref, err
104104
}
105105

106+
func getStructField(t reflect.Type, fieldInfo jsoninfo.FieldInfo) reflect.StructField {
107+
var ff reflect.StructField
108+
// fieldInfo.Index is an array of indexes starting from the root of the type
109+
for i := 0; i < len(fieldInfo.Index); i++ {
110+
ff = t.Field(fieldInfo.Index[i])
111+
t = ff.Type
112+
}
113+
return ff
114+
}
115+
106116
func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflect.Type, name string, tag reflect.StructTag) (*openapi3.SchemaRef, error) {
107117
typeInfo := jsoninfo.GetTypeInfo(t)
108118
for _, parent := range parents {
@@ -266,7 +276,7 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
266276
schema.WithPropertyRef(fieldName, ref)
267277
}
268278
} else {
269-
ff := t.Field(fieldInfo.Index[len(fieldInfo.Index)-1])
279+
ff := getStructField(t, fieldInfo)
270280
if tag, ok := ff.Tag.Lookup("yaml"); ok && tag != "-" {
271281
fieldName, fType = tag, ff.Type
272282
}
@@ -276,7 +286,7 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
276286
// extract the field tag if we have a customizer
277287
var fieldTag reflect.StructTag
278288
if g.opts.schemaCustomizer != nil {
279-
ff := t.Field(fieldInfo.Index[len(fieldInfo.Index)-1])
289+
ff := getStructField(t, fieldInfo)
280290
fieldTag = ff.Tag
281291
}
282292

openapi3gen/openapi3gen_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,23 @@ func TestCyclicReferences(t *testing.T) {
150150
}
151151

152152
func TestSchemaCustomizer(t *testing.T) {
153-
type Bla struct {
153+
type NestedInnerBla struct {
154+
Enum1Field string `json:"enum1" myenumtag:"a,b"`
155+
}
156+
157+
type InnerBla struct {
154158
UntaggedStringField string
155159
AnonStruct struct {
156160
InnerFieldWithoutTag int
157161
InnerFieldWithTag int `mymintag:"-1" mymaxtag:"50"`
162+
NestedInnerBla
158163
}
159-
EnumField string `json:"another" myenumtag:"a,b"`
164+
Enum2Field string `json:"enum2" myenumtag:"c,d"`
165+
}
166+
167+
type Bla struct {
168+
InnerBla
169+
EnumField3 string `json:"enum3" myenumtag:"e,f"`
160170
}
161171

162172
schemaRef, _, err := NewSchemaRefForValue(&Bla{}, UseAllExportedFields(), SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error {
@@ -196,17 +206,31 @@ func TestSchemaCustomizer(t *testing.T) {
196206
},
197207
"InnerFieldWithoutTag": {
198208
"type": "integer"
199-
}
209+
},
210+
"enum1": {
211+
"enum": [
212+
"a",
213+
"b"
214+
],
215+
"type": "string"
216+
}
200217
},
201218
"type": "object"
202219
},
203220
"UntaggedStringField": {
204221
"type": "string"
205222
},
206-
"another": {
223+
"enum2": {
224+
"enum": [
225+
"c",
226+
"d"
227+
],
228+
"type": "string"
229+
},
230+
"enum3": {
207231
"enum": [
208-
"a",
209-
"b"
232+
"e",
233+
"f"
210234
],
211235
"type": "string"
212236
}

0 commit comments

Comments
 (0)