增加解析openapi文档的方法, 待完善
This commit is contained in:
parent
71d6db967d
commit
22550f9ac0
@ -227,7 +227,7 @@ type RequestBody struct {
|
|||||||
// Date : 17:21 2024/7/19
|
// Date : 17:21 2024/7/19
|
||||||
type Media struct {
|
type Media struct {
|
||||||
Schema *Schema `json:"schema,omitempty"` // 定义此媒体类型的结构。
|
Schema *Schema `json:"schema,omitempty"` // 定义此媒体类型的结构。
|
||||||
Example string `json:"example,omitempty"` // 媒体类型的示例。示例对象应该符合此媒体类型的格式, 这里指定的example对象 object is mutually exclusive of the examples object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。
|
Example map[string]any `json:"example,omitempty"` // 媒体类型的示例。示例对象应该符合此媒体类型的格式, 这里指定的example对象 object is mutually exclusive of the examples object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。
|
||||||
Examples map[string]*Example `json:"examples,omitempty"` // 媒体类型的示例,每个媒体对象的值都应该匹配它对应的媒体类型的格式。 The examples object is mutually exclusive of the example object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。
|
Examples map[string]*Example `json:"examples,omitempty"` // 媒体类型的示例,每个媒体对象的值都应该匹配它对应的媒体类型的格式。 The examples object is mutually exclusive of the example object. 而且如果引用的schema也包含示例,在这里指定的example值将会覆盖schema提供的示例。
|
||||||
Encoding map[string]*Encoding `json:"encoding,omitempty"` // 属性名与编码信息的映射。每个属性名必须存在于schema属性的key中,当媒体类型等于multipart或application/x-www-form-urlencoded时,编码对象信息仅适用于requestBody。
|
Encoding map[string]*Encoding `json:"encoding,omitempty"` // 属性名与编码信息的映射。每个属性名必须存在于schema属性的key中,当媒体类型等于multipart或application/x-www-form-urlencoded时,编码对象信息仅适用于requestBody。
|
||||||
}
|
}
|
||||||
|
@ -207,7 +207,7 @@ func (g *Generate) AddApiFromInAndOut(uriPrefix string, paramType reflect.Type,
|
|||||||
Schema: &define.Schema{
|
Schema: &define.Schema{
|
||||||
Ref: g.getSchemaRef(paramSchemaName),
|
Ref: g.getSchemaRef(paramSchemaName),
|
||||||
},
|
},
|
||||||
Example: "",
|
Example: nil,
|
||||||
Examples: nil,
|
Examples: nil,
|
||||||
Encoding: nil,
|
Encoding: nil,
|
||||||
}
|
}
|
||||||
@ -220,7 +220,7 @@ func (g *Generate) AddApiFromInAndOut(uriPrefix string, paramType reflect.Type,
|
|||||||
Schema: &define.Schema{
|
Schema: &define.Schema{
|
||||||
Ref: g.getSchemaRef(resultSchemaName),
|
Ref: g.getSchemaRef(resultSchemaName),
|
||||||
},
|
},
|
||||||
Example: "",
|
Example: nil,
|
||||||
Examples: nil,
|
Examples: nil,
|
||||||
Encoding: nil,
|
Encoding: nil,
|
||||||
}
|
}
|
||||||
|
33
parser.go
33
parser.go
@ -8,34 +8,35 @@
|
|||||||
package api_doc
|
package api_doc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parse 解析swagger文档
|
// ParseOpenapi3 解析openapi文档
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 16:54 2024/12/23
|
// Date : 21:02 2025/2/25
|
||||||
func Parse(docUrl string) (*define.DocParseResult, error) {
|
func ParseOpenapi3(docUrl string, docContent string) (*define.OpenapiDoc, error) {
|
||||||
var (
|
var (
|
||||||
|
docRes define.OpenapiDoc
|
||||||
err error
|
err error
|
||||||
docContent []byte
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
swaggerVersion := gjson.GetBytes(docContent, "swagger").String()
|
} else {
|
||||||
if "" == swaggerVersion || !strings.HasPrefix(swaggerVersion, "2.") {
|
docByte = []byte(docContent)
|
||||||
// 未指定swagger版本或swagger版本3.x
|
|
||||||
return ParseOpenapi3(docContent)
|
|
||||||
}
|
}
|
||||||
return ParseSwagger2(docContent)
|
if err = serialize.JSON.UnmarshalWithNumber(docByte, &docRes); nil != err {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
func ParseOpenapi3(docContent []byte) (*define.DocParseResult, error) {
|
return &docRes, nil
|
||||||
return nil, nil
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -95,9 +94,3 @@ func Test_parser_Openapi3(t *testing.T) {
|
|||||||
byteData, _ := json.Marshal(g.docData)
|
byteData, _ := json.Marshal(g.docData)
|
||||||
fmt.Println(string(byteData))
|
fmt.Println(string(byteData))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseForSwagger(t *testing.T) {
|
|
||||||
docUrl := "https://git.zhangdeman.cn/swagger.v1.json"
|
|
||||||
res, _ := Parse(docUrl)
|
|
||||||
serialize.JSON.ConsoleOutput(res)
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user