mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
remove sort feature for openapi
This commit is contained in:
parent
6664437b06
commit
f4192d695c
@ -48,11 +48,13 @@ func (s *Server) initOpenApi() {
|
|||||||
|
|
||||||
// openapiSpec is a build-in handler automatic producing for openapi specification json file.
|
// openapiSpec is a build-in handler automatic producing for openapi specification json file.
|
||||||
func (s *Server) openapiSpec(r *Request) {
|
func (s *Server) openapiSpec(r *Request) {
|
||||||
var err error
|
var (
|
||||||
|
err error
|
||||||
|
)
|
||||||
if s.config.OpenApiPath == "" {
|
if s.config.OpenApiPath == "" {
|
||||||
r.Response.Write(`OpenApi specification file producing is disabled`)
|
r.Response.Write(`OpenApi specification file producing is disabled`)
|
||||||
} else {
|
} else {
|
||||||
err = r.Response.WriteJson(s.openapi.MustJson())
|
err = r.Response.WriteJson(s.openapi)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -10,13 +10,10 @@
|
|||||||
package goai
|
package goai
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"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/gcode"
|
||||||
"github.com/gogf/gf/v2/errors/gerror"
|
"github.com/gogf/gf/v2/errors/gerror"
|
||||||
"github.com/gogf/gf/v2/internal/intlog"
|
"github.com/gogf/gf/v2/internal/intlog"
|
||||||
@ -241,63 +238,6 @@ func (oai *OpenApiV3) fileMapWithShortTags(m map[string]string) map[string]strin
|
|||||||
return m
|
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 {
|
func formatRefToBytes(ref string) []byte {
|
||||||
return []byte(fmt.Sprintf(`{"$ref":"#/components/schemas/%s"}`, ref))
|
return []byte(fmt.Sprintf(`{"$ref":"#/components/schemas/%s"}`, ref))
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,6 @@ type Path struct {
|
|||||||
Trace *Operation `json:"trace,omitempty"`
|
Trace *Operation `json:"trace,omitempty"`
|
||||||
Servers Servers `json:"servers,omitempty"`
|
Servers Servers `json:"servers,omitempty"`
|
||||||
Parameters Parameters `json:"parameters,omitempty"`
|
Parameters Parameters `json:"parameters,omitempty"`
|
||||||
Sort int `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Paths are specified by OpenAPI/Swagger standard version 3.0.
|
// Paths are specified by OpenAPI/Swagger standard version 3.0.
|
||||||
|
@ -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)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user