增加解析openapi文档的方法, 待完善
This commit is contained in:
37
parser.go
37
parser.go
@ -8,34 +8,35 @@
|
||||
package api_doc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Parse 解析swagger文档
|
||||
// ParseOpenapi3 解析openapi文档
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:54 2024/12/23
|
||||
func Parse(docUrl string) (*define.DocParseResult, error) {
|
||||
// Date : 21:02 2025/2/25
|
||||
func ParseOpenapi3(docUrl string, docContent string) (*define.OpenapiDoc, error) {
|
||||
var (
|
||||
err error
|
||||
docContent []byte
|
||||
docRes define.OpenapiDoc
|
||||
err error
|
||||
docByte []byte
|
||||
)
|
||||
|
||||
if docContent, err = serialize.File.ReadFromRemote(docUrl); nil != err {
|
||||
if len(docContent) < 2 {
|
||||
if len(docUrl) == 0 {
|
||||
return nil, errors.New("doc url and doc content all empty")
|
||||
}
|
||||
if docByte, err = serialize.File.ReadFromRemote(docUrl); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
} else {
|
||||
docByte = []byte(docContent)
|
||||
}
|
||||
if err = serialize.JSON.UnmarshalWithNumber(docByte, &docRes); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
swaggerVersion := gjson.GetBytes(docContent, "swagger").String()
|
||||
if "" == swaggerVersion || !strings.HasPrefix(swaggerVersion, "2.") {
|
||||
// 未指定swagger版本或swagger版本3.x
|
||||
return ParseOpenapi3(docContent)
|
||||
}
|
||||
return ParseSwagger2(docContent)
|
||||
}
|
||||
|
||||
func ParseOpenapi3(docContent []byte) (*define.DocParseResult, error) {
|
||||
return nil, nil
|
||||
return &docRes, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user