Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tools/goctl/api/swagger/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ const (
propertyKeyHost = "host"
propertyKeyBasePath = "basePath"
propertyKeyWrapCodeMsg = "wrapCodeMsg"
propertyKeyWrapCodeMsgMapping = "wrapCodeMsgMapping"
propertyKeyBizCodeEnumDescription = "bizCodeEnumDescription"
)

const (
defaultValueOfPropertyUseDefinition = false
defaultValueOfPropertyUseDefinition = false
defaultValueOfPropertyWrapCodeMsgMapping = `{"code":"code", "data":"data", "msg":"msg"}`
)
2 changes: 2 additions & 0 deletions tools/goctl/api/swagger/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type Context struct {
UseDefinitions bool
WrapCodeMsg bool
WrapCodeMsgMapping string
BizCodeEnumDescription string
}

Expand All @@ -23,6 +24,7 @@ func contextFromApi(info spec.Info) Context {
return Context{
UseDefinitions: getBoolFromKVOrDefault(info.Properties, propertyKeyUseDefinitions, defaultValueOfPropertyUseDefinition),
WrapCodeMsg: getBoolFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsg, false),
WrapCodeMsgMapping: getStringFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsgMapping, defaultValueOfPropertyWrapCodeMsgMapping),
BizCodeEnumDescription: getStringFromKVOrDefault(info.Properties, propertyKeyBizCodeEnumDescription, "business code"),
}
}
18 changes: 15 additions & 3 deletions tools/goctl/api/swagger/swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,24 @@ func wrapCodeMsgProps(ctx Context, properties spec.SchemaProps, atDoc apiSpec.At
if !ctx.WrapCodeMsg {
return properties
}

type WrapCodeMsgMapping struct {
Code string `json:"code"`
Data string `json:"data"`
Msg string `json:"msg"`
}

var wrapCodeMsgMapping WrapCodeMsgMapping
if err := json.Unmarshal([]byte(ctx.WrapCodeMsgMapping), &wrapCodeMsgMapping); err != nil {
return properties
}

globalCodeDesc := ctx.BizCodeEnumDescription
methodCodeDesc := getStringFromKVOrDefault(atDoc.Properties, propertyKeyBizCodeEnumDescription, globalCodeDesc)
return spec.SchemaProps{
Type: []string{swaggerTypeObject},
Properties: spec.SchemaProperties{
"code": {
wrapCodeMsgMapping.Code: {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls ignore this operation if the value of wrapCodeMsgMapping.Code is empty, and the same operation withwrapCodeMsgMapping.Data, wrapCodeMsgMapping.Msg too.

SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: 0,
},
Expand All @@ -273,7 +285,7 @@ func wrapCodeMsgProps(ctx Context, properties spec.SchemaProps, atDoc apiSpec.At
Description: methodCodeDesc,
},
},
"msg": {
wrapCodeMsgMapping.Msg: {
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: "ok",
},
Expand All @@ -282,7 +294,7 @@ func wrapCodeMsgProps(ctx Context, properties spec.SchemaProps, atDoc apiSpec.At
Description: "business message",
},
},
"data": {
wrapCodeMsgMapping.Data: {
SchemaProps: properties,
},
},
Expand Down