基础简版swagger文档生成

This commit is contained in:
白茶清欢 2024-04-23 12:17:46 +08:00
parent d86880c329
commit 8bc001fc79
2 changed files with 17 additions and 18 deletions

View File

@ -61,7 +61,7 @@ type SwaggerPathConfig struct {
// //
// Date : 16:53 2024/4/19 // Date : 16:53 2024/4/19
type SwaggerPathConfigParameter struct { type SwaggerPathConfigParameter struct {
Type string `json:"type"` // 类型 Type string `json:"type,omitempty"` // 类型
Description string `json:"description"` // 描述 Description string `json:"description"` // 描述
Name string `json:"name"` // 参数名称 Name string `json:"name"` // 参数名称
In string `json:"in"` // 参数位置 In string `json:"in"` // 参数位置
@ -87,7 +87,7 @@ type SwaggerPathConfigResponse struct {
// Date : 17:01 2024/4/19 // Date : 17:01 2024/4/19
type SwaggerDefinition struct { type SwaggerDefinition struct {
Type string `json:"type"` // 类型 Type string `json:"type"` // 类型
Required []string `json:"required"` // 必传参数列表 Required []string `json:"required,omitempty"` // 必传参数列表
Properties map[string]*SwaggerDefinitionProperty `json:"properties"` // 参数名 => 参数配置 Properties map[string]*SwaggerDefinitionProperty `json:"properties"` // 参数名 => 参数配置
} }

View File

@ -121,7 +121,7 @@ func generatePathConfig(swaggerInfo *define.Swagger, docConfig *define.SwaggerIn
func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define.SwaggerPathInput) { func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define.SwaggerPathInput) {
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = make([]*define.SwaggerPathConfigParameter, 0) swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Parameters = make([]*define.SwaggerPathConfigParameter, 0)
hasDealTable := map[string]bool{} hasDealTable := map[string]bool{}
// hasDealResponseTable := map[string]bool{} hasDealResponseTable := map[string]bool{}
for _, itemParamInput := range pathConfig.ParameterList { for _, itemParamInput := range pathConfig.ParameterList {
if len(itemParamInput.Name) == 0 { if len(itemParamInput.Name) == 0 {
@ -140,8 +140,8 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define
if _, exist := hasDealTable[namePath[0]]; !exist { if _, exist := hasDealTable[namePath[0]]; !exist {
hasDealTable[namePath[0]] = true hasDealTable[namePath[0]] = true
generateParam := &define.SwaggerPathConfigParameter{ generateParam := &define.SwaggerPathConfigParameter{
Type: wrapper.TernaryOperator.String(realParamName == "jsonBody", "object", wrapper.String(itemParamInput.Type)).Value(), Type: wrapper.TernaryOperator.String(realParamName == "jsonBody", "", wrapper.String(itemParamInput.Type)).Value(),
Description: wrapper.TernaryOperator.String(realParamName == "jsonBody", "object", wrapper.String(itemParamInput.Description)).Value(), Description: wrapper.TernaryOperator.String(realParamName == "jsonBody", "参数结构", wrapper.String(itemParamInput.Description)).Value(),
Name: realParamName, Name: realParamName,
In: itemParamInput.In, In: itemParamInput.In,
Required: itemParamInput.Required, Required: itemParamInput.Required,
@ -154,28 +154,27 @@ func generatePathParameterConfig(swaggerInfo *define.Swagger, pathConfig *define
} }
} }
/*for _, itemResponseConfig := range pathConfig.ResponseList { for _, itemResponseConfig := range pathConfig.ResponseList {
for _, itemResponseInput := range itemResponseConfig.List { for _, itemResponseInput := range itemResponseConfig.List {
if len(itemResponseInput.Field) == 0 { if len(itemResponseInput.Field) == 0 {
// 未指定参数名, 忽略 // 未指定参数名, 忽略
continue continue
} }
// name 可能是 x.x.x 递归数组, 或者 x.x.[].x // name 可能是 x.x.x 递归数组, 或者 x.x.[].x
outputDefine := pathConfig.Uri + "." + itemResponseConfig.Code + ".output" outputDefine := strings.TrimLeft(pathConfig.Uri, "/") + "." + itemResponseConfig.Code + ".output"
generateResponseDefinitions(swaggerInfo, pathConfig.Uri, outputDefine, itemResponseInput.Field, itemResponseInput) generateResponseDefinitions(swaggerInfo, pathConfig.Uri, outputDefine, itemResponseInput.Field, itemResponseInput)
namePath := strings.Split(itemResponseInput.Field, ".") namePath := strings.Split(itemResponseInput.Field, ".")
if _, exist := hasDealResponseTable[namePath[0]]; !exist { if _, exist := hasDealResponseTable[namePath[0]]; !exist {
hasDealResponseTable[namePath[0]] = true hasDealResponseTable[namePath[0]] = true
swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Responses[itemResponseConfig.Code] = &define.SwaggerPathConfigResponse{ swaggerInfo.Paths[pathConfig.Uri][pathConfig.Method].Responses[itemResponseConfig.Code] = &define.SwaggerPathConfigResponse{
Description: itemResponseInput.Description, Description: "返回数据",
Schema: map[string]string{ Schema: map[string]string{
"$ref": "#/definitions" + outputDefine, "$ref": "#/definitions/" + outputDefine,
}, },
} }
} }
} }
}
}*/
} }
// generateParameterDefinitions 生成请求参数的描述 // generateParameterDefinitions 生成请求参数的描述
@ -258,11 +257,11 @@ func generateResponseDefinitions(swaggerInfo *define.Swagger, uri string, parent
if len(parentPath) == 0 { if len(parentPath) == 0 {
return return
} }
swaggerInfo.Definitions[parentPath].Type = responseConfig.Type // swaggerInfo.Definitions[parentPath].Type = responseConfig.Type
/*swaggerInfo.Definitions[parentPath].Properties[paramConfig.Name] = &define.SwaggerDefinitionProperty{ swaggerInfo.Definitions[parentPath].Properties[responseConfig.Field] = &define.SwaggerDefinitionProperty{
Description: paramConfig.Description, Description: responseConfig.Description,
Type: paramConfig.Type, Type: responseConfig.Type,
}*/ }
return return
} }
if len(parentPath) == 0 { if len(parentPath) == 0 {