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

feat: add generation of type description to schema (#708) #1106

Merged
merged 9 commits into from Jan 20, 2022
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -613,13 +613,37 @@ type Account struct {
### Description of struct

```go
// Account model info
// @Description User account information
// @Description with user id and username
type Account struct {
KuboOrk marked this conversation as resolved.
Show resolved Hide resolved
// ID this is userid
ID int `json:"id"`
Name string `json:"name"` // This is Name
}
```

[#708](https://github.com/swaggo/swag/issues/708) The parser handles only struct comments starting with `@Description` attribute.
But it writes all struct field comments as is.

So, generated swagger doc as follows:
```json
"Account": {
"type":"object",
"description": "User account information with user id and username"
"properties": {
"id": {
"type": "integer",
"description": "ID this is userid"
},
"name": {
"type":"string",
"description": "This is Name"
}
}
}
```

### Use swaggertype tag to supported custom type
[#201](https://github.com/swaggo/swag/issues/201#issuecomment-475479409)

Expand Down
54 changes: 54 additions & 0 deletions parser.go
Expand Up @@ -954,6 +954,10 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error)
return nil, err
}

if definition.Description == "" {
fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef)
}

s := Schema{
Name: refTypeName,
PkgPath: typeSpecDef.PkgPath,
Expand All @@ -978,6 +982,56 @@ func fullTypeName(pkgName, typeName string) string {
return typeName
}

// 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) {
for _, astDeclaration := range file.Decls {
generalDeclaration, ok := astDeclaration.(*ast.GenDecl)
if !ok || generalDeclaration.Tok != token.TYPE {
continue
}

for _, astSpec := range generalDeclaration.Specs {
typeSpec, ok := astSpec.(*ast.TypeSpec)
if !ok || typeSpec != typeSpecDef.TypeSpec {
continue
}

definition.Description =
extractDeclarationDescription(typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc)
}
}
}

// extractDeclarationDescription gets first description
// from attribute descriptionAttr in commentGroups (ast.CommentGroup)
func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
var description string

for _, commentGroup := range commentGroups {
KuboOrk marked this conversation as resolved.
Show resolved Hide resolved
if commentGroup == nil {
continue
}

isHandlingDescription := false
for _, comment := range commentGroup.List {
commentText := strings.TrimSpace(strings.TrimLeft(comment.Text, "/"))
attribute := strings.Split(commentText, " ")[0]
if strings.ToLower(attribute) != descriptionAttr {
if !isHandlingDescription {
continue
}
break
}

KuboOrk marked this conversation as resolved.
Show resolved Hide resolved
isHandlingDescription = true
description += " " + strings.TrimSpace(commentText[len(attribute):])
}
}

return strings.TrimLeft(description, " ")
}

// parseTypeExpr parses given type expression that corresponds to the type under
// given name and package, and returns swagger schema for it.
func (parser *Parser) parseTypeExpr(file *ast.File, typeExpr ast.Expr, ref bool) (*spec.Schema, error) {
Expand Down
1 change: 1 addition & 0 deletions parser_test.go
Expand Up @@ -1800,6 +1800,7 @@ func TestParseStructComment(t *testing.T) {
},
"definitions": {
"web.APIError": {
"description": "API error with information about it",
"type": "object",
"properties": {
"createdAt": {
Expand Down
4 changes: 4 additions & 0 deletions testdata/struct_comment/web/handler.go
Expand Up @@ -15,6 +15,10 @@ type Post struct {
} `json:"data"`
}

// APIError
// @Description API error
// @Description with information about it
// Other some summary
type APIError struct {
// Error an Api error
Error string // Error this is Line comment
Expand Down