优化文档解析
This commit is contained in:
parent
613e004bf2
commit
aff4c86cb0
47
generate.go
47
generate.go
@ -189,14 +189,18 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
return nil
|
||||
}
|
||||
// post类解析
|
||||
defaultPkgPath := wrapper.String(strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "_")).SnakeCaseToCamel()
|
||||
paramPkgPath := defaultPkgPath + baseCfg.Method + "Param"
|
||||
resultPkgPath := defaultPkgPath + baseCfg.Method + "Result"
|
||||
g.AddComponentsSchema("", paramPkgPath, paramType)
|
||||
g.AddComponentsSchema("", resultPkgPath, resultType)
|
||||
if paramType.Kind() == reflect.Ptr {
|
||||
paramType = paramType.Elem()
|
||||
}
|
||||
if resultType.Kind() == reflect.Ptr {
|
||||
resultType = resultType.Elem()
|
||||
}
|
||||
g.AddComponentsSchema("", paramType.PkgPath(), paramType)
|
||||
g.AddComponentsSchema("", resultType.PkgPath(), resultType)
|
||||
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
|
||||
g.docData.Paths[baseCfg.Uri] = &define.PathConfig{}
|
||||
}
|
||||
defaultPkgPath := wrapper.String(strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "_")).SnakeCaseToCamel()
|
||||
cfg := &define.PathItemOperationConfig{
|
||||
Tags: baseCfg.TagList,
|
||||
Summary: baseCfg.Summary,
|
||||
@ -223,7 +227,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
for _, itemType := range baseCfg.ContentType {
|
||||
cfg.RequestBody.Content[itemType] = &define.Media{
|
||||
Schema: &define.Schema{
|
||||
Ref: g.getSchemaRes(paramPkgPath),
|
||||
Ref: g.getSchemaRes(paramType.Name()),
|
||||
},
|
||||
Example: "",
|
||||
Examples: nil,
|
||||
@ -233,7 +237,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
for _, itemOutputType := range baseCfg.OutputContentType {
|
||||
cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{
|
||||
Schema: &define.Schema{
|
||||
Ref: g.getSchemaRes(resultPkgPath),
|
||||
Ref: g.getSchemaRes(resultType.Name()),
|
||||
},
|
||||
Example: "",
|
||||
Examples: nil,
|
||||
@ -269,22 +273,6 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
//
|
||||
// Date : 15:25 2025/2/8
|
||||
func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType reflect.Type) string {
|
||||
if rootSchemaName != "" {
|
||||
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
|
||||
Type: g.realType2SwaggerType(inputType.String()),
|
||||
Format: inputType.String(),
|
||||
Enum: nil,
|
||||
Default: "",
|
||||
Description: "",
|
||||
AllOf: nil,
|
||||
OneOf: nil,
|
||||
AnyOf: nil,
|
||||
Items: nil,
|
||||
AdditionalProperties: nil,
|
||||
Properties: nil,
|
||||
Ref: g.getSchemaRes(schemaName),
|
||||
}
|
||||
}
|
||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||
Nullable: false,
|
||||
@ -299,7 +287,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
Required: make([]string, 0),
|
||||
Enum: make([]any, 0),
|
||||
Type: "",
|
||||
Ref: "",
|
||||
Ref: g.getSchemaRes(schemaName),
|
||||
}
|
||||
}
|
||||
if inputType.Kind() == reflect.Map {
|
||||
@ -311,7 +299,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
|
||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
|
||||
sliceItemType := g.parseSliceItem(schemaName, inputType)
|
||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Name()] = &define.Property{
|
||||
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Format: inputType.String(),
|
||||
Items: &define.PropertyXOf{Ref: g.getSchemaRes(sliceItemType)},
|
||||
@ -337,10 +325,9 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
}
|
||||
continue
|
||||
}
|
||||
g.AddComponentsSchema(schemaName, schemaName+g.getParamName(inputType.Field(i)), inputType.Field(i).Type)
|
||||
if inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Map {
|
||||
g.docData.Components.Schemas[schemaName].Properties[schemaName+g.getParamName(inputType.Field(i))] = &define.Property{
|
||||
g.docData.Components.Schemas[schemaName].Properties[g.getParamName(inputType.Field(i))] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeObject,
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Description: g.getParamDesc(inputType.Field(i)),
|
||||
@ -348,7 +335,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
}
|
||||
} else if inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||
g.docData.Components.Schemas[schemaName].Properties[schemaName+g.getParamName(inputType.Field(i))] = &define.Property{
|
||||
g.docData.Components.Schemas[schemaName].Properties[g.getParamName(inputType.Field(i))] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Description: g.getParamDesc(inputType.Field(i)),
|
||||
@ -359,6 +346,8 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
}
|
||||
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
||||
|
||||
} else {
|
||||
g.AddComponentsSchema(schemaName, g.getParamName(inputType.Field(i)), inputType.Field(i).Type)
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -377,7 +366,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
convertType := g.realBaseType2SwaggerType(inputType.String())
|
||||
if convertType == inputType.String() {
|
||||
// 非基础数据类型
|
||||
return g.AddComponentsSchema(schemaName, schemaName+inputType.Elem().String(), inputType.Elem())
|
||||
return g.AddComponentsSchema(schemaName, inputType.Elem().String(), inputType.Elem())
|
||||
} else {
|
||||
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
|
||||
Type: convertType,
|
||||
|
@ -23,13 +23,15 @@ import (
|
||||
//
|
||||
// Date : 17:55 2024/7/19
|
||||
func Test_parser_Openapi3(t *testing.T) {
|
||||
type A struct {
|
||||
type User struct {
|
||||
Name string `json:"name" d:"zhang" desc:"用户姓名"`
|
||||
Age int `json:"age" d:"18" desc:"年龄"`
|
||||
}
|
||||
type B struct {
|
||||
List []A `json:"list"`
|
||||
type List struct {
|
||||
Total int64 `json:"total"`
|
||||
UserList []User `json:"user_list"`
|
||||
}
|
||||
var bArr *B
|
||||
var l List
|
||||
g := NewOpenapiDoc(nil, nil)
|
||||
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
||||
Uri: "/ent/user/detail",
|
||||
@ -41,10 +43,10 @@ func Test_parser_Openapi3(t *testing.T) {
|
||||
Description: "",
|
||||
ParamList: nil,
|
||||
ResultList: nil,
|
||||
}, reflect.TypeOf(bArr), reflect.TypeOf(bArr))
|
||||
}, reflect.TypeOf(l), reflect.TypeOf(l))
|
||||
byteData, _ := json.Marshal(g.docData)
|
||||
fmt.Println(string(byteData))
|
||||
fmt.Println(g.parseSliceItem("", reflect.TypeOf(bArr)))
|
||||
fmt.Println(g.parseSliceItem("", reflect.TypeOf(l)))
|
||||
}
|
||||
|
||||
func TestParseForSwagger(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user