Skip to content

Commit abb75d7

Browse files
committed
feat(swagger): When wrapCodeMsgMapping is code or data or msg, wrapCodeMsg is ignored
feat(swagger): When wrapCodeMsgMapping data is empty ugnore wrap
1 parent a134948 commit abb75d7

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

tools/goctl/api/swagger/annotation.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package swagger
33
import (
44
"strconv"
55

6-
"github.com/zeromicro/go-zero/tools/goctl/util"
76
"google.golang.org/grpc/metadata"
7+
8+
"github.com/zeromicro/go-zero/tools/goctl/util"
89
)
910

1011
func getBoolFromKVOrDefault(properties map[string]string, key string, def bool) bool {
@@ -40,6 +41,14 @@ func getStringFromKVOrDefault(properties map[string]string, key string, def stri
4041
return str
4142
}
4243

44+
func isExist(properties map[string]string, key string) bool {
45+
if len(properties) == 0 {
46+
return false
47+
}
48+
_, ok := properties[key]
49+
return ok
50+
}
51+
4352
func getListFromInfoOrDefault(properties map[string]string, key string, def []string) []string {
4453
if len(properties) == 0 {
4554
return def

tools/goctl/api/swagger/context.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,18 @@ func contextFromApi(info spec.Info) Context {
2121
if len(info.Properties) == 0 {
2222
return Context{}
2323
}
24-
return Context{
24+
25+
ctx := Context{
2526
UseDefinitions: getBoolFromKVOrDefault(info.Properties, propertyKeyUseDefinitions, defaultValueOfPropertyUseDefinition),
2627
WrapCodeMsg: getBoolFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsg, false),
27-
WrapCodeMsgMapping: getStringFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsgMapping, defaultValueOfPropertyWrapCodeMsgMapping),
2828
BizCodeEnumDescription: getStringFromKVOrDefault(info.Properties, propertyKeyBizCodeEnumDescription, "business code"),
2929
}
30+
31+
if !isExist(info.Properties, propertyKeyWrapCodeMsgMapping) {
32+
ctx.WrapCodeMsgMapping = defaultValueOfPropertyWrapCodeMsgMapping
33+
} else {
34+
ctx.WrapCodeMsgMapping = getStringFromKVOrDefault(info.Properties, propertyKeyWrapCodeMsgMapping, "")
35+
}
36+
37+
return ctx
3038
}

tools/goctl/api/swagger/swagger.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/go-openapi/spec"
9+
910
apiSpec "github.com/zeromicro/go-zero/tools/goctl/api/spec"
1011
"github.com/zeromicro/go-zero/tools/goctl/internal/version"
1112
)
@@ -271,34 +272,47 @@ func wrapCodeMsgProps(ctx Context, properties spec.SchemaProps, atDoc apiSpec.At
271272
return properties
272273
}
273274

275+
if len(wrapCodeMsgMapping.Data) == 0 {
276+
return properties
277+
}
278+
274279
globalCodeDesc := ctx.BizCodeEnumDescription
275280
methodCodeDesc := getStringFromKVOrDefault(atDoc.Properties, propertyKeyBizCodeEnumDescription, globalCodeDesc)
276-
return spec.SchemaProps{
281+
282+
schemaProps := spec.SchemaProps{
277283
Type: []string{swaggerTypeObject},
278284
Properties: spec.SchemaProperties{
279-
wrapCodeMsgMapping.Code: {
280-
SwaggerSchemaProps: spec.SwaggerSchemaProps{
281-
Example: 0,
282-
},
283-
SchemaProps: spec.SchemaProps{
284-
Type: []string{swaggerTypeInteger},
285-
Description: methodCodeDesc,
286-
},
287-
},
288-
wrapCodeMsgMapping.Msg: {
289-
SwaggerSchemaProps: spec.SwaggerSchemaProps{
290-
Example: "ok",
291-
},
292-
SchemaProps: spec.SchemaProps{
293-
Type: []string{swaggerTypeString},
294-
Description: "business message",
295-
},
296-
},
297285
wrapCodeMsgMapping.Data: {
298286
SchemaProps: properties,
299287
},
300288
},
301289
}
290+
291+
if len(wrapCodeMsgMapping.Code) > 0 {
292+
schemaProps.Properties[wrapCodeMsgMapping.Code] = spec.Schema{
293+
SchemaProps: spec.SchemaProps{
294+
Description: methodCodeDesc,
295+
Type: []string{swaggerTypeInteger},
296+
},
297+
SwaggerSchemaProps: spec.SwaggerSchemaProps{
298+
Example: 0,
299+
},
300+
}
301+
}
302+
303+
if len(wrapCodeMsgMapping.Msg) > 0 {
304+
schemaProps.Properties[wrapCodeMsgMapping.Msg] = spec.Schema{
305+
SchemaProps: spec.SchemaProps{
306+
Description: "business message",
307+
Type: []string{swaggerTypeString},
308+
},
309+
SwaggerSchemaProps: spec.SwaggerSchemaProps{
310+
Example: "ok",
311+
},
312+
}
313+
}
314+
315+
return schemaProps
302316
}
303317

304318
func specExtensions(api apiSpec.Info) (spec.Extensions, *spec.Info) {

0 commit comments

Comments
 (0)