Skip to content

Commit a990865

Browse files
committed
fix: template request body empty, support add group attr at file doc comment
1 parent 7fee8d4 commit a990865

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

gen/template/apidocs/group_apis.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ parameter|parameterType|dataType|required|validate|example|description
5151
{{- range $p:= $v.Requests.Parameters}}
5252
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
5353
{{- end}}
54+
{{- end}}
5455
{{- if $v.Requests.Body}}
5556

5657
_body_:
@@ -59,7 +60,6 @@ _body_:
5960
{{$v.Requests.Body}}
6061
```
6162
{{- end}}
62-
{{- end}}
6363
{{- if $v.Responses}}
6464

6565
__Response__:

gen/template/apidocs/single_index.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ parameter|parameterType|dataType|required|validate|example|description
5454
{{- range $p:= $v.Requests.Parameters}}
5555
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
5656
{{- end}}
57+
{{- end}}
5758
{{- if $v.Requests.Body}}
5859

5960
_body_:
@@ -62,7 +63,6 @@ _body_:
6263
{{$v.Requests.Body}}
6364
```
6465
{{- end}}
65-
{{- end}}
6666
{{- if $v.Responses}}
6767

6868
__Response__:

gen/template/markdown/group_apis.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ parameter|parameterType|dataType|required|validate|example|description
4444
{{- range $p:= $v.Requests.Parameters}}
4545
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
4646
{{- end}}
47+
{{- end}}
4748
{{- if $v.Requests.Body}}
4849

4950
_body_:
@@ -52,7 +53,7 @@ _body_:
5253
{{$v.Requests.Body}}
5354
```
5455
{{- end}}
55-
{{- end}}
56+
5657
{{- if $v.Responses}}
5758

5859
__Response__:

gen/template/markdown/single_index.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ parameter|parameterType|dataType|required|validate|example|description
5454
{{- range $p:= $v.Requests.Parameters}}
5555
__{{$p.Name}}__|_{{$p.ParameterTypes}}_|{{$p.DataType}}|{{$p.Required}}|{{$p.Validate}}|{{$p.Example}}|{{$p.Description}}
5656
{{- end}}
57+
{{- end}}
5758
{{- if $v.Requests.Body}}
5859

5960
_body_:
@@ -62,7 +63,6 @@ _body_:
6263
{{$v.Requests.Body}}
6364
```
6465
{{- end}}
65-
{{- end}}
6666
{{- if $v.Responses}}
6767

6868
__Response__:

parser/parser.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ func (p *Parser) GetApiDoc() *ApiDocSpec {
105105

106106
func (p *Parser) parseApiInfos(fileName string, astFile *ast.File) error {
107107
//parse group
108+
var fileGroup string
109+
comments := strings.Split(astFile.Doc.Text(), "\n")
110+
if len(comments) > 0 {
111+
for _, comment := range comments {
112+
commentLine := strings.TrimSpace(strings.TrimLeft(comment, "/"))
113+
if len(commentLine) == 0 {
114+
continue
115+
}
116+
attribute := strings.Fields(commentLine)[0]
117+
lineRemainder, lowerAttribute := strings.TrimSpace(commentLine[len(attribute):]), strings.ToLower(attribute)
118+
if lowerAttribute == groupAttr {
119+
fileGroup = lineRemainder
120+
break
121+
}
122+
}
123+
}
108124
for _, comment := range astFile.Comments {
109125
comments := strings.Split(comment.Text(), "\n")
110126
if isApiGroupComment(comments) {
@@ -141,7 +157,10 @@ func (p *Parser) parseApiInfos(fileName string, astFile *ast.File) error {
141157
return fmt.Errorf("ParseComment error in file %s :%+v", fileName, err)
142158
}
143159
}
144-
operation.ApiSpec.doc = p.doc //ptr, for build full url
160+
operation.ApiSpec.doc = p.doc //ptr, for build full url
161+
if operation.ApiSpec.Group == "" && fileGroup != "" { //use file group
162+
operation.ApiSpec.Group = fileGroup
163+
}
145164
if operation.ApiSpec.Group == "" {
146165
p.doc.UngroupedApis = append(p.doc.UngroupedApis, &operation.ApiSpec)
147166
} else {

parser/spec.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,15 @@ func (v *TypeSchema) buildComment() string {
325325
} else {
326326
s += v.Type
327327
}
328-
if v.IsRequired() {
329-
s += ", required"
328+
329+
validate := v.ValidateTag()
330+
if validate != "" && !strings.Contains(validate, "required") {
331+
if v.IsRequired() {
332+
s += ", required"
333+
}
334+
}
335+
if validate != "" {
336+
s += fmt.Sprintf(", validate:\"%s\"", validate)
330337
}
331338
if len(v.Comment) > 0 {
332339
s += ", " + v.Comment
@@ -553,7 +560,7 @@ func getTypeExample(typeName, example string) string {
553560

554561
func exampleInt(example string) int {
555562
if example == "" {
556-
return 123
563+
return 0
557564
}
558565
v, _ := strconv.Atoi(example)
559566
return v
@@ -592,7 +599,7 @@ func exampleBool(example string) bool {
592599

593600
func exampleString(example string) string {
594601
if example == "" {
595-
return "abc"
602+
return "string"
596603
}
597604
return example
598605
}

0 commit comments

Comments
 (0)