diff --git a/protocol/goai/goai.go b/protocol/goai/goai.go index f4232676b..d52723946 100644 --- a/protocol/goai/goai.go +++ b/protocol/goai/goai.go @@ -16,6 +16,7 @@ import ( "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/internal/json" + "github.com/gogf/gf/text/gstr" "reflect" ) @@ -141,3 +142,7 @@ func (oai OpenApiV3) String() string { func formatRefToBytes(ref string) []byte { return []byte(fmt.Sprintf(`{"$ref":"#/components/schemas/%s"}`, ref)) } + +func golangTypeToSchemaName(t reflect.Type) string { + return gstr.TrimLeft(t.String(), "*") +} diff --git a/protocol/goai/goai_path.go b/protocol/goai/goai_path.go index 5bd68b9ac..7e3798ffa 100644 --- a/protocol/goai/goai_path.go +++ b/protocol/goai/goai_path.go @@ -88,8 +88,8 @@ func (oai *OpenApiV3) addPath(in addPathInput) error { outputMetaMap = gmeta.Data(outputObject.Interface()) isInputStructEmpty = oai.structHasNoFields(inputObject.Interface()) isOutputStructEmpty = oai.structHasNoFields(outputObject.Interface()) - inputStructTypeName = gstr.SubStrFromREx(inputObject.Type().String(), ".") - outputStructTypeName = gstr.SubStrFromREx(outputObject.Type().String(), ".") + inputStructTypeName = golangTypeToSchemaName(inputObject.Type()) + outputStructTypeName = golangTypeToSchemaName(outputObject.Type()) operation = Operation{ Responses: map[string]ResponseRef{}, } diff --git a/protocol/goai/goai_shema.go b/protocol/goai/goai_shema.go index 927d6c60a..6d2d61aef 100644 --- a/protocol/goai/goai_shema.go +++ b/protocol/goai/goai_shema.go @@ -76,7 +76,7 @@ func (oai *OpenApiV3) doAddSchemaSingle(object interface{}) error { var ( reflectType = reflect.TypeOf(object) - structTypeName = gstr.SubStrFromREx(reflectType.String(), ".") + structTypeName = golangTypeToSchemaName(reflectType) ) // Already added. diff --git a/protocol/goai/goai_shemaref.go b/protocol/goai/goai_shemaref.go index 1a2b1d346..38ecf5bd1 100644 --- a/protocol/goai/goai_shemaref.go +++ b/protocol/goai/goai_shemaref.go @@ -10,7 +10,6 @@ import ( "github.com/gogf/gf/errors/gcode" "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/internal/json" - "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/gconv" "reflect" ) @@ -56,7 +55,7 @@ func (oai *OpenApiV3) newSchemaRefWithGolangType(golangType reflect.Type, tagMap case TypeObject: var ( - structTypeName = gstr.SubStrFromREx(golangType.String(), ".") + structTypeName = golangTypeToSchemaName(golangType) ) // Specially for map type. if golangType.Kind() == reflect.Map { diff --git a/protocol/goai/goai_test.go b/protocol/goai/goai_test.go index 4f106b0c2..f143cbe13 100644 --- a/protocol/goai/goai_test.go +++ b/protocol/goai/goai_test.go @@ -47,13 +47,13 @@ func Test_Basic(t *testing.T) { t.AssertNil(err) // Schema asserts. t.Assert(len(oai.Components.Schemas), 2) - t.Assert(oai.Components.Schemas[`CreateResourceReq`].Value.Type, goai.TypeObject) - t.Assert(len(oai.Components.Schemas[`CreateResourceReq`].Value.Properties), 7) - t.Assert(oai.Components.Schemas[`CreateResourceReq`].Value.Properties[`appId`].Value.Type, goai.TypeNumber) - t.Assert(oai.Components.Schemas[`CreateResourceReq`].Value.Properties[`resourceId`].Value.Type, goai.TypeString) + t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Type, goai.TypeObject) + t.Assert(len(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties), 7) + t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`appId`].Value.Type, goai.TypeNumber) + t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`resourceId`].Value.Type, goai.TypeString) - t.Assert(len(oai.Components.Schemas[`SetSpecInfo`].Value.Properties), 3) - t.Assert(oai.Components.Schemas[`SetSpecInfo`].Value.Properties[`Params`].Value.Type, goai.TypeArray) + t.Assert(len(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties), 3) + t.Assert(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties[`Params`].Value.Type, goai.TypeArray) }) } @@ -107,13 +107,13 @@ func TestOpenApiV3_Add(t *testing.T) { //fmt.Println(oai.String()) // Schema asserts. t.Assert(len(oai.Components.Schemas), 3) - t.Assert(oai.Components.Schemas[`CreateResourceReq`].Value.Type, goai.TypeObject) - t.Assert(len(oai.Components.Schemas[`CreateResourceReq`].Value.Properties), 7) - t.Assert(oai.Components.Schemas[`CreateResourceReq`].Value.Properties[`appId`].Value.Type, goai.TypeNumber) - t.Assert(oai.Components.Schemas[`CreateResourceReq`].Value.Properties[`resourceId`].Value.Type, goai.TypeString) + t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Type, goai.TypeObject) + t.Assert(len(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties), 7) + t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`appId`].Value.Type, goai.TypeNumber) + t.Assert(oai.Components.Schemas[`goai_test.CreateResourceReq`].Value.Properties[`resourceId`].Value.Type, goai.TypeString) - t.Assert(len(oai.Components.Schemas[`SetSpecInfo`].Value.Properties), 3) - t.Assert(oai.Components.Schemas[`SetSpecInfo`].Value.Properties[`Params`].Value.Type, goai.TypeArray) + t.Assert(len(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties), 3) + t.Assert(oai.Components.Schemas[`goai_test.SetSpecInfo`].Value.Properties[`Params`].Value.Type, goai.TypeArray) // Paths. t.Assert(len(oai.Paths), 2) @@ -157,8 +157,8 @@ func TestOpenApiV3_Add_Recursive(t *testing.T) { t.AssertNil(err) // Schema asserts. t.Assert(len(oai.Components.Schemas), 3) - t.Assert(oai.Components.Schemas[`CategoryTreeItem`].Value.Type, goai.TypeObject) - t.Assert(len(oai.Components.Schemas[`CategoryTreeItem`].Value.Properties), 3) + t.Assert(oai.Components.Schemas[`goai_test.CategoryTreeItem`].Value.Type, goai.TypeObject) + t.Assert(len(oai.Components.Schemas[`goai_test.CategoryTreeItem`].Value.Properties), 3) }) }