openapi格式的文档基础生成 #3
22
generate.go
22
generate.go
@ -276,6 +276,10 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
|||||||
inputNameArr := strings.Split(inputType.Name(), ".")
|
inputNameArr := strings.Split(inputType.Name(), ".")
|
||||||
inputName := inputNameArr[len(inputNameArr)-1]
|
inputName := inputNameArr[len(inputNameArr)-1]
|
||||||
schemaName := strings.ReplaceAll(pkgPath+"."+inputName, "/", "-")
|
schemaName := strings.ReplaceAll(pkgPath+"."+inputName, "/", "-")
|
||||||
|
if _, exist := g.docData.Components.Schemas[schemaName]; exist {
|
||||||
|
// 已存在, 无需重复生成
|
||||||
|
return schemaName
|
||||||
|
}
|
||||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||||
s := &define.Schema{
|
s := &define.Schema{
|
||||||
Nullable: false,
|
Nullable: false,
|
||||||
@ -322,10 +326,8 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
|||||||
}
|
}
|
||||||
// g.docData.Components.Schemas[schemaName].Ref = consts.SwaggerDataTypeObject
|
// g.docData.Components.Schemas[schemaName].Ref = consts.SwaggerDataTypeObject
|
||||||
for i := 0; i < inputType.NumField(); i++ {
|
for i := 0; i < inputType.NumField(); i++ {
|
||||||
if ValidateRule.IsRequired(inputType.Field(i)) {
|
// 设置参数各种属性
|
||||||
// 必传字段
|
g.setStructFieldProperty(schemaName, inputType.Field(i))
|
||||||
g.docData.Components.Schemas[schemaName].Required = append(g.docData.Components.Schemas[schemaName].Required, ParseStructField.GetParamName(inputType.Field(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 ||
|
||||||
@ -461,3 +463,15 @@ func (g *Generate) realType2SwaggerType(realType string) string {
|
|||||||
}
|
}
|
||||||
return g.realBaseType2SwaggerType(realType)
|
return g.realBaseType2SwaggerType(realType)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setStructFieldProperty 添加struct_field各种属性
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 16:13 2025/2/13
|
||||||
|
func (g *Generate) setStructFieldProperty(schemaName string, structField reflect.StructField) {
|
||||||
|
paramName := ParseStructField.GetParamName(structField)
|
||||||
|
if ValidateRule.IsRequired(structField) {
|
||||||
|
g.docData.Components.Schemas[schemaName].Required = append(g.docData.Components.Schemas[schemaName].Required, paramName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -28,8 +28,8 @@ func Test_parser_Openapi3(t *testing.T) {
|
|||||||
Age int `json:"age" d:"18" desc:"年龄" binding:"required"`
|
Age int `json:"age" d:"18" desc:"年龄" binding:"required"`
|
||||||
}
|
}
|
||||||
type List struct {
|
type List struct {
|
||||||
Total int64 `json:"total"`
|
Total int64 `json:"total" binding:"required"`
|
||||||
UserList []User `json:"user_list" binding:"required"`
|
UserList []User `json:"user_list"`
|
||||||
}
|
}
|
||||||
var l List
|
var l List
|
||||||
g := NewOpenapiDoc(nil, nil)
|
g := NewOpenapiDoc(nil, nil)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user