Skip to content

Commit 6d9245a

Browse files
committed
fix: generic types now carry package prefix in schema names
shortenGenericName was building the container name from m[1] (raw type name, no package info), causing e.g. ResponseTodoList instead of ModelResponseTodoList. Fix by extracting the container portion from defaultDefName, which already has the package prefix applied and StripDefinitionNamePrefix run — making generic schema names consistent with non-generic structs.
1 parent 2df14d9 commit 6d9245a

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

examples/todo/docs/openapi.yaml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ paths:
1010
content:
1111
application/json:
1212
schema:
13-
$ref: '#/components/schemas/ResponseTodoList'
13+
$ref: '#/components/schemas/ModelResponseTodoList'
1414
description: OK
1515
summary: List all todos
1616
tags:
@@ -26,7 +26,7 @@ paths:
2626
content:
2727
application/json:
2828
schema:
29-
$ref: '#/components/schemas/ResponseTodo'
29+
$ref: '#/components/schemas/ModelResponseTodo'
3030
description: Created
3131
"400":
3232
content:
@@ -77,7 +77,7 @@ paths:
7777
content:
7878
application/json:
7979
schema:
80-
$ref: '#/components/schemas/ResponseTodo'
80+
$ref: '#/components/schemas/ModelResponseTodo'
8181
description: OK
8282
"404":
8383
content:
@@ -109,7 +109,7 @@ paths:
109109
content:
110110
application/json:
111111
schema:
112-
$ref: '#/components/schemas/ResponseTodo'
112+
$ref: '#/components/schemas/ModelResponseTodo'
113113
description: OK
114114
"400":
115115
content:
@@ -146,7 +146,7 @@ paths:
146146
content:
147147
application/json:
148148
schema:
149-
$ref: '#/components/schemas/ResponseTodo'
149+
$ref: '#/components/schemas/ModelResponseTodo'
150150
description: OK
151151
"404":
152152
content:
@@ -169,6 +169,23 @@ components:
169169
title:
170170
type: string
171171
type: object
172+
ModelResponseTodo:
173+
properties:
174+
data:
175+
$ref: '#/components/schemas/ModelTodo'
176+
message:
177+
type: string
178+
type: object
179+
ModelResponseTodoList:
180+
properties:
181+
data:
182+
items:
183+
$ref: '#/components/schemas/ModelTodo'
184+
nullable: true
185+
type: array
186+
message:
187+
type: string
188+
type: object
172189
ModelTodo:
173190
properties:
174191
created_at:
@@ -196,20 +213,3 @@ components:
196213
title:
197214
type: string
198215
type: object
199-
ResponseTodo:
200-
properties:
201-
data:
202-
$ref: '#/components/schemas/ModelTodo'
203-
message:
204-
type: string
205-
type: object
206-
ResponseTodoList:
207-
properties:
208-
data:
209-
items:
210-
$ref: '#/components/schemas/ModelTodo'
211-
nullable: true
212-
type: array
213-
message:
214-
type: string
215-
type: object

examples/todo/go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ go 1.24.0
55
require (
66
github.com/gin-gonic/gin v1.10.0
77
github.com/oaswrap/gswag v0.0.0
8+
github.com/oaswrap/spec-ui v0.2.0
89
github.com/onsi/ginkgo/v2 v2.28.1
910
github.com/onsi/gomega v1.39.1
1011
modernc.org/sqlite v1.35.0
1112
)
1213

13-
require github.com/oaswrap/spec-ui v0.2.0
14-
1514
require (
1615
github.com/Masterminds/semver/v3 v3.4.0 // indirect
1716
github.com/bytedance/sonic v1.11.6 // indirect

spec_collector.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ func shortenGenericName(t reflect.Type, defaultDefName string) string {
6262
if m == nil {
6363
return defaultDefName
6464
}
65+
// Use the container name from defaultDefName, which already has the package
66+
// prefix applied and StripDefinitionNamePrefix already run — so the result
67+
// is consistent with how non-generic struct names are generated.
68+
containerName := m[1]
69+
if before, _, found := strings.Cut(defaultDefName, "["); found {
70+
containerName = before
71+
}
6572
args := strings.Split(m[2], ", ")
66-
result := m[1]
73+
result := containerName
6774
var sb strings.Builder
6875
for _, arg := range args {
6976
arg = strings.TrimPrefix(arg, "*")

0 commit comments

Comments
 (0)