support for multiple routing response docs

This commit is contained in:
cuisaihang
2022-10-11 18:18:20 +08:00
parent 3ff75d22a5
commit dc7397c7c2
6 changed files with 98 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package generate
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
@ -26,6 +27,7 @@ const (
exampleOption = "example"
optionSeparator = "|"
equalToken = "="
atRespDoc = "@respdoc-"
)
func parseRangeOption(option string) (float64, float64, bool) {
@ -285,6 +287,52 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
},
}
for _, v := range route.Doc {
markerIndex := strings.Index(v, atRespDoc)
if markerIndex >= 0 {
l := strings.Index(v, "(")
r := strings.Index(v, ")")
code := strings.TrimSpace(v[markerIndex+len(atRespDoc) : l])
var comment string
commentIndex := strings.Index(v, "//")
if commentIndex > 0 {
comment = strings.TrimSpace(strings.Trim(v[commentIndex+2:], "*/"))
}
content := strings.TrimSpace(v[l+1 : r])
if strings.Index(v, ":") > 0 {
lines := strings.Split(content, "\n")
kv := make(map[string]string, len(lines))
for _, line := range lines {
sep := strings.Index(line, ":")
key := strings.TrimSpace(line[:sep])
value := strings.TrimSpace(line[sep+1:])
kv[key] = value
}
kvByte, err := json.Marshal(kv)
if err != nil {
continue
}
operationObject.Responses[code] = swaggerResponseObject{
Description: comment,
Schema: swaggerSchemaObject{
schemaCore: schemaCore{
Example: string(kvByte),
},
},
}
} else if len(content) > 0 {
operationObject.Responses[code] = swaggerResponseObject{
Description: comment,
Schema: swaggerSchemaObject{
schemaCore: schemaCore{
Ref: fmt.Sprintf("#/definitions/%s", content),
},
},
}
}
}
}
// set OperationID
operationObject.OperationID = route.Handler