Skip to content

Commit

Permalink
Panic with customizer and embedded structs (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbroadhurst authored Oct 7, 2021
1 parent 9b46ae7 commit 47bb0b2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
14 changes: 12 additions & 2 deletions openapi3gen/openapi3gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ func (g *Generator) generateSchemaRefFor(parents []*jsoninfo.TypeInfo, t reflect
return ref, err
}

func getStructField(t reflect.Type, fieldInfo jsoninfo.FieldInfo) reflect.StructField {
var ff reflect.StructField
// fieldInfo.Index is an array of indexes starting from the root of the type
for i := 0; i < len(fieldInfo.Index); i++ {
ff = t.Field(fieldInfo.Index[i])
t = ff.Type
}
return ff
}

func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflect.Type, name string, tag reflect.StructTag) (*openapi3.SchemaRef, error) {
typeInfo := jsoninfo.GetTypeInfo(t)
for _, parent := range parents {
Expand Down Expand Up @@ -266,7 +276,7 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
schema.WithPropertyRef(fieldName, ref)
}
} else {
ff := t.Field(fieldInfo.Index[len(fieldInfo.Index)-1])
ff := getStructField(t, fieldInfo)
if tag, ok := ff.Tag.Lookup("yaml"); ok && tag != "-" {
fieldName, fType = tag, ff.Type
}
Expand All @@ -276,7 +286,7 @@ func (g *Generator) generateWithoutSaving(parents []*jsoninfo.TypeInfo, t reflec
// extract the field tag if we have a customizer
var fieldTag reflect.StructTag
if g.opts.schemaCustomizer != nil {
ff := t.Field(fieldInfo.Index[len(fieldInfo.Index)-1])
ff := getStructField(t, fieldInfo)
fieldTag = ff.Tag
}

Expand Down
36 changes: 30 additions & 6 deletions openapi3gen/openapi3gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,23 @@ func TestCyclicReferences(t *testing.T) {
}

func TestSchemaCustomizer(t *testing.T) {
type Bla struct {
type NestedInnerBla struct {
Enum1Field string `json:"enum1" myenumtag:"a,b"`
}

type InnerBla struct {
UntaggedStringField string
AnonStruct struct {
InnerFieldWithoutTag int
InnerFieldWithTag int `mymintag:"-1" mymaxtag:"50"`
NestedInnerBla
}
EnumField string `json:"another" myenumtag:"a,b"`
Enum2Field string `json:"enum2" myenumtag:"c,d"`
}

type Bla struct {
InnerBla
EnumField3 string `json:"enum3" myenumtag:"e,f"`
}

schemaRef, _, err := NewSchemaRefForValue(&Bla{}, UseAllExportedFields(), SchemaCustomizer(func(name string, ft reflect.Type, tag reflect.StructTag, schema *openapi3.Schema) error {
Expand Down Expand Up @@ -196,17 +206,31 @@ func TestSchemaCustomizer(t *testing.T) {
},
"InnerFieldWithoutTag": {
"type": "integer"
}
},
"enum1": {
"enum": [
"a",
"b"
],
"type": "string"
}
},
"type": "object"
},
"UntaggedStringField": {
"type": "string"
},
"another": {
"enum2": {
"enum": [
"c",
"d"
],
"type": "string"
},
"enum3": {
"enum": [
"a",
"b"
"e",
"f"
],
"type": "string"
}
Expand Down

0 comments on commit 47bb0b2

Please sign in to comment.