fix
This commit is contained in:
parent
aff4c86cb0
commit
8c84285347
@ -131,6 +131,7 @@ type Schema struct {
|
||||
Required []string `json:"required,omitempty"` // 必传属性列表
|
||||
Enum []any `json:"enum,omitempty"` // 枚举值列表
|
||||
Type string `json:"type,omitempty"` // 类型
|
||||
Items *PropertyXOf `json:"items,omitempty"` // items 必须存在如果 type 的值是 array。
|
||||
Ref string `json:"$ref,omitempty"` // 类型引用
|
||||
}
|
||||
|
||||
|
41
generate.go
41
generate.go
@ -195,8 +195,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
if resultType.Kind() == reflect.Ptr {
|
||||
resultType = resultType.Elem()
|
||||
}
|
||||
g.AddComponentsSchema("", paramType.PkgPath(), paramType)
|
||||
g.AddComponentsSchema("", resultType.PkgPath(), resultType)
|
||||
paramSchemaName := g.AddComponentsSchema("", strings.ReplaceAll(paramType.PkgPath(), "/", "-"), paramType)
|
||||
resultSchemaName := g.AddComponentsSchema("", strings.ReplaceAll(resultType.PkgPath(), "/", "-"), resultType)
|
||||
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
|
||||
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 {
|
||||
cfg.RequestBody.Content[itemType] = &define.Media{
|
||||
Schema: &define.Schema{
|
||||
Ref: g.getSchemaRes(paramType.Name()),
|
||||
Ref: g.getSchemaRef(paramSchemaName),
|
||||
},
|
||||
Example: "",
|
||||
Examples: nil,
|
||||
@ -237,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(resultType.Name()),
|
||||
Ref: g.getSchemaRef(resultSchemaName),
|
||||
},
|
||||
Example: "",
|
||||
Examples: nil,
|
||||
@ -272,7 +272,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// 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 {
|
||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||
Nullable: false,
|
||||
@ -287,7 +288,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
||||
Required: make([]string, 0),
|
||||
Enum: make([]any, 0),
|
||||
Type: "",
|
||||
Ref: g.getSchemaRes(schemaName),
|
||||
Ref: g.getSchemaRef(schemaName),
|
||||
}
|
||||
}
|
||||
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 {
|
||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
|
||||
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.getSchemaRes(sliceItemType)},
|
||||
if len(rootSchemaName) == 0 {
|
||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
|
||||
sliceItemType := g.parseSliceItem(schemaName, inputType)
|
||||
g.docData.Components.Schemas[schemaName].Items = &define.PropertyXOf{Ref: g.getSchemaRef(g.getSchemaRef(sliceItemType))}
|
||||
} else {
|
||||
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
|
||||
}
|
||||
// 结构体
|
||||
@ -391,7 +398,7 @@ func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type)
|
||||
sliceValue := reflect.MakeSlice(inputType, 1, 1)
|
||||
sliceItemType := sliceValue.Index(0).Type()
|
||||
g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType)
|
||||
if rootSchemaName != "" {
|
||||
/* if rootSchemaName != "" {
|
||||
g.docData.Components.Schemas[rootSchemaName].Properties[sliceItemType.PkgPath()] = &define.Property{
|
||||
Type: "",
|
||||
Format: inputType.String(),
|
||||
@ -404,21 +411,21 @@ func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type)
|
||||
Items: nil,
|
||||
AdditionalProperties: nil,
|
||||
Properties: nil,
|
||||
Ref: g.getSchemaRes(sliceItemType.PkgPath()),
|
||||
Ref: g.getSchemaRef(sliceItemType.PkgPath()),
|
||||
}
|
||||
}
|
||||
}*/
|
||||
if len(sliceItemType.PkgPath()) == 0 {
|
||||
return sliceItemType.String()
|
||||
}
|
||||
return sliceItemType.PkgPath() + "." + sliceItemType.String()
|
||||
}
|
||||
|
||||
// getSchemaRes 获取引用的类型
|
||||
// getSchemaRef 获取引用的类型
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:25 2025/2/9
|
||||
func (g *Generate) getSchemaRes(schemaName string) string {
|
||||
func (g *Generate) getSchemaRef(schemaName string) string {
|
||||
if "" == schemaName {
|
||||
return ""
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user