openapi格式的文档基础生成 #3
47
generate.go
47
generate.go
@ -189,14 +189,18 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// post类解析
|
// post类解析
|
||||||
defaultPkgPath := wrapper.String(strings.ReplaceAll(strings.TrimLeft(baseCfg.Uri, "/"), "/", "_")).SnakeCaseToCamel()
|
if paramType.Kind() == reflect.Ptr {
|
||||||
paramPkgPath := defaultPkgPath + baseCfg.Method + "Param"
|
paramType = paramType.Elem()
|
||||||
resultPkgPath := defaultPkgPath + baseCfg.Method + "Result"
|
}
|
||||||
g.AddComponentsSchema("", paramPkgPath, paramType)
|
if resultType.Kind() == reflect.Ptr {
|
||||||
g.AddComponentsSchema("", resultPkgPath, resultType)
|
resultType = resultType.Elem()
|
||||||
|
}
|
||||||
|
g.AddComponentsSchema("", paramType.PkgPath(), paramType)
|
||||||
|
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()
|
||||||
cfg := &define.PathItemOperationConfig{
|
cfg := &define.PathItemOperationConfig{
|
||||||
Tags: baseCfg.TagList,
|
Tags: baseCfg.TagList,
|
||||||
Summary: baseCfg.Summary,
|
Summary: baseCfg.Summary,
|
||||||
@ -223,7 +227,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
for _, itemType := range baseCfg.ContentType {
|
for _, itemType := range baseCfg.ContentType {
|
||||||
cfg.RequestBody.Content[itemType] = &define.Media{
|
cfg.RequestBody.Content[itemType] = &define.Media{
|
||||||
Schema: &define.Schema{
|
Schema: &define.Schema{
|
||||||
Ref: g.getSchemaRes(paramPkgPath),
|
Ref: g.getSchemaRes(paramType.Name()),
|
||||||
},
|
},
|
||||||
Example: "",
|
Example: "",
|
||||||
Examples: nil,
|
Examples: nil,
|
||||||
@ -233,7 +237,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
for _, itemOutputType := range baseCfg.OutputContentType {
|
for _, itemOutputType := range baseCfg.OutputContentType {
|
||||||
cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{
|
cfg.Responses[fmt.Sprintf("%v", http.StatusOK)].Content[itemOutputType] = &define.Media{
|
||||||
Schema: &define.Schema{
|
Schema: &define.Schema{
|
||||||
Ref: g.getSchemaRes(resultPkgPath),
|
Ref: g.getSchemaRes(resultType.Name()),
|
||||||
},
|
},
|
||||||
Example: "",
|
Example: "",
|
||||||
Examples: nil,
|
Examples: nil,
|
||||||
@ -269,22 +273,6 @@ 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 {
|
||||||
if rootSchemaName != "" {
|
|
||||||
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
|
|
||||||
Type: g.realType2SwaggerType(inputType.String()),
|
|
||||||
Format: inputType.String(),
|
|
||||||
Enum: nil,
|
|
||||||
Default: "",
|
|
||||||
Description: "",
|
|
||||||
AllOf: nil,
|
|
||||||
OneOf: nil,
|
|
||||||
AnyOf: nil,
|
|
||||||
Items: nil,
|
|
||||||
AdditionalProperties: nil,
|
|
||||||
Properties: nil,
|
|
||||||
Ref: g.getSchemaRes(schemaName),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||||
Nullable: false,
|
Nullable: false,
|
||||||
@ -299,7 +287,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
Required: make([]string, 0),
|
Required: make([]string, 0),
|
||||||
Enum: make([]any, 0),
|
Enum: make([]any, 0),
|
||||||
Type: "",
|
Type: "",
|
||||||
Ref: "",
|
Ref: g.getSchemaRes(schemaName),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if inputType.Kind() == reflect.Map {
|
if inputType.Kind() == reflect.Map {
|
||||||
@ -311,7 +299,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
|
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
|
||||||
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
|
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
|
||||||
sliceItemType := g.parseSliceItem(schemaName, inputType)
|
sliceItemType := g.parseSliceItem(schemaName, inputType)
|
||||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Name()] = &define.Property{
|
g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{
|
||||||
Type: consts.SwaggerDataTypeArray,
|
Type: consts.SwaggerDataTypeArray,
|
||||||
Format: inputType.String(),
|
Format: inputType.String(),
|
||||||
Items: &define.PropertyXOf{Ref: g.getSchemaRes(sliceItemType)},
|
Items: &define.PropertyXOf{Ref: g.getSchemaRes(sliceItemType)},
|
||||||
@ -337,10 +325,9 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
g.AddComponentsSchema(schemaName, schemaName+g.getParamName(inputType.Field(i)), inputType.Field(i).Type)
|
|
||||||
if inputType.Field(i).Type.Kind() == reflect.Struct ||
|
if inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Map {
|
inputType.Field(i).Type.Kind() == reflect.Map {
|
||||||
g.docData.Components.Schemas[schemaName].Properties[schemaName+g.getParamName(inputType.Field(i))] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[g.getParamName(inputType.Field(i))] = &define.Property{
|
||||||
Type: consts.SwaggerDataTypeObject,
|
Type: consts.SwaggerDataTypeObject,
|
||||||
Format: inputType.Field(i).Type.String(),
|
Format: inputType.Field(i).Type.String(),
|
||||||
Description: g.getParamDesc(inputType.Field(i)),
|
Description: g.getParamDesc(inputType.Field(i)),
|
||||||
@ -348,7 +335,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
}
|
}
|
||||||
} else if inputType.Field(i).Type.Kind() == reflect.Array ||
|
} else if inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||||
g.docData.Components.Schemas[schemaName].Properties[schemaName+g.getParamName(inputType.Field(i))] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[g.getParamName(inputType.Field(i))] = &define.Property{
|
||||||
Type: consts.SwaggerDataTypeArray,
|
Type: consts.SwaggerDataTypeArray,
|
||||||
Format: inputType.Field(i).Type.String(),
|
Format: inputType.Field(i).Type.String(),
|
||||||
Description: g.getParamDesc(inputType.Field(i)),
|
Description: g.getParamDesc(inputType.Field(i)),
|
||||||
@ -359,6 +346,8 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
}
|
}
|
||||||
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
g.AddComponentsSchema(schemaName, g.getParamName(inputType.Field(i)), inputType.Field(i).Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -377,7 +366,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
convertType := g.realBaseType2SwaggerType(inputType.String())
|
convertType := g.realBaseType2SwaggerType(inputType.String())
|
||||||
if convertType == inputType.String() {
|
if convertType == inputType.String() {
|
||||||
// 非基础数据类型
|
// 非基础数据类型
|
||||||
return g.AddComponentsSchema(schemaName, schemaName+inputType.Elem().String(), inputType.Elem())
|
return g.AddComponentsSchema(schemaName, inputType.Elem().String(), inputType.Elem())
|
||||||
} else {
|
} else {
|
||||||
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
|
||||||
Type: convertType,
|
Type: convertType,
|
||||||
|
@ -23,13 +23,15 @@ import (
|
|||||||
//
|
//
|
||||||
// Date : 17:55 2024/7/19
|
// Date : 17:55 2024/7/19
|
||||||
func Test_parser_Openapi3(t *testing.T) {
|
func Test_parser_Openapi3(t *testing.T) {
|
||||||
type A struct {
|
type User struct {
|
||||||
Name string `json:"name" d:"zhang" desc:"用户姓名"`
|
Name string `json:"name" d:"zhang" desc:"用户姓名"`
|
||||||
|
Age int `json:"age" d:"18" desc:"年龄"`
|
||||||
}
|
}
|
||||||
type B struct {
|
type List struct {
|
||||||
List []A `json:"list"`
|
Total int64 `json:"total"`
|
||||||
|
UserList []User `json:"user_list"`
|
||||||
}
|
}
|
||||||
var bArr *B
|
var l List
|
||||||
g := NewOpenapiDoc(nil, nil)
|
g := NewOpenapiDoc(nil, nil)
|
||||||
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
g.AddApiFromInAndOut(&define.UriBaseConfig{
|
||||||
Uri: "/ent/user/detail",
|
Uri: "/ent/user/detail",
|
||||||
@ -41,10 +43,10 @@ func Test_parser_Openapi3(t *testing.T) {
|
|||||||
Description: "",
|
Description: "",
|
||||||
ParamList: nil,
|
ParamList: nil,
|
||||||
ResultList: nil,
|
ResultList: nil,
|
||||||
}, reflect.TypeOf(bArr), reflect.TypeOf(bArr))
|
}, reflect.TypeOf(l), reflect.TypeOf(l))
|
||||||
byteData, _ := json.Marshal(g.docData)
|
byteData, _ := json.Marshal(g.docData)
|
||||||
fmt.Println(string(byteData))
|
fmt.Println(string(byteData))
|
||||||
fmt.Println(g.parseSliceItem("", reflect.TypeOf(bArr)))
|
fmt.Println(g.parseSliceItem("", reflect.TypeOf(l)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseForSwagger(t *testing.T) {
|
func TestParseForSwagger(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user