diff --git a/packages.go b/packages.go index 74439d442..20324140c 100644 --- a/packages.go +++ b/packages.go @@ -382,7 +382,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe continue } - //delete it from parsed schemas, and will parse it again + // delete it from parsed schemas, and will parse it again if _, ok = parsedSchemas[typeDef]; ok { delete(parsedSchemas, typeDef) } @@ -498,7 +498,7 @@ func (pkgDefs *PackagesDefinitions) findPackagePathFromImports(pkg string, file } break } else if imp.Name.Name == "_" && len(pkg) > 0 { - //for unused types + // for unused types pd, ok := pkgDefs.packages[path] if ok { if pd.Name == pkg { @@ -598,8 +598,12 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File return pkgDefs.parametrizeGenericType(file, typeDef, typeName) } - //in case that comment //@name renamed the type with a name without a dot - for _, v := range pkgDefs.uniqueDefinitions { + // in case that comment //@name renamed the type with a name without a dot + for k, v := range pkgDefs.uniqueDefinitions { + if v == nil { + pkgDefs.debug.Printf("%s TypeSpecDef is nil", k) + continue + } if v.SchemaName == typeName { return v } diff --git a/parser.go b/parser.go index b99405769..eb5d96c89 100644 --- a/parser.go +++ b/parser.go @@ -1294,7 +1294,10 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error) } if definition.Description == "" { - fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef) + err = parser.fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef) + if err != nil { + return nil, err + } } if len(typeSpecDef.Enums) > 0 { @@ -1344,7 +1347,7 @@ func fullTypeName(parts ...string) string { // fillDefinitionDescription additionally fills fields in definition (spec.Schema) // TODO: If .go file contains many types, it may work for a long time -func fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) { +func (parser *Parser) fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) (err error) { if file == nil { return } @@ -1359,16 +1362,23 @@ func fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpec if !ok || typeSpec != typeSpecDef.TypeSpec { continue } - - definition.Description = - extractDeclarationDescription(typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc) + var typeName string + if typeSpec.Name != nil { + typeName = typeSpec.Name.Name + } + definition.Description, err = + parser.extractDeclarationDescription(typeName, typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc) + if err != nil { + return + } } } + return nil } // extractDeclarationDescription gets first description // from attribute descriptionAttr in commentGroups (ast.CommentGroup) -func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string { +func (parser *Parser) extractDeclarationDescription(typeName string, commentGroups ...*ast.CommentGroup) (string, error) { var description string for _, commentGroup := range commentGroups { @@ -1383,9 +1393,23 @@ func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string { if len(commentText) == 0 { continue } - attribute := FieldsByAnySpace(commentText, 2)[0] + fields := FieldsByAnySpace(commentText, 2) + attribute := fields[0] - if strings.ToLower(attribute) != descriptionAttr { + if attr := strings.ToLower(attribute); attr == descriptionMarkdownAttr { + if len(fields) > 1 { + typeName = fields[1] + } + if typeName == "" { + continue + } + desc, err := getMarkdownForTag(typeName, parser.markdownFileDir) + if err != nil { + return "", err + } + // if found markdown description, we will only use the markdown file content + return string(desc), nil + } else if attr != descriptionAttr { if !isHandlingDescription { continue } @@ -1398,7 +1422,7 @@ func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string { } } - return strings.TrimLeft(description, " ") + return strings.TrimLeft(description, " "), nil } // parseTypeExpr parses given type expression that corresponds to the type under