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