Skip to content

Commit

Permalink
fix: template request body empty, support add group attr at file doc …
Browse files Browse the repository at this point in the history
…comment
  • Loading branch information
alovn committed May 19, 2022
1 parent 7fee8d4 commit a990865
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gen/template/apidocs/group_apis.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ parameter|parameterType|dataType|required|validate|example|description
{{- range $p:= $v.Requests.Parameters}}
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
{{- end}}
{{- end}}
{{- if $v.Requests.Body}}

_body_:
Expand All @@ -59,7 +60,6 @@ _body_:
{{$v.Requests.Body}}
```
{{- end}}
{{- end}}
{{- if $v.Responses}}

__Response__:
Expand Down
2 changes: 1 addition & 1 deletion gen/template/apidocs/single_index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parameter|parameterType|dataType|required|validate|example|description
{{- range $p:= $v.Requests.Parameters}}
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
{{- end}}
{{- end}}
{{- if $v.Requests.Body}}

_body_:
Expand All @@ -62,7 +63,6 @@ _body_:
{{$v.Requests.Body}}
```
{{- end}}
{{- end}}
{{- if $v.Responses}}

__Response__:
Expand Down
3 changes: 2 additions & 1 deletion gen/template/markdown/group_apis.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ parameter|parameterType|dataType|required|validate|example|description
{{- range $p:= $v.Requests.Parameters}}
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
{{- end}}
{{- end}}
{{- if $v.Requests.Body}}

_body_:
Expand All @@ -52,7 +53,7 @@ _body_:
{{$v.Requests.Body}}
```
{{- end}}
{{- end}}

{{- if $v.Responses}}

__Response__:
Expand Down
2 changes: 1 addition & 1 deletion gen/template/markdown/single_index.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ parameter|parameterType|dataType|required|validate|example|description
{{- range $p:= $v.Requests.Parameters}}
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
{{- end}}
{{- end}}
{{- if $v.Requests.Body}}

_body_:
Expand All @@ -62,7 +63,6 @@ _body_:
{{$v.Requests.Body}}
```
{{- end}}
{{- end}}
{{- if $v.Responses}}

__Response__:
Expand Down
21 changes: 20 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ func (p *Parser) GetApiDoc() *ApiDocSpec {

func (p *Parser) parseApiInfos(fileName string, astFile *ast.File) error {
//parse group
var fileGroup string
comments := strings.Split(astFile.Doc.Text(), "\n")
if len(comments) > 0 {
for _, comment := range comments {
commentLine := strings.TrimSpace(strings.TrimLeft(comment, "/"))
if len(commentLine) == 0 {
continue
}
attribute := strings.Fields(commentLine)[0]
lineRemainder, lowerAttribute := strings.TrimSpace(commentLine[len(attribute):]), strings.ToLower(attribute)
if lowerAttribute == groupAttr {
fileGroup = lineRemainder
break
}
}
}
for _, comment := range astFile.Comments {
comments := strings.Split(comment.Text(), "\n")
if isApiGroupComment(comments) {
Expand Down Expand Up @@ -141,7 +157,10 @@ func (p *Parser) parseApiInfos(fileName string, astFile *ast.File) error {
return fmt.Errorf("ParseComment error in file %s :%+v", fileName, err)
}
}
operation.ApiSpec.doc = p.doc //ptr, for build full url
operation.ApiSpec.doc = p.doc //ptr, for build full url
if operation.ApiSpec.Group == "" && fileGroup != "" { //use file group
operation.ApiSpec.Group = fileGroup
}
if operation.ApiSpec.Group == "" {
p.doc.UngroupedApis = append(p.doc.UngroupedApis, &operation.ApiSpec)
} else {
Expand Down
15 changes: 11 additions & 4 deletions parser/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,15 @@ func (v *TypeSchema) buildComment() string {
} else {
s += v.Type
}
if v.IsRequired() {
s += ", required"

validate := v.ValidateTag()
if validate != "" && !strings.Contains(validate, "required") {
if v.IsRequired() {
s += ", required"
}
}
if validate != "" {
s += fmt.Sprintf(", validate:\"%s\"", validate)
}
if len(v.Comment) > 0 {
s += ", " + v.Comment
Expand Down Expand Up @@ -553,7 +560,7 @@ func getTypeExample(typeName, example string) string {

func exampleInt(example string) int {
if example == "" {
return 123
return 0
}
v, _ := strconv.Atoi(example)
return v
Expand Down Expand Up @@ -592,7 +599,7 @@ func exampleBool(example string) bool {

func exampleString(example string) string {
if example == "" {
return "abc"
return "string"
}
return example
}
Expand Down

0 comments on commit a990865

Please sign in to comment.