From 9d2067e66eef402856de5d210e80ad2b293a0b9e Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 22 Oct 2014 10:45:26 -0400 Subject: [PATCH] Fix of api docs gen bug on object description of structs with anonymous fields Added json:"-" as omitter on api docs gen of struct field for object description Avoid use json:"omitempty" on api docs gen as object description field name --- g_docs.go | 56 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/g_docs.go b/g_docs.go index 39cf85f..c370696 100644 --- a/g_docs.go +++ b/g_docs.go @@ -561,27 +561,51 @@ func getModel(str string) (pkgpath, objectname string, m swagger.Model, realType } else { mp.Type = realType } - // if the tag contains json tag, set the name to the left most json tag - var name = field.Names[0].Name - if field.Tag != nil { + + // dont add property if anonymous field + if field.Names != nil { + + // set property name as field name + var name = field.Names[0].Name + + // if no tag skip tag processing + if field.Tag == nil { + m.Properties[name] = mp + continue + } + + var tagValues []string stag := reflect.StructTag(strings.Trim(field.Tag.Value, "`")) - if tag := stag.Get("json"); tag != "" { - name = tag + tag := stag.Get("json") + + if tag != "" { + tagValues = strings.Split(tag, ",") } - if thrifttag := stag.Get("thrift"); thrifttag != "" { - ts := strings.Split(thrifttag, ",") - if ts[0] != "" { - name = ts[0] + + // dont add property if json tag first value is "-" + if len(tagValues) == 0 || tagValues[0] != "-" { + + // set property name to the left most json tag value only if is not omitempty + if len(tagValues) > 0 && tagValues[0] != "omitempty" { + name = tagValues[0] } - } - if required := stag.Get("required"); required != "" { - m.Required = append(m.Required, name) - } - if desc := stag.Get("description"); desc != "" { - mp.Description = desc + + if thrifttag := stag.Get("thrift"); thrifttag != "" { + ts := strings.Split(thrifttag, ",") + if ts[0] != "" { + name = ts[0] + } + } + if required := stag.Get("required"); required != "" { + m.Required = append(m.Required, name) + } + if desc := stag.Get("description"); desc != "" { + mp.Description = desc + } + + m.Properties[name] = mp } } - m.Properties[name] = mp } } return