修复文档不规范格式
This commit is contained in:
37
generate.go
37
generate.go
@ -192,8 +192,8 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
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)
|
||||
g.AddComponentsSchema("", paramPkgPath, paramType)
|
||||
g.AddComponentsSchema("", resultPkgPath, resultType)
|
||||
if _, exist := g.docData.Paths[baseCfg.Uri]; !exist {
|
||||
g.docData.Paths[baseCfg.Uri] = &define.PathConfig{}
|
||||
}
|
||||
@ -268,7 +268,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:25 2025/2/8
|
||||
func (g *Generate) AddComponentsSchema(schemaName string, inputType reflect.Type) string {
|
||||
func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType reflect.Type) string {
|
||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||
Nullable: false,
|
||||
@ -293,7 +293,8 @@ func (g *Generate) AddComponentsSchema(schemaName string, inputType reflect.Type
|
||||
}
|
||||
// 数组
|
||||
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
|
||||
sliceItemType := g.parseSliceItem(inputType)
|
||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
|
||||
sliceItemType := g.parseSliceItem(schemaName, inputType)
|
||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Name()] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Format: inputType.String(),
|
||||
@ -303,13 +304,19 @@ func (g *Generate) AddComponentsSchema(schemaName string, inputType reflect.Type
|
||||
}
|
||||
// 结构体
|
||||
if inputType.Kind() == reflect.Struct {
|
||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeObject
|
||||
for i := 0; i < inputType.NumField(); i++ {
|
||||
if inputType.Field(i).Type.Kind() == reflect.Ptr ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Map ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||
g.AddComponentsSchema(schemaName+inputType.Field(i).Name, inputType.Field(i).Type)
|
||||
g.AddComponentsSchema(schemaName, schemaName+inputType.Field(i).Name, inputType.Field(i).Type)
|
||||
g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeObject,
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Properties: map[string]*define.Property{},
|
||||
}
|
||||
} else {
|
||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
||||
Type: "string",
|
||||
@ -326,14 +333,30 @@ func (g *Generate) AddComponentsSchema(schemaName string, inputType reflect.Type
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:33 2025/2/8
|
||||
func (g *Generate) parseSliceItem(inputType reflect.Type) string {
|
||||
func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type) string {
|
||||
if inputType.Kind() != reflect.Slice && inputType.Kind() != reflect.Array {
|
||||
// 不是数组
|
||||
return ""
|
||||
}
|
||||
sliceValue := reflect.MakeSlice(inputType, 1, 1)
|
||||
sliceItemType := sliceValue.Index(0).Type()
|
||||
g.AddComponentsSchema(sliceItemType.PkgPath(), sliceItemType)
|
||||
g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType)
|
||||
if rootSchemaName != "" {
|
||||
g.docData.Components.Schemas[rootSchemaName].Properties[sliceItemType.PkgPath()] = &define.Property{
|
||||
Type: "",
|
||||
Format: inputType.String(),
|
||||
Enum: nil,
|
||||
Default: nil,
|
||||
Description: "",
|
||||
AllOf: nil,
|
||||
OneOf: nil,
|
||||
AnyOf: nil,
|
||||
Items: nil,
|
||||
AdditionalProperties: nil,
|
||||
Properties: nil,
|
||||
Ref: g.getSchemaRes(sliceItemType.PkgPath()),
|
||||
}
|
||||
}
|
||||
if len(sliceItemType.PkgPath()) == 0 {
|
||||
return sliceItemType.String()
|
||||
}
|
||||
|
Reference in New Issue
Block a user