diff --git a/protocol/goai/goai_path.go b/protocol/goai/goai_path.go index 62f29f142..51d837360 100644 --- a/protocol/goai/goai_path.go +++ b/protocol/goai/goai_path.go @@ -34,9 +34,6 @@ type Path struct { Parameters Parameters `json:"parameters,omitempty" yaml:"parameters,omitempty"` } -// Paths are specified by OpenAPI/Swagger standard version 3.0. -type Paths map[string]Path - const ( responseOkKey = `200` ) @@ -49,10 +46,6 @@ type addPathInput struct { } func (oai *OpenApiV3) addPath(in addPathInput) error { - if oai.Paths == nil { - oai.Paths = map[string]Path{} - } - var ( reflectType = reflect.TypeOf(in.Function) ) @@ -106,8 +99,8 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { ) } - if v, ok := oai.Paths[in.Path]; ok { - path = v + if v := oai.Paths.Get(in.Path); v != nil { + path = *v } // Method check. @@ -280,7 +273,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { default: return gerror.NewCodef(gcode.CodeInvalidParameter, `invalid method "%s"`, in.Method) } - oai.Paths[in.Path] = path + oai.Paths.Set(in.Path, path) return nil } diff --git a/protocol/goai/goai_paths.go b/protocol/goai/goai_paths.go new file mode 100644 index 000000000..63b71fa34 --- /dev/null +++ b/protocol/goai/goai_paths.go @@ -0,0 +1,52 @@ +// 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 + +import ( + "github.com/gogf/gf/v2/container/gmap" +) + +// Paths are specified by OpenAPI/Swagger standard version 3.0. +type Paths struct { + paths *gmap.ListMap // map[string]Path +} + +func (p *Paths) init() { + if p.paths == nil { + p.paths = gmap.NewListMap() + } +} + +func (p *Paths) Get(name string) *Path { + p.init() + value := p.paths.Get(name) + if value != nil { + path := value.(Path) + return &path + } + return nil +} + +func (p *Paths) Set(name string, path Path) { + p.init() + p.paths.Set(name, path) +} + +func (p *Paths) Map() map[string]Path { + p.init() + m := make(map[string]Path) + p.paths.Iterator(func(key, value interface{}) bool { + m[key.(string)] = value.(Path) + return true + }) + return m +} + +func (p Paths) MarshalJSON() ([]byte, error) { + p.init() + return p.paths.MarshalJSON() +} diff --git a/protocol/goai/goai_shemas.go b/protocol/goai/goai_shemas.go index c25b8ce00..47fa348fa 100644 --- a/protocol/goai/goai_shemas.go +++ b/protocol/goai/goai_shemas.go @@ -11,7 +11,7 @@ import ( ) type Schemas struct { - refs *gmap.ListMap + refs *gmap.ListMap // map[string]SchemaRef } func createSchemas() Schemas { @@ -26,7 +26,7 @@ func (s *Schemas) init() { } } -func (s Schemas) Get(name string) *SchemaRef { +func (s *Schemas) Get(name string) *SchemaRef { s.init() value := s.refs.Get(name) if value != nil { @@ -36,12 +36,12 @@ func (s Schemas) Get(name string) *SchemaRef { return nil } -func (s Schemas) Set(name string, ref SchemaRef) { +func (s *Schemas) Set(name string, ref SchemaRef) { s.init() s.refs.Set(name, ref) } -func (s Schemas) Map() map[string]SchemaRef { +func (s *Schemas) Map() map[string]SchemaRef { s.init() m := make(map[string]SchemaRef) s.refs.Iterator(func(key, value interface{}) bool { @@ -51,7 +51,7 @@ func (s Schemas) Map() map[string]SchemaRef { return m } -func (s Schemas) Iterator(f func(key string, ref SchemaRef) bool) { +func (s *Schemas) Iterator(f func(key string, ref SchemaRef) bool) { s.init() s.refs.Iterator(func(key, value interface{}) bool { return f(key.(string), value.(SchemaRef)) diff --git a/protocol/goai/goai_z_unit_test.go b/protocol/goai/goai_z_unit_test.go index f18a77761..7b4b5986a 100644 --- a/protocol/goai/goai_z_unit_test.go +++ b/protocol/goai/goai_z_unit_test.go @@ -118,13 +118,13 @@ func TestOpenApiV3_Add(t *testing.T) { t.Assert(oai.Components.Schemas.Get(`github.com.gogf.gf.v2.protocol.goai_test.SetSpecInfo`).Value.Properties.Get(`Params`).Value.Type, goai.TypeArray) // Paths. - t.Assert(len(oai.Paths), 1) - t.AssertNE(oai.Paths[`/test1/{appId}`].Put, nil) - t.Assert(len(oai.Paths[`/test1/{appId}`].Put.Tags), 1) - t.Assert(len(oai.Paths[`/test1/{appId}`].Put.Parameters), 2) - t.AssertNE(oai.Paths[`/test1/{appId}`].Post, nil) - t.Assert(len(oai.Paths[`/test1/{appId}`].Post.Tags), 1) - t.Assert(len(oai.Paths[`/test1/{appId}`].Post.Parameters), 2) + t.Assert(len(oai.Paths.Map()), 1) + t.AssertNE(oai.Paths.Map()[`/test1/{appId}`].Put, nil) + t.Assert(len(oai.Paths.Map()[`/test1/{appId}`].Put.Tags), 1) + t.Assert(len(oai.Paths.Map()[`/test1/{appId}`].Put.Parameters), 2) + t.AssertNE(oai.Paths.Map()[`/test1/{appId}`].Post, nil) + t.Assert(len(oai.Paths.Map()[`/test1/{appId}`].Post.Tags), 1) + t.Assert(len(oai.Paths.Map()[`/test1/{appId}`].Post.Parameters), 2) }) } @@ -223,15 +223,15 @@ func TestOpenApiV3_Add_AutoDetectIn(t *testing.T) { fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 2) - t.Assert(len(oai.Paths), 1) - t.AssertNE(oai.Paths[path].Get, nil) - t.Assert(len(oai.Paths[path].Get.Parameters), 3) - t.Assert(oai.Paths[path].Get.Parameters[0].Value.Name, `Name`) - t.Assert(oai.Paths[path].Get.Parameters[0].Value.In, goai.ParameterInPath) - t.Assert(oai.Paths[path].Get.Parameters[1].Value.Name, `Product`) - t.Assert(oai.Paths[path].Get.Parameters[1].Value.In, goai.ParameterInPath) - t.Assert(oai.Paths[path].Get.Parameters[2].Value.Name, `Region`) - t.Assert(oai.Paths[path].Get.Parameters[2].Value.In, goai.ParameterInQuery) + t.Assert(len(oai.Paths.Map()), 1) + t.AssertNE(oai.Paths.Map()[path].Get, nil) + t.Assert(len(oai.Paths.Map()[path].Get.Parameters), 3) + t.Assert(oai.Paths.Map()[path].Get.Parameters[0].Value.Name, `Name`) + t.Assert(oai.Paths.Map()[path].Get.Parameters[0].Value.In, goai.ParameterInPath) + t.Assert(oai.Paths.Map()[path].Get.Parameters[1].Value.Name, `Product`) + t.Assert(oai.Paths.Map()[path].Get.Parameters[1].Value.In, goai.ParameterInPath) + t.Assert(oai.Paths.Map()[path].Get.Parameters[2].Value.Name, `Region`) + t.Assert(oai.Paths.Map()[path].Get.Parameters[2].Value.In, goai.ParameterInQuery) }) } @@ -275,8 +275,8 @@ func TestOpenApiV3_CommonRequest(t *testing.T) { t.AssertNil(err) // Schema asserts. t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3) }) } @@ -320,8 +320,8 @@ func TestOpenApiV3_CommonRequest_WithoutDataField_Setting(t *testing.T) { // Schema asserts. // fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 5) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 5) }) } @@ -360,8 +360,8 @@ func TestOpenApiV3_CommonRequest_EmptyRequest(t *testing.T) { // Schema asserts. // fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 3) }) } @@ -415,9 +415,9 @@ func TestOpenApiV3_CommonRequest_SubDataField(t *testing.T) { // Schema asserts. // fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 4) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 1) - t.Assert(len(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Get(`Request`).Value.Properties.Map()), 4) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Value.Properties.Get(`Request`).Value.Properties.Map()), 4) }) } @@ -460,13 +460,13 @@ func TestOpenApiV3_CommonResponse(t *testing.T) { }) t.AssertNil(err) - //g.Dump(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()) + //g.Dump(oai.Paths.Map()["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()) // Schema asserts. t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3) t.Assert( - oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Get("data").Value.Description, + oai.Paths.Map()["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Get("data").Value.Description, `Result data for certain request according API definition`, ) }) @@ -512,8 +512,8 @@ func TestOpenApiV3_CommonResponse_WithoutDataField_Setting(t *testing.T) { // Schema asserts. fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 8) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 8) }) } @@ -552,9 +552,9 @@ func TestOpenApiV3_CommonResponse_EmptyResponse(t *testing.T) { // Schema asserts. // fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(len(oai.Paths), 1) - t.Assert(oai.Paths["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Ref, `github.com.gogf.gf.v2.protocol.goai_test.Req`) - t.Assert(len(oai.Paths["/index"].Put.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(oai.Paths.Map()["/index"].Put.RequestBody.Value.Content["application/json"].Schema.Ref, `github.com.gogf.gf.v2.protocol.goai_test.Req`) + t.Assert(len(oai.Paths.Map()["/index"].Put.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 3) }) } @@ -608,9 +608,9 @@ func TestOpenApiV3_CommonResponse_SubDataField(t *testing.T) { // Schema asserts. // fmt.Println(oai.String()) t.Assert(len(oai.Components.Schemas.Map()), 4) - t.Assert(len(oai.Paths), 1) - t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 1) - t.Assert(len(oai.Paths["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Get(`Response`).Value.Properties.Map()), 7) + t.Assert(len(oai.Paths.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Map()), 1) + t.Assert(len(oai.Paths.Map()["/index"].Get.Responses["200"].Value.Content["application/json"].Schema.Value.Properties.Get(`Response`).Value.Properties.Map()), 7) }) } @@ -664,7 +664,7 @@ func TestOpenApiV3_ShortTags(t *testing.T) { // fmt.Println(oai.String()) // Schema asserts. t.Assert(len(oai.Components.Schemas.Map()), 3) - t.Assert(oai.Paths[`/test1/{appId}`].Summary, `CreateResourceReq sum`) + t.Assert(oai.Paths.Map()[`/test1/{appId}`].Summary, `CreateResourceReq sum`) t.Assert(oai. Components. Schemas.Get(`github.com.gogf.gf.v2.protocol.goai_test.CreateResourceReq`).