完成一般本接口文档生成
This commit is contained in:
parent
77c9e06b39
commit
8a7999a480
25
generate.go
25
generate.go
@ -195,12 +195,12 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
if resultType.Kind() == reflect.Ptr {
|
||||
resultType = resultType.Elem()
|
||||
}
|
||||
paramSchemaName := g.AddComponentsSchema("", strings.ReplaceAll(paramType.PkgPath(), "/", "-"), paramType)
|
||||
resultSchemaName := g.AddComponentsSchema("", strings.ReplaceAll(resultType.PkgPath(), "/", "-"), resultType)
|
||||
paramSchemaName := g.AddComponentsSchema("", paramType.PkgPath(), paramType)
|
||||
resultSchemaName := 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()
|
||||
defaultPkgPath := wrapper.String(strings.TrimLeft(baseCfg.Uri, "/")).SnakeCaseToCamel()
|
||||
cfg := &define.PathItemOperationConfig{
|
||||
Tags: baseCfg.TagList,
|
||||
Summary: baseCfg.Summary,
|
||||
@ -273,9 +273,9 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
//
|
||||
// Date : 15:25 2025/2/8
|
||||
func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, inputType reflect.Type) string {
|
||||
schemaName := pkgPath + "." + inputType.Name()
|
||||
schemaName := strings.ReplaceAll(pkgPath+"."+inputType.Name(), "/", "-")
|
||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||
s := &define.Schema{
|
||||
Nullable: false,
|
||||
Discriminator: nil,
|
||||
ReadOnly: false,
|
||||
@ -290,6 +290,10 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
Type: "",
|
||||
Ref: g.getSchemaRef(schemaName),
|
||||
}
|
||||
if len(rootSchemaName) == 0 || inputType.Kind() == reflect.Struct {
|
||||
s.Ref = ""
|
||||
}
|
||||
g.docData.Components.Schemas[schemaName] = s
|
||||
}
|
||||
if inputType.Kind() == reflect.Map {
|
||||
// map, 直接添加公共
|
||||
@ -315,7 +319,12 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
}
|
||||
// 结构体
|
||||
if inputType.Kind() == reflect.Struct {
|
||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeObject
|
||||
if len(rootSchemaName) == 0 {
|
||||
|
||||
} else {
|
||||
// g.docData.Components.Schemas[schemaName].Properties[""] = schemaName
|
||||
}
|
||||
// g.docData.Components.Schemas[schemaName].Ref = consts.SwaggerDataTypeObject
|
||||
for i := 0; i < inputType.NumField(); i++ {
|
||||
if inputType.Field(i).Type.Kind() == reflect.Ptr ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||
@ -429,7 +438,7 @@ func (g *Generate) getSchemaRef(schemaName string) string {
|
||||
if "" == schemaName {
|
||||
return ""
|
||||
}
|
||||
return "#/components/schemas/" + schemaName
|
||||
return "#/components/schemas/" + strings.ReplaceAll(schemaName, "/", "-")
|
||||
}
|
||||
|
||||
// realType2SwaggerType golang 真实数据类型转换为golang数据类型
|
||||
@ -448,7 +457,7 @@ func (g *Generate) realBaseType2SwaggerType(realType string) string {
|
||||
case "float32", "*float32", "float64", "*float64":
|
||||
return consts.SwaggerDataTypeNumber
|
||||
case "int", "*int", "uint", "*uint", "int64", "*int64", "uint64", "*uint64":
|
||||
return consts.SwaggerDataTypeLong
|
||||
return consts.SwaggerDataTypeNumber
|
||||
case "int8", "*int8", "uint8", "*uint8", "int16", "*int16", "uint16", "*uint16", "int32", "*int32", "uint32", "*uint32":
|
||||
return consts.SwaggerDataTypeInteger
|
||||
default:
|
||||
|
Loading…
x
Reference in New Issue
Block a user