openapi格式的文档基础生成 #3
@ -279,9 +279,9 @@ type Info struct {
|
|||||||
//
|
//
|
||||||
// Date : 16:08 2024/4/19
|
// Date : 16:08 2024/4/19
|
||||||
type Contact struct {
|
type Contact struct {
|
||||||
Name string `json:"name"` // 人或组织的名称。
|
Name string `json:"name,omitempty"` // 人或组织的名称。
|
||||||
Url string `json:"url"` // 指向联系人信息的 URL 地址,必须是 URL 地址格式。
|
Url string `json:"url,omitempty"` // 指向联系人信息的 URL 地址,必须是 URL 地址格式。
|
||||||
Email string `json:"email"` // 人或组织的 email 地址,必须是 email 地址格式
|
Email string `json:"email,omitempty"` // 人或组织的 email 地址,必须是 email 地址格式
|
||||||
}
|
}
|
||||||
|
|
||||||
// License 开源协议
|
// License 开源协议
|
||||||
|
27
generate.go
27
generate.go
@ -9,6 +9,7 @@ package api_doc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||||
@ -186,8 +187,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
}
|
}
|
||||||
// post类解析
|
// post类解析
|
||||||
defaultPkgPath := wrapper.String(strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "_")).SnakeCaseToCamel()
|
defaultPkgPath := wrapper.String(strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "_")).SnakeCaseToCamel()
|
||||||
paramPkgPath := defaultPkgPath + "Param"
|
paramPkgPath := defaultPkgPath + baseCfg.Method + "Param"
|
||||||
resultPkgPath := defaultPkgPath + "Result"
|
resultPkgPath := defaultPkgPath + baseCfg.Method + "Result"
|
||||||
g.AddComponentsSchema(paramPkgPath, paramType)
|
g.AddComponentsSchema(paramPkgPath, paramType)
|
||||||
g.AddComponentsSchema(resultPkgPath, resultType)
|
g.AddComponentsSchema(resultPkgPath, resultType)
|
||||||
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
|
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
|
||||||
@ -206,16 +207,30 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
Content: map[string]*define.Media{},
|
Content: map[string]*define.Media{},
|
||||||
Ref: "",
|
Ref: "",
|
||||||
},
|
},
|
||||||
Responses: nil,
|
Responses: map[string]*define.Response{
|
||||||
|
fmt.Sprintf("%v", http.StatusOK): &define.Response{
|
||||||
|
Content: map[string]*define.Media{},
|
||||||
|
},
|
||||||
|
},
|
||||||
Callbacks: nil,
|
Callbacks: nil,
|
||||||
Deprecated: false,
|
Deprecated: false,
|
||||||
Security: nil,
|
Security: nil,
|
||||||
Servers: nil,
|
Servers: nil,
|
||||||
}
|
}
|
||||||
for _, itemOutputType := range baseCfg.OutputContentType {
|
for _, itemType := range baseCfg.ContentType {
|
||||||
cfg.RequestBody.Content[itemOutputType] = &define.Media{
|
cfg.RequestBody.Content[itemType] = &define.Media{
|
||||||
Schema: &define.Schema{
|
Schema: &define.Schema{
|
||||||
Ref: g.getSchemaRes(paramPkgPath + "." + paramType.Name()),
|
Ref: g.getSchemaRes(paramPkgPath),
|
||||||
|
},
|
||||||
|
Example: "",
|
||||||
|
Examples: nil,
|
||||||
|
Encoding: nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, itemOutputType := range baseCfg.OutputContentType {
|
||||||
|
cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{
|
||||||
|
Schema: &define.Schema{
|
||||||
|
Ref: g.getSchemaRes(resultPkgPath),
|
||||||
},
|
},
|
||||||
Example: "",
|
Example: "",
|
||||||
Examples: nil,
|
Examples: nil,
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
package api_doc
|
package api_doc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.zhangdeman.cn/gateway/api-doc/define"
|
"git.zhangdeman.cn/gateway/api-doc/define"
|
||||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||||
@ -28,7 +29,7 @@ func Test_parser_Openapi3(t *testing.T) {
|
|||||||
type B struct {
|
type B struct {
|
||||||
List []A `json:"list"`
|
List []A `json:"list"`
|
||||||
}
|
}
|
||||||
var bArr []*B
|
var bArr *B
|
||||||
g := NewOpenapiDoc(nil, nil)
|
g := NewOpenapiDoc(nil, nil)
|
||||||
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
||||||
Uri: "/a/b/c/d",
|
Uri: "/a/b/c/d",
|
||||||
@ -41,6 +42,8 @@ func Test_parser_Openapi3(t *testing.T) {
|
|||||||
ParamList: nil,
|
ParamList: nil,
|
||||||
ResultList: nil,
|
ResultList: nil,
|
||||||
}, reflect.TypeOf(bArr), reflect.TypeOf(bArr))
|
}, reflect.TypeOf(bArr), reflect.TypeOf(bArr))
|
||||||
|
byteData, _ := json.Marshal(g.docData)
|
||||||
|
fmt.Println(string(byteData))
|
||||||
fmt.Println(g.parseSliceItem(reflect.TypeOf(bArr)))
|
fmt.Println(g.parseSliceItem(reflect.TypeOf(bArr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user