openapi格式的文档基础生成 #3
33
generate.go
33
generate.go
@ -269,10 +269,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, schemaName string, inputType reflect.Type) string {
|
func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType reflect.Type) string {
|
||||||
fmt.Println(inputType.String())
|
|
||||||
if rootSchemaName != "" {
|
if rootSchemaName != "" {
|
||||||
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
|
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
|
||||||
Type: g.getSchemaRes(schemaName),
|
Type: g.realType2SwaggerType(inputType.String()),
|
||||||
Format: inputType.String(),
|
Format: inputType.String(),
|
||||||
Enum: nil,
|
Enum: nil,
|
||||||
Default: nil,
|
Default: nil,
|
||||||
@ -328,7 +327,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
inputType.Field(i).Type.Kind() == reflect.Map ||
|
inputType.Field(i).Type.Kind() == reflect.Map ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Array ||
|
inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||||
if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); convertType != inputType.Field(i).Type.Kind().String() {
|
if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() {
|
||||||
// 针对基础类型指针
|
// 针对基础类型指针
|
||||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
||||||
Type: g.realBaseType2SwaggerType(convertType),
|
Type: g.realBaseType2SwaggerType(convertType),
|
||||||
@ -337,11 +336,27 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
g.AddComponentsSchema(schemaName, schemaName+inputType.Field(i).Name, inputType.Field(i).Type)
|
g.AddComponentsSchema(schemaName, schemaName+inputType.Field(i).Name, inputType.Field(i).Type)
|
||||||
|
if inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||||
|
inputType.Field(i).Type.Kind() == reflect.Map {
|
||||||
g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{
|
||||||
Type: consts.SwaggerDataTypeObject,
|
Type: consts.SwaggerDataTypeObject,
|
||||||
Format: inputType.Field(i).Type.String(),
|
Format: inputType.Field(i).Type.String(),
|
||||||
Properties: map[string]*define.Property{},
|
Properties: map[string]*define.Property{},
|
||||||
}
|
}
|
||||||
|
} else if inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||||
|
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||||
|
g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{
|
||||||
|
Type: consts.SwaggerDataTypeArray,
|
||||||
|
Format: inputType.Field(i).Type.String(),
|
||||||
|
Items: &define.PropertyXOf{
|
||||||
|
Ref: g.parseSliceItem(schemaName, inputType.Field(i).Type),
|
||||||
|
},
|
||||||
|
Properties: map[string]*define.Property{},
|
||||||
|
}
|
||||||
|
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
||||||
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
|
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
|
||||||
@ -438,3 +453,15 @@ func (g *Generate) realBaseType2SwaggerType(realType string) string {
|
|||||||
return realType
|
return realType
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// realType2SwaggerType ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 21:20 2025/2/11
|
||||||
|
func (g *Generate) realType2SwaggerType(realType string) string {
|
||||||
|
if strings.HasPrefix(realType, "[]") {
|
||||||
|
return consts.SwaggerDataTypeArray
|
||||||
|
}
|
||||||
|
return g.realBaseType2SwaggerType(realType)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user