Skip to content

Commit

Permalink
fix make schema name to support generics (#41)
Browse files Browse the repository at this point in the history
* fix make schema name to support generics

* fix units
  • Loading branch information
zc2638 authored Mar 14, 2024
1 parent a206f70 commit c0db946
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions endpoint/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}),
},
}
Expand All @@ -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{}),
},
}
Expand All @@ -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{},
},
}
Expand Down
6 changes: 3 additions & 3 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 6 additions & 6 deletions testdata/pet.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"swag.Person": {
"github.com_zc2638_swag.Person": {
"type": "object",
"properties": {
"First": {
Expand All @@ -15,7 +15,7 @@
}
}
},
"swag.Pet": {
"github.com_zc2638_swag.Pet": {
"type": "object",
"required": [
"pointer"
Expand Down Expand Up @@ -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"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package swag

import (
"fmt"
"path"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -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, "-", "_")
}

0 comments on commit c0db946

Please sign in to comment.