From f4192d695ce6cde99ec5153f2348d9de9105ad3a Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 24 Mar 2022 22:08:06 +0800 Subject: [PATCH] remove sort feature for openapi --- net/ghttp/ghttp_server_openapi.go | 6 +- protocol/goai/goai.go | 60 ------------------- protocol/goai/goai_path.go | 1 - protocol/goai/goai_z_unit_paths_sort_test.go | 61 -------------------- 4 files changed, 4 insertions(+), 124 deletions(-) delete mode 100644 protocol/goai/goai_z_unit_paths_sort_test.go diff --git a/net/ghttp/ghttp_server_openapi.go b/net/ghttp/ghttp_server_openapi.go index 9c8dc2109..035063f04 100644 --- a/net/ghttp/ghttp_server_openapi.go +++ b/net/ghttp/ghttp_server_openapi.go @@ -48,11 +48,13 @@ func (s *Server) initOpenApi() { // openapiSpec is a build-in handler automatic producing for openapi specification json file. func (s *Server) openapiSpec(r *Request) { - var err error + var ( + err error + ) if s.config.OpenApiPath == "" { r.Response.Write(`OpenApi specification file producing is disabled`) } else { - err = r.Response.WriteJson(s.openapi.MustJson()) + err = r.Response.WriteJson(s.openapi) } if err != nil { diff --git a/protocol/goai/goai.go b/protocol/goai/goai.go index cb82cacce..9eb473cae 100644 --- a/protocol/goai/goai.go +++ b/protocol/goai/goai.go @@ -10,13 +10,10 @@ package goai import ( - "bytes" "context" "fmt" "reflect" - "github.com/gogf/gf/v2/container/garray" - "github.com/gogf/gf/v2/encoding/gjson" "github.com/gogf/gf/v2/errors/gcode" "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/internal/intlog" @@ -241,63 +238,6 @@ func (oai *OpenApiV3) fileMapWithShortTags(m map[string]string) map[string]strin return m } -func (oai OpenApiV3) MustJson() []byte { - b, err := oai.Json() - if err != nil { - panic(err) - } - return b -} - -// Json converts current OpenApi object to JSON format. -// Note that this function support `Sort` feature of Path which sets the Path in custom sequence. -func (oai OpenApiV3) Json() ([]byte, error) { - // Marshal Paths in sequence by `Sort` attribute. - type PathItem struct { - Name string - Path Path - } - array := garray.NewSortedArray(func(a, b interface{}) int { - path1 := a.(PathItem) - path2 := b.(PathItem) - return path1.Path.Sort - path2.Path.Sort - }) - for name, path := range oai.Paths { - array.Add(PathItem{ - Name: name, - Path: path, - }) - } - buffer := bytes.NewBuffer(nil) - buffer.WriteString("{") - array.Iterator(func(k int, v interface{}) bool { - if buffer.Len() > 1 { - buffer.WriteString(",") - } - item := v.(PathItem) - buffer.WriteString(fmt.Sprintf(`"%s":%s`, item.Name, gjson.MustEncodeString(item.Path))) - return true - }) - buffer.WriteString("}") - // Produce JSON content. - oaiJsonBytes, err := json.Marshal(oai) - if err != nil { - return nil, err - } - oaiJson, err := gjson.LoadJson(oaiJsonBytes) - if err != nil { - return nil, err - } - pathJson, err := gjson.LoadJson(buffer.Bytes()) - if err != nil { - return nil, err - } - if err = oaiJson.Set(`paths`, pathJson); err != nil { - return nil, err - } - return oaiJson.ToJson() -} - func formatRefToBytes(ref string) []byte { return []byte(fmt.Sprintf(`{"$ref":"#/components/schemas/%s"}`, ref)) } diff --git a/protocol/goai/goai_path.go b/protocol/goai/goai_path.go index 809ff021b..464ed7ccf 100644 --- a/protocol/goai/goai_path.go +++ b/protocol/goai/goai_path.go @@ -32,7 +32,6 @@ type Path struct { Trace *Operation `json:"trace,omitempty"` Servers Servers `json:"servers,omitempty"` Parameters Parameters `json:"parameters,omitempty"` - Sort int `json:"-"` } // Paths are specified by OpenAPI/Swagger standard version 3.0. diff --git a/protocol/goai/goai_z_unit_paths_sort_test.go b/protocol/goai/goai_z_unit_paths_sort_test.go deleted file mode 100644 index ada4c254f..000000000 --- a/protocol/goai/goai_z_unit_paths_sort_test.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. -// -// This Source Code Form is subject to the terms of the MIT License. -// If a copy of the MIT was not distributed with this file, -// You can obtain one at https://github.com/gogf/gf. - -package goai_test - -import ( - "context" - "testing" - - "github.com/gogf/gf/v2/protocol/goai" - "github.com/gogf/gf/v2/test/gtest" - "github.com/gogf/gf/v2/text/gstr" - "github.com/gogf/gf/v2/util/gmeta" -) - -func TestOpenApiV3_Path_Sort(t *testing.T) { - type Req1 struct { - gmeta.Meta `method:"GET" sort:"1"` - Name1 string `json:"name1" in:"query" ` - } - type Res1 struct{} - - type Req2 struct { - gmeta.Meta `method:"GET" sort:"2"` - Name2 string `json:"name2" in:"query"` - } - type Res2 struct{} - - f1 := func(ctx context.Context, req *Req1) (res *Res1, err error) { - return - } - f2 := func(ctx context.Context, req *Req2) (res *Res2, err error) { - return - } - - gtest.C(t, func(t *gtest.T) { - for i := 0; i < 100; i++ { - var ( - err error - oai = goai.New() - ) - err = oai.Add(goai.AddInput{ - Path: "/index2", - Object: f2, - }) - t.AssertNil(err) - err = oai.Add(goai.AddInput{ - Path: "/index1", - Object: f1, - }) - t.AssertNil(err) - - b, err := oai.Json() - t.AssertNil(err) - t.Assert(gstr.Contains(string(b), `"paths":{"/index1":`), true) - } - }) -}