Skip to content

Commit 98dfe03

Browse files
mromaszewiczclaude
andcommitted
test: add Pattern I for inline response with x-go-type $ref properties
Add test case from oapi-codegen-exp#14: an inline response object whose properties $ref component schemas with x-go-type: string. In the experimental rewrite (libopenapi), this caused duplicate type declarations because libopenapi copies extensions from $ref targets. V2 (kin-openapi) handles this correctly, but the test guards against future regressions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 434e1f6 commit 98dfe03

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

internal/test/name_conflict_resolution/name_conflict_resolution.gen.go

Lines changed: 114 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/test/name_conflict_resolution/name_conflict_resolution_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,28 @@ func TestExtGoTypeWithCollisionResolver(t *testing.T) {
291291
assert.Equal(t, "response-result", *wrapper.JSON200.Result)
292292
}
293293

294+
// TestInlineResponseWithRefProperties verifies Pattern I (oapi-codegen-exp#14):
295+
// when a response has an inline object whose properties contain $refs to component
296+
// schemas with x-go-type, the property-level refs must NOT produce duplicate type
297+
// declarations. The component schemas keep their type aliases (Widget = string,
298+
// Metadata = string), and the inline response object gets its own struct type.
299+
//
300+
// Covers: oapi-codegen-exp#14
301+
func TestInlineResponseWithRefProperties(t *testing.T) {
302+
// Component schemas with x-go-type: string produce type aliases
303+
var widget Widget = "widget-value"
304+
assert.Equal(t, "widget-value", string(widget))
305+
306+
var metadata Metadata = "metadata-value"
307+
assert.Equal(t, "metadata-value", string(metadata))
308+
309+
// The inline response object should have fields typed by the component aliases.
310+
// The client wrapper for listEntities should exist and have a JSON200 field
311+
// pointing to the inline response type.
312+
var wrapper ListEntitiesResponse
313+
assert.Nil(t, wrapper.JSON200)
314+
}
315+
294316
func ptr[T any](v T) *T {
295317
return &v
296318
}

internal/test/name_conflict_resolution/spec.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ info:
55
description: |
66
Exercises all documented name collision patterns across issues and PRs:
77
#200, #254, #255, #292, #407, #899, #1357, #1450, #1474, #1713, #1881, #2097, #2213
8+
Also covers oapi-codegen-exp#14 (inline response object with $ref properties).
89
version: 0.0.0
910

1011
paths:
@@ -149,6 +150,28 @@ paths:
149150
schema:
150151
$ref: '#/components/schemas/Order'
151152

153+
# Pattern I: Inline response object with $ref properties to x-go-type schemas
154+
# (oapi-codegen-exp#14). The response has an inline object with properties that
155+
# $ref component schemas carrying x-go-type. Each property ref should use the
156+
# component schema's type alias, not produce duplicate type declarations.
157+
/entities:
158+
get:
159+
operationId: listEntities
160+
responses:
161+
'200':
162+
description: OK
163+
content:
164+
application/json:
165+
schema:
166+
type: object
167+
properties:
168+
data:
169+
type: array
170+
items:
171+
$ref: '#/components/schemas/Widget'
172+
metadata:
173+
$ref: '#/components/schemas/Metadata'
174+
152175
# Cross-section: requestBody vs schema (issues #254, #407)
153176
# "Pet" appears in both schemas and requestBodies.
154177
/pets:
@@ -223,6 +246,22 @@ components:
223246
name:
224247
type: string
225248

249+
# Pattern I: schemas with x-go-type used as $ref targets in inline response properties.
250+
# (oapi-codegen-exp#14)
251+
Widget:
252+
type: object
253+
x-go-type: string
254+
properties:
255+
id:
256+
type: string
257+
258+
Metadata:
259+
type: object
260+
x-go-type: string
261+
properties:
262+
total:
263+
type: integer
264+
226265
# Pattern F: x-go-type-name extension + cross-section collision
227266
# Schema "Qux" has x-go-type-name: CustomQux and collides with response "Qux".
228267
Qux:

0 commit comments

Comments
 (0)