From fa1e762f51280f06b788f9faac56e51f1faa7458 Mon Sep 17 00:00:00 2001 From: James Tombleson Date: Wed, 17 Jul 2024 22:01:58 -0700 Subject: [PATCH] made a script to convert swagger to a openapi document --- tools/swaggertoopenapi/main.go | 17 ++++++ tools/swaggertoopenapi/src/online.go | 70 +++++++++++++++++++++++ tools/swaggertoopenapi/src/online_test.go | 14 +++++ 3 files changed, 101 insertions(+) create mode 100644 tools/swaggertoopenapi/main.go create mode 100644 tools/swaggertoopenapi/src/online.go create mode 100644 tools/swaggertoopenapi/src/online_test.go diff --git a/tools/swaggertoopenapi/main.go b/tools/swaggertoopenapi/main.go new file mode 100644 index 0000000..6404045 --- /dev/null +++ b/tools/swaggertoopenapi/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "os" + + "git.jamestombleson.com/jtom38/newsbot-api/tools/swaggertoopenapi/src" +) + + +func main() { + err := src.ConvertToOpenApi("docs/swagger.json", "docs/openapi.json", true) + if err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/tools/swaggertoopenapi/src/online.go b/tools/swaggertoopenapi/src/online.go new file mode 100644 index 0000000..29910d2 --- /dev/null +++ b/tools/swaggertoopenapi/src/online.go @@ -0,0 +1,70 @@ +package src + +import ( + "bytes" + "fmt" + "io" + "log" + "net/http" + "os" +) + +const ( + ApplicationJson = "application/json" +) + +func ConvertToOpenApi(filePath, outputPath string, force bool) error { + // check if the file already exists + exists, err := os.Stat(outputPath) + if exists != nil { + // if force was given, attempt to remove it + if force { + err = os.Remove(outputPath) + if err != nil { + return err + } + } else { + return fmt.Errorf("'%s' already exists and force was not approved", outputPath) + } + } + + log.Printf("Reading '%s'", filePath) + content, err := os.ReadFile(filePath) + if err != nil { + return err + } + + client := http.Client{} + req, err := http.NewRequest(http.MethodPost, "https://converter.swagger.io/api/convert", bytes.NewReader(content)) + if err != nil { + return err + } + req.Header.Add("Accept", ApplicationJson) + req.Header.Add("Content-Type", ApplicationJson) + + log.Println("Converting to OpenAPI spec") + resp, err := client.Do(req) + if err != nil { + return err + } + + log.Println("Reading the response") + respContent, err := io.ReadAll(resp.Body) + if err != nil { + return err + } + + log.Println("Writing converted data to openapi.json") + writer, err := os.Create(outputPath) + if err != nil { + return err + } + defer writer.Close() + + _, err = writer.Write(respContent) + if err != nil { + return err + } + + return nil +} diff --git a/tools/swaggertoopenapi/src/online_test.go b/tools/swaggertoopenapi/src/online_test.go new file mode 100644 index 0000000..30e34b3 --- /dev/null +++ b/tools/swaggertoopenapi/src/online_test.go @@ -0,0 +1,14 @@ +package src_test + +import ( + "testing" + + "git.jamestombleson.com/jtom38/newsbot-api/tools/swaggerToOpenapi/src" +) + +func TestConvertOnline(t *testing.T) { + err := src.ConvertToOpenApi("../../docs/swagger.json", "../../docs/openapi.json", true) + if err != nil { + t.FailNow() + } +}