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