fix: convert swagger field type string to integer

This commit is contained in:
zhuyasen 2025-07-20 19:11:10 +08:00
parent 7828f22442
commit 6b48d50a11
4 changed files with 15 additions and 15 deletions

View File

@ -25,6 +25,7 @@ func HandleSwaggerJSONCommand() *cobra.Command {
enableUniformResponse bool enableUniformResponse bool
enableConvertToOpenAPI3 bool enableConvertToOpenAPI3 bool
enableTransformIntegerToString bool enableTransformIntegerToString bool
enableStringToInteger bool
jsonFile string jsonFile string
) )
@ -47,6 +48,13 @@ func HandleSwaggerJSONCommand() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
var err error var err error
if enableStringToInteger {
if err = handleSwaggerFieldStringToInteger(jsonFile); err != nil {
return err
}
fmt.Printf("Successfully transform 64-bit string to integer in %s fields\n", jsonFile)
}
if enableUniformResponse { if enableUniformResponse {
if err = handleStandardizeResponseAction(jsonFile); err != nil { if err = handleStandardizeResponseAction(jsonFile); err != nil {
return err return err
@ -62,17 +70,6 @@ func HandleSwaggerJSONCommand() *cobra.Command {
fmt.Printf("Successfully convert swagger2.0 to openapi3.0, output: %s, %s\n", outputJSONFile, outputYamlFile) fmt.Printf("Successfully convert swagger2.0 to openapi3.0, output: %s, %s\n", outputJSONFile, outputYamlFile)
} }
if enableTransformIntegerToString {
if err = handleSwaggerIntegerToStringAction(jsonFile); err != nil {
return err
}
fmt.Printf("Successfully transform 64-bit integer to string in %s fields\n", jsonFile)
}
if enableUniformResponse == false && enableConvertToOpenAPI3 == false && enableTransformIntegerToString == false { //nolint
fmt.Println("No action specified, please use 'sponge web swagger -h' to see available options.")
}
return nil return nil
}, },
} }
@ -80,7 +77,8 @@ func HandleSwaggerJSONCommand() *cobra.Command {
cmd.Flags().StringVarP(&jsonFile, "file", "f", "docs/apis.swagger.json", "input swagger json file") cmd.Flags().StringVarP(&jsonFile, "file", "f", "docs/apis.swagger.json", "input swagger json file")
cmd.Flags().BoolVarP(&enableUniformResponse, "enable-standardize-response", "u", false, "standardize response format data in swagger json") cmd.Flags().BoolVarP(&enableUniformResponse, "enable-standardize-response", "u", false, "standardize response format data in swagger json")
cmd.Flags().BoolVarP(&enableConvertToOpenAPI3, "enable-to-openapi3", "o", false, "convert swagger2.0 to openapi3") cmd.Flags().BoolVarP(&enableConvertToOpenAPI3, "enable-to-openapi3", "o", false, "convert swagger2.0 to openapi3")
cmd.Flags().BoolVarP(&enableTransformIntegerToString, "enable-integer-to-string", "s", true, "transform 64-bit integer into string in swagger.json fields") cmd.Flags().BoolVarP(&enableStringToInteger, "enable-string-to-integer", "t", true, "transform string into 64-bit integer in swagger.json fields")
cmd.Flags().BoolVarP(&enableTransformIntegerToString, "enable-integer-to-string", "s", false, "Deprecated, instead use --enable-string-to-integer")
return cmd return cmd
} }
@ -470,7 +468,7 @@ func getOutputFile(filePath string) (yamlFile string, jsonFile string) {
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------
func handleSwaggerIntegerToStringAction(jsonFilePath string) error { func handleSwaggerFieldStringToInteger(jsonFilePath string) error {
newData, err := convertStringToInteger(jsonFilePath) newData, err := convertStringToInteger(jsonFilePath)
if err != nil { if err != nil {
return err return err

View File

@ -201,7 +201,7 @@ function generateBySpecifiedProto(){
checkResult $? checkResult $?
# handle apis.swagger.json # handle apis.swagger.json
sponge web swagger --enable-standardize-response --enable-to-openapi3 --enable-integer-to-string --file=docs/apis.swagger.json > /dev/null sponge web swagger --enable-standardize-response --enable-to-openapi3 --file=docs/apis.swagger.json > /dev/null
# A total of four files are generated: the registration route file *_router.pb.go (saved in the same directory as the protobuf file), # A total of four files are generated: the registration route file *_router.pb.go (saved in the same directory as the protobuf file),
# the injection route file *_router.go (saved in internal/routers by default), the logic code template file *.go (saved in internal/service by default), # the injection route file *_router.go (saved in internal/routers by default), the logic code template file *.go (saved in internal/service by default),

View File

@ -27,6 +27,8 @@ sleep 0.2
go build -o ${binaryFile} cmd/${serverName}/main.go go build -o ${binaryFile} cmd/${serverName}/main.go
checkResult $? checkResult $?
trap 'exit 0' SIGINT
# running server # running server
if test -f "$configFile"; then if test -f "$configFile"; then
./${binaryFile} -c $configFile ./${binaryFile} -c $configFile

View File

@ -30,7 +30,7 @@ checkResult $?
sponge patch modify-dup-num --dir=internal/ecode sponge patch modify-dup-num --dir=internal/ecode
sponge patch modify-dup-err-code --dir=internal/ecode sponge patch modify-dup-err-code --dir=internal/ecode
# handle swagger.json # handle swagger.json
sponge web swagger --enable-to-openapi3 --enable-integer-to-string --file=docs/swagger.json > /dev/null sponge web swagger --enable-to-openapi3 --file=docs/swagger.json > /dev/null
colorGreen='\033[1;32m' colorGreen='\033[1;32m'
colorCyan='\033[1;36m' colorCyan='\033[1;36m'