优化 summary/operationId处理

This commit is contained in:
白茶清欢 2025-02-18 10:51:24 +08:00
parent 1ff465b953
commit f0f15a4df3

View File

@ -270,7 +270,7 @@ func (g *Generate) getApiDocBaseCfg(baseCfg *define.UriBaseConfig, paramType ref
Summary: baseCfg.Summary,
Description: baseCfg.Description,
ExternalDocs: nil,
OperationID: baseCfg.Summary + "(" + strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "-") + ")",
OperationID: baseCfg.Summary + "(" + baseCfg.Method + "-" + strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "-") + ")",
Parameters: make([]*define.PathConfigParameter, 0),
RequestBody: &define.RequestBody{
Required: true,
@ -338,11 +338,31 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe
// 空Meta字段认为是用来描述元信息的, 忽略
continue
}
convertBaseType, isBaseType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String())
realInputTypeFormat := inputType.Field(i).Type.String()
fieldType := inputType.Field(i).Type
/*if inputType.Field(i).Type.Kind() == reflect.Ptr {
fieldType = inputType.Field(i).Type.Elem()
}*/
if isBaseType {
// 当做默认基础类型, 默认不会出现 *map *[]
baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{
Name: propertyName,
In: consts.SwaggerParameterInQuery,
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
Required: ValidateRule.IsRequired(inputType.Field(i)),
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
Schema: &define.Schema{
Type: convertBaseType,
Format: realInputTypeFormat,
},
AllowEmptyValue: false,
Style: "",
Explode: false,
AllowReserved: false,
})
continue
}
if inputType.Field(i).Type.Kind() == reflect.Ptr {
// 处理指针
if inputType.Field(i).Type.Elem().Kind() == reflect.Struct {
@ -364,22 +384,7 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe
AllowReserved: false,
})
} else {
// 当做默认基础类型, 默认不会出现 *map *[]
baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{
Name: propertyName,
In: consts.SwaggerParameterInQuery,
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
Required: ValidateRule.IsRequired(inputType.Field(i)),
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
Schema: &define.Schema{
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
Format: realInputTypeFormat,
},
AllowEmptyValue: false,
Style: "",
Explode: false,
AllowReserved: false,
})
}
continue
}
@ -387,29 +392,6 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe
fieldType.Kind() == reflect.Map ||
fieldType.Kind() == reflect.Array ||
fieldType.Kind() == reflect.Slice {
if convertType := g.realBaseType2SwaggerType(fieldType.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() {
// 针对基础类型指针
baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{
Name: ParseStructFieldTag.GetParamName(inputType.Field(i)),
In: consts.SwaggerParameterInQuery,
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
Required: ValidateRule.IsRequired(inputType.Field(i)),
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
Schema: &define.Schema{
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
Items: nil,
Ref: "",
Format: realInputTypeFormat,
},
AllowEmptyValue: false,
Style: "",
Explode: false,
AllowReserved: false,
Ref: "",
})
continue
}
} else {
baseReqCfg.Parameters = append(baseReqCfg.Parameters, &define.PathConfigParameter{
Name: ParseStructFieldTag.GetParamName(inputType.Field(i)),
In: consts.SwaggerParameterInQuery,
@ -417,7 +399,7 @@ func (g *Generate) ParseReadConfigParam(requestCfg *define.UriBaseConfig, baseRe
Required: ValidateRule.IsRequired(inputType.Field(i)),
Deprecated: ParseStructFieldTag.Deprecated(inputType.Field(i)),
Schema: &define.Schema{
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
Type: convertBaseType,
Items: nil,
Ref: "",
Format: realInputTypeFormat,
@ -524,8 +506,9 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
}
} else {
// 当做默认基础类型, 默认不会出现 *map *[]
convertBaseType, _ := g.realBaseType2SwaggerType(inputType.Field(i).Type.String())
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: g.realBaseType2SwaggerType(g.realBaseType2SwaggerType(inputType.Field(i).Type.String())),
Type: convertBaseType,
Format: inputType.Field(i).Type.String(),
Default: ParseStructFieldTag.GetDefaultValue(inputType.Field(i)),
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
@ -559,10 +542,10 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
} else {
g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type)
}
} else {
convertBaseType, _ := g.realBaseType2SwaggerType(inputType.Field(i).Type.String())
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
Type: convertBaseType,
Format: inputType.Field(i).Type.String(),
Default: ParseStructFieldTag.GetDefaultValue(inputType.Field(i)),
Description: ParseStructFieldTag.GetParamDesc(inputType.Field(i)),
@ -579,7 +562,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
// 非基础数据类型
return g.AddComponentsSchema(schemaName, inputType.Elem().String(), inputType.Elem())
} else {
convertType := g.realBaseType2SwaggerType(inputType.String())
convertType, _ := g.realBaseType2SwaggerType(inputType.String())
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
Type: convertType,
Format: inputType.String(),
@ -615,9 +598,10 @@ func (g *Generate) handleAnonymousField(schemaName string, field reflect.StructF
g.AddComponentsSchema(schemaName, handleType.Field(i).Type.PkgPath(), handleType.Field(i).Type)
continue
} else {
convertBaseType, _ := g.realBaseType2SwaggerType(handleType.Field(i).Type.String())
paramName := ParseStructFieldTag.GetParamName(itemField)
g.docData.Components.Schemas[schemaName].Properties[paramName] = &define.Property{
Type: g.realBaseType2SwaggerType(handleType.Field(i).Type.String()),
Type: convertBaseType,
Format: handleType.Field(i).Type.String(),
Enum: ValidateRule.Enum(handleType.Field(i)),
Default: ParseStructFieldTag.GetDefaultValue(handleType.Field(i)),
@ -671,37 +655,28 @@ func (g *Generate) getSchemaRef(schemaName string) string {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:25 2025/2/11
func (g *Generate) realBaseType2SwaggerType(realType string) string {
func (g *Generate) realBaseType2SwaggerType(realType string) (string, bool) {
switch realType {
case "bool", "*bool":
return consts.SwaggerDataTypeBoolean
return consts.SwaggerDataTypeBoolean, true
case "string", "*string":
return consts.SwaggerDataTypeString
return consts.SwaggerDataTypeString, true
case "byte", "*byte":
return consts.SwaggerDataTypeByte
return consts.SwaggerDataTypeByte, true
case "float32", "*float32", "float64", "*float64":
return consts.SwaggerDataTypeDouble
return consts.SwaggerDataTypeDouble, true
case "int", "*int", "uint", "*uint", "int64", "*int64", "uint64", "*uint64":
return consts.SwaggerDataTypeInteger
return consts.SwaggerDataTypeInteger, true
case "int8", "*int8", "uint8", "*uint8", "int16", "*int16", "uint16", "*uint16", "int32", "*int32", "uint32", "*uint32":
return consts.SwaggerDataTypeInteger
return consts.SwaggerDataTypeInteger, true
default:
return realType
if strings.HasPrefix(realType, "[]") {
return consts.SwaggerDataTypeArray, true
}
return realType, false
}
}
// 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)
}
// setStructFieldProperty 添加struct_field各种属性
//
// Author : go_developer@163.com<白茶清欢>
@ -770,6 +745,9 @@ func (g *Generate) parseBaseUriConfig(uriPrefix string, paramType reflect.Type)
}
res.OutputContentType = strings.Split(responseContentType, ",")
res.Summary = ParseStructFieldTag.Summary(metaField)
if len(res.Summary) == 0 {
res.Summary = wrapper.String(strings.ReplaceAll(strings.TrimLeft(res.Uri, "/"), "/", "_")).SnakeCaseToCamel()
}
if res.Method == "" {
return nil, errors.New("baseCfg.Method is empty")
}