mirror of https://github.com/zhufuyi/sponge
feat: convert swagger2.0 to openapi3
This commit is contained in:
parent
69aebc1f1f
commit
5c44839aa9
|
@ -0,0 +1,95 @@
|
|||
package generate
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/fatih/color"
|
||||
"github.com/getkin/kin-openapi/openapi2"
|
||||
"github.com/getkin/kin-openapi/openapi2conv"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/go-dev-frame/sponge/pkg/gofile"
|
||||
)
|
||||
|
||||
// ConvertSwagger2ToOpenAPI3Command convert swagger2 to openapi3
|
||||
func ConvertSwagger2ToOpenAPI3Command() *cobra.Command {
|
||||
var file string
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "swagger2-to-openapi3",
|
||||
Short: "Convert swagger2.0 to openapi3",
|
||||
Long: "Convert swagger2.0 to openapi3.",
|
||||
Example: color.HiBlackString(` # Convert swagger2.0 files to openapi3
|
||||
sponge web swagger2-to-openapi3 --file=swagger.json`),
|
||||
SilenceErrors: true,
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return convertOpenAPI3(file)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&file, "file", "f", "", "input json file path")
|
||||
_ = cmd.MarkFlagRequired("file")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func convertOpenAPI3(inputFile string) error {
|
||||
if gofile.GetFileSuffixName(inputFile) != ".json" {
|
||||
return fmt.Errorf("input file must be a json file")
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(inputFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outputYAML, outputJSON := getOutputFile(inputFile)
|
||||
|
||||
var swaggerDoc openapi2.T
|
||||
if err = json.Unmarshal(data, &swaggerDoc); err != nil {
|
||||
return fmt.Errorf("parse swagger json file failed: %v", err)
|
||||
}
|
||||
|
||||
openapi3Doc, err := openapi2conv.ToV3(&swaggerDoc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("convert to openapi3 failed: %v", err)
|
||||
}
|
||||
|
||||
jsonData, err := json.MarshalIndent(openapi3Doc, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("serialize to json failed: %v", err)
|
||||
}
|
||||
if err = os.WriteFile(outputJSON, jsonData, 0644); err != nil {
|
||||
return fmt.Errorf("write json file failed: %v", err)
|
||||
}
|
||||
|
||||
yamlData, err := yaml.Marshal(openapi3Doc)
|
||||
if err != nil {
|
||||
return fmt.Errorf("serialize to yaml failed: %v", err)
|
||||
}
|
||||
if err = os.WriteFile(outputYAML, yamlData, 0644); err != nil {
|
||||
return fmt.Errorf("write yaml file failed: %v", err)
|
||||
}
|
||||
|
||||
fmt.Printf("conversion successful, output files: %s, %s\n", outputJSON, outputYAML)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getOutputFile(filePath string) (yamlFile string, jsonFile string) {
|
||||
var suffix string
|
||||
if strings.HasSuffix(filePath, "swagger.json") {
|
||||
suffix = "swagger.json"
|
||||
|
||||
} else {
|
||||
suffix = gofile.GetFilename(filePath)
|
||||
}
|
||||
yamlFile = strings.TrimSuffix(filePath, suffix) + "openapi3.yaml"
|
||||
jsonFile = strings.TrimSuffix(filePath, suffix) + "openapi3.json"
|
||||
return yamlFile, jsonFile
|
||||
}
|
|
@ -279,8 +279,8 @@ func NewCenter(configFile string) (*Center, error) {
|
|||
|
||||
checkResult $?
|
||||
|
||||
sponge web swagger --file=docs/apis.swagger.json
|
||||
checkResult $?
|
||||
sponge web swagger2-to-openapi3 --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
sponge web swagger --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
|
||||
moduleName=$(cat docs/gen.info | head -1 | cut -d , -f 1)
|
||||
serverName=$(cat docs/gen.info | head -1 | cut -d , -f 2)
|
||||
|
@ -308,8 +308,8 @@ func NewCenter(configFile string) (*Center, error) {
|
|||
|
||||
checkResult $?
|
||||
|
||||
sponge micro swagger --file=docs/apis.swagger.json
|
||||
checkResult $?
|
||||
sponge web swagger2-to-openapi3 --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
sponge web swagger --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
|
||||
moduleName=$(cat docs/gen.info | head -1 | cut -d , -f 1)
|
||||
serverName=$(cat docs/gen.info | head -1 | cut -d , -f 2)
|
||||
|
@ -337,8 +337,8 @@ func NewCenter(configFile string) (*Center, error) {
|
|||
|
||||
checkResult $?
|
||||
|
||||
sponge web swagger --file=docs/apis.swagger.json
|
||||
checkResult $?
|
||||
sponge web swagger2-to-openapi3 --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
sponge web swagger --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
|
||||
moduleName=$(cat docs/gen.info | head -1 | cut -d , -f 1)
|
||||
serverName=$(cat docs/gen.info | head -1 | cut -d , -f 2)
|
||||
|
|
|
@ -24,6 +24,7 @@ func GenWebCommand() *cobra.Command {
|
|||
generate.HTTPCommand(),
|
||||
generate.HTTPPbCommand(),
|
||||
generate.ConvertSwagJSONCommand("web"),
|
||||
generate.ConvertSwagger2ToOpenAPI3Command(),
|
||||
generate.HandlerPbCommand(),
|
||||
)
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ function handlePbGoFiles(){
|
|||
cd ..
|
||||
}
|
||||
|
||||
autoDetectTypesProto() {
|
||||
function autoDetectTypesProto() {
|
||||
local target="api/types/types.proto"
|
||||
for file in "$@"; do
|
||||
if [ "$file" = "$target" ]; then
|
||||
|
@ -200,9 +200,8 @@ function generateBySpecifiedProto(){
|
|||
|
||||
checkResult $?
|
||||
|
||||
# convert 64-bit fields type string to integer
|
||||
sponge web swagger --file=docs/apis.swagger.json > /dev/null
|
||||
checkResult $?
|
||||
sponge web swagger2-to-openapi3 --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
sponge web swagger --file=docs/apis.swagger.json > /dev/null 2>&1
|
||||
|
||||
# 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),
|
||||
|
|
|
@ -29,6 +29,8 @@ checkResult $?
|
|||
# modify duplicate numbers and error codes
|
||||
sponge patch modify-dup-num --dir=internal/ecode
|
||||
sponge patch modify-dup-err-code --dir=internal/ecode
|
||||
sponge web swagger2-to-openapi3 --file=docs/swagger.json > /dev/null 2>&1
|
||||
sponge web swagger --file=docs/swagger.json > /dev/null 2>&1
|
||||
|
||||
colorGreen='\033[1;32m'
|
||||
colorCyan='\033[1;36m'
|
||||
|
|
Loading…
Reference in New Issue