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

Panic with customizer and embedded structs #434

Merged
merged 1 commit into from
Oct 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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