This commit is contained in:
白茶清欢 2025-02-12 22:15:14 +08:00
parent aff4c86cb0
commit 8c84285347
2 changed files with 25 additions and 17 deletions

View File

@ -131,6 +131,7 @@ type Schema struct {
Required []string `json:"required,omitempty"` // 必传属性列表 Required []string `json:"required,omitempty"` // 必传属性列表
Enum []any `json:"enum,omitempty"` // 枚举值列表 Enum []any `json:"enum,omitempty"` // 枚举值列表
Type string `json:"type,omitempty"` // 类型 Type string `json:"type,omitempty"` // 类型
Items *PropertyXOf `json:"items,omitempty"` // items 必须存在如果 type 的值是 array。
Ref string `json:"$ref,omitempty"` // 类型引用 Ref string `json:"$ref,omitempty"` // 类型引用
} }

View File

@ -195,8 +195,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
if resultType.Kind() == reflect.Ptr { if resultType.Kind() == reflect.Ptr {
resultType = resultType.Elem() resultType = resultType.Elem()
} }
g.AddComponentsSchema("", paramType.PkgPath(), paramType) paramSchemaName := g.AddComponentsSchema("", strings.ReplaceAll(paramType.PkgPath(), "/", "-"), paramType)
g.AddComponentsSchema("", resultType.PkgPath(), resultType) resultSchemaName := g.AddComponentsSchema("", strings.ReplaceAll(resultType.PkgPath(), "/", "-"), resultType)
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist { if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
g.docData.Paths[baseCfg.Uri] = &define.PathConfig{} g.docData.Paths[baseCfg.Uri] = &define.PathConfig{}
} }
@ -227,7 +227,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
for _, itemType := range baseCfg.ContentType { for _, itemType := range baseCfg.ContentType {
cfg.RequestBody.Content[itemType] = &define.Media{ cfg.RequestBody.Content[itemType] = &define.Media{
Schema: &define.Schema{ Schema: &define.Schema{
Ref: g.getSchemaRes(paramType.Name()), Ref: g.getSchemaRef(paramSchemaName),
}, },
Example: "", Example: "",
Examples: nil, Examples: nil,
@ -237,7 +237,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
for _, itemOutputType := range baseCfg.OutputContentType { for _, itemOutputType := range baseCfg.OutputContentType {
cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{ cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{
Schema: &define.Schema{ Schema: &define.Schema{
Ref: g.getSchemaRes(resultType.Name()), Ref: g.getSchemaRef(resultSchemaName),
}, },
Example: "", Example: "",
Examples: nil, Examples: nil,
@ -272,7 +272,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:25 2025/2/8 // Date : 15:25 2025/2/8
func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType reflect.Type) string { func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, inputType reflect.Type) string {
schemaName := pkgPath + "." + inputType.Name()
if _, exist := g.docData.Components.Schemas[schemaName]; !exist { if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
g.docData.Components.Schemas[schemaName] = &define.Schema{ g.docData.Components.Schemas[schemaName] = &define.Schema{
Nullable: false, Nullable: false,
@ -287,7 +288,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
Required: make([]string, 0), Required: make([]string, 0),
Enum: make([]any, 0), Enum: make([]any, 0),
Type: "", Type: "",
Ref: g.getSchemaRes(schemaName), Ref: g.getSchemaRef(schemaName),
} }
} }
if inputType.Kind() == reflect.Map { if inputType.Kind() == reflect.Map {
@ -297,13 +298,19 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
} }
// 数组 // 数组
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array { if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray if len(rootSchemaName) == 0 {
sliceItemType := g.parseSliceItem(schemaName, inputType) g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{ sliceItemType := g.parseSliceItem(schemaName, inputType)
Type: consts.SwaggerDataTypeArray, g.docData.Components.Schemas[schemaName].Items = &define.PropertyXOf{Ref: g.getSchemaRef(g.getSchemaRef(sliceItemType))}
Format: inputType.String(), } else {
Items: &define.PropertyXOf{Ref: g.getSchemaRes(sliceItemType)}, sliceItemType := g.parseSliceItem(schemaName, inputType)
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
Type: consts.SwaggerDataTypeArray,
Format: inputType.String(),
Items: &define.PropertyXOf{Ref: g.getSchemaRef(sliceItemType)},
}
} }
return schemaName return schemaName
} }
// 结构体 // 结构体
@ -391,7 +398,7 @@ func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type)
sliceValue := reflect.MakeSlice(inputType, 1, 1) sliceValue := reflect.MakeSlice(inputType, 1, 1)
sliceItemType := sliceValue.Index(0).Type() sliceItemType := sliceValue.Index(0).Type()
g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType) g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType)
if rootSchemaName != "" { /* if rootSchemaName != "" {
g.docData.Components.Schemas[rootSchemaName].Properties[sliceItemType.PkgPath()] = &define.Property{ g.docData.Components.Schemas[rootSchemaName].Properties[sliceItemType.PkgPath()] = &define.Property{
Type: "", Type: "",
Format: inputType.String(), Format: inputType.String(),
@ -404,21 +411,21 @@ func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type)
Items: nil, Items: nil,
AdditionalProperties: nil, AdditionalProperties: nil,
Properties: nil, Properties: nil,
Ref: g.getSchemaRes(sliceItemType.PkgPath()), Ref: g.getSchemaRef(sliceItemType.PkgPath()),
} }
} }*/
if len(sliceItemType.PkgPath()) == 0 { if len(sliceItemType.PkgPath()) == 0 {
return sliceItemType.String() return sliceItemType.String()
} }
return sliceItemType.PkgPath() + "." + sliceItemType.String() return sliceItemType.PkgPath() + "." + sliceItemType.String()
} }
// getSchemaRes 获取引用的类型 // getSchemaRef 获取引用的类型
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:25 2025/2/9 // Date : 14:25 2025/2/9
func (g *Generate) getSchemaRes(schemaName string) string { func (g *Generate) getSchemaRef(schemaName string) string {
if "" == schemaName { if "" == schemaName {
return "" return ""
} }