From 6a289e50e9d7b48e3041fb3b3527801be588d555 Mon Sep 17 00:00:00 2001 From: zc Date: Thu, 14 Mar 2024 14:34:00 +0800 Subject: [PATCH 1/2] fix make schema name to support generics --- util.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/util.go b/util.go index 8b7ef8f..c3419dd 100644 --- a/util.go +++ b/util.go @@ -16,7 +16,6 @@ package swag import ( "fmt" - "path" "reflect" "regexp" "strconv" @@ -68,10 +67,11 @@ func makeName(t reflect.Type) string { ptr := reflect2.PtrOf(t) name = "ptr" + strconv.FormatUint(uint64(uintptr(ptr)), 10) } - pkgPath := path.Base(t.PkgPath()) + pkgPath := t.PkgPath() if pkgPath != "." { pkgPath += "." } fullName := pkgPath + name - return strings.Replace(fullName, "-", "_", -1) + fullName = strings.ReplaceAll(fullName, "/", "_") + return strings.ReplaceAll(fullName, "-", "_") } From 8034a078a73bf2a3b059a5b25e8b17207919a904 Mon Sep 17 00:00:00 2001 From: zc Date: Thu, 14 Mar 2024 14:54:22 +0800 Subject: [PATCH 2/2] fix units --- endpoint/endpoint_test.go | 6 +++--- reflect_test.go | 6 +++--- testdata/pet.json | 12 ++++++------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/endpoint/endpoint_test.go b/endpoint/endpoint_test.go index 17d6416..d1f5aaf 100644 --- a/endpoint/endpoint_test.go +++ b/endpoint/endpoint_test.go @@ -212,7 +212,7 @@ func TestBodyR(t *testing.T) { Name: "body", Required: true, Schema: &swag.Schema{ - Ref: "#/definitions/endpoint.Model", + Ref: "#/definitions/github.com_zc2638_swag_endpoint.Model", Prototype: reflect.TypeOf(Model{}), }, } @@ -234,7 +234,7 @@ func TestBody(t *testing.T) { Description: "the description", Required: true, Schema: &swag.Schema{ - Ref: "#/definitions/endpoint.Model", + Ref: "#/definitions/github.com_zc2638_swag_endpoint.Model", Prototype: reflect.TypeOf(Model{}), }, } @@ -253,7 +253,7 @@ func TestResponse(t *testing.T) { expected := swag.Response{ Description: "successful", Schema: &swag.Schema{ - Ref: "#/definitions/endpoint.Model", + Ref: "#/definitions/github.com_zc2638_swag_endpoint.Model", Prototype: Model{}, }, } diff --git a/reflect_test.go b/reflect_test.go index 92d50a2..58d4876 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -58,7 +58,7 @@ type Empty struct { func TestDefine(t *testing.T) { v := define(Pet{}) - obj, ok := v["swag.Pet"] + obj, ok := v["github.com_zc2638_swag.Pet"] assert.True(t, ok) assert.False(t, obj.IsArray) assert.Equal(t, 17, len(obj.Properties)) @@ -68,7 +68,7 @@ func TestDefine(t *testing.T) { assert.Nil(t, err) err = json.NewDecoder(bytes.NewReader(data)).Decode(&content) assert.Nil(t, err) - expected := content["swag.Pet"] + expected := content["github.com_zc2638_swag.Pet"] assert.Equal(t, expected.IsArray, obj.IsArray, "expected IsArray to match") assert.Equal(t, expected.Type, obj.Type, "expected Type to match") @@ -130,7 +130,7 @@ func TestNotStructDefine(t *testing.T) { func TestHonorJsonIgnore(t *testing.T) { v := define(Empty{}) - obj, ok := v["swag.Empty"] + obj, ok := v["github.com_zc2638_swag.Empty"] assert.True(t, ok) assert.False(t, obj.IsArray) assert.Equal(t, 0, len(obj.Properties), "expected zero exposed properties") diff --git a/testdata/pet.json b/testdata/pet.json index 2fec270..9fba898 100644 --- a/testdata/pet.json +++ b/testdata/pet.json @@ -1,5 +1,5 @@ { - "swag.Person": { + "github.com_zc2638_swag.Person": { "type": "object", "properties": { "First": { @@ -15,7 +15,7 @@ } } }, - "swag.Pet": { + "github.com_zc2638_swag.Pet": { "type": "object", "required": [ "pointer" @@ -85,23 +85,23 @@ "example": "b" }, "friend": { - "$ref": "#/definitions/swag.Person", + "$ref": "#/definitions/github.com_zc2638_swag.Person", "description": "description short expression" }, "friends": { "type": "array", "description": "long desc", "items": { - "$ref": "#/definitions/swag.Person" + "$ref": "#/definitions/github.com_zc2638_swag.Person" } }, "pointer": { - "$ref": "#/definitions/swag.Person" + "$ref": "#/definitions/github.com_zc2638_swag.Person" }, "pointers": { "type": "array", "items": { - "$ref": "#/definitions/swag.Person" + "$ref": "#/definitions/github.com_zc2638_swag.Person" } } }