mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
openapi generating feature
This commit is contained in:
@ -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(), "*")
|
||||
}
|
||||
|
||||
@ -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{},
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user