Skip to content

Commit bf6c5fc

Browse files
author
Xinzhao Xu
authored
fix(api-docs): handle the default value of parameter correctly (#333)
* fix(api-docs): handle the default value of parameter correctly * fix: address comments
1 parent 18e87d7 commit bf6c5fc

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

utils/generators/swagger/generator.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package swagger
1818

1919
import (
20+
"context"
21+
"encoding/json"
2022
"fmt"
2123
"net/http"
2224
"reflect"
@@ -485,7 +487,41 @@ func (g *Generator) generateAutoParameter(typ api.TypeName) []spec.Parameter {
485487
return nil
486488
}
487489
return g.enum(structType)
490+
}
488491

492+
var converters = map[string]service.Converter{
493+
"bool": service.ConvertToBool,
494+
"int": service.ConvertToInt,
495+
"int8": service.ConvertToInt8,
496+
"int16": service.ConvertToInt16,
497+
"int32": service.ConvertToInt32,
498+
"int64": service.ConvertToInt64,
499+
"uint": service.ConvertToUint,
500+
"uint8": service.ConvertToUint8,
501+
"uint16": service.ConvertToUint16,
502+
"uint32": service.ConvertToUint32,
503+
"uint64": service.ConvertToUint64,
504+
"float32": service.ConvertToFloat32,
505+
"float64": service.ConvertToFloat64,
506+
"string": service.ConvertToString,
507+
"*bool": service.ConvertToBoolP,
508+
"*int": service.ConvertToIntP,
509+
"*int8": service.ConvertToInt8P,
510+
"*int16": service.ConvertToInt16P,
511+
"*int32": service.ConvertToInt32P,
512+
"*int64": service.ConvertToInt64P,
513+
"*uint": service.ConvertToUintP,
514+
"*uint8": service.ConvertToUint8P,
515+
"*uint16": service.ConvertToUint16P,
516+
"*uint32": service.ConvertToUint32P,
517+
"*uint64": service.ConvertToUint64P,
518+
"*float32": service.ConvertToFloat32P,
519+
"*float64": service.ConvertToFloat64P,
520+
"*string": service.ConvertToStringP,
521+
"[]bool": service.ConvertToBoolSlice,
522+
"[]int": service.ConvertToIntSlice,
523+
"[]float64": service.ConvertToFloat64Slice,
524+
"[]string": service.ConvertToStringSlice,
489525
}
490526

491527
func (g *Generator) enum(typ *api.Type) []spec.Parameter {
@@ -495,14 +531,22 @@ func (g *Generator) enum(typ *api.Type) []spec.Parameter {
495531
parameters := []spec.Parameter(nil)
496532
if tag != "" {
497533
source, name, apc, err := service.ParseAutoParameterTag(tag)
498-
defaultValue := apc.Get(service.AutoParameterConfigKeyDefaultValue)
534+
rawDefaultValue := apc.Get(service.AutoParameterConfigKeyDefaultValue)
535+
var defaultValue []byte
536+
if c := converters[string(field.Type)]; rawDefaultValue != "" && c != nil {
537+
// we don't find a good way to handle the default value of non-basic types,
538+
// so for now the default value of those types are always empty
539+
v, _ := c(context.TODO(), []string{rawDefaultValue})
540+
defaultValue, _ = json.Marshal(v)
541+
}
542+
499543
if err == nil {
500544
parameters = g.generateParameter(&api.Parameter{
501545
Source: source,
502546
Name: name,
503547
Description: g.escapeNewline(field.Comments),
504548
Type: field.Type,
505-
Default: []byte(defaultValue),
549+
Default: defaultValue,
506550
})
507551
}
508552
} else {

0 commit comments

Comments
 (0)