Skip to content

Commit 8107da4

Browse files
committed
Fix schema gathering oversight
Response and RequestBodies need to include any additional types into model generation
1 parent 98dfe03 commit 8107da4

File tree

4 files changed

+238
-8
lines changed

4 files changed

+238
-8
lines changed

internal/test/name_conflict_resolution/name_conflict_resolution.gen.go

Lines changed: 197 additions & 8 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ func TestRequestBodyVsSchema(t *testing.T) {
213213
assert.Equal(t, "Fluffy", *petReqBody.Name)
214214
assert.Equal(t, "cat", *petReqBody.Species)
215215

216+
// Inline nested object with additionalProperties in the requestBody
217+
// must produce a named AdditionalType (not get silently dropped).
218+
reqBodyOwner := Pet_Owner{
219+
Name: ptr("Alice"),
220+
Phone: ptr("555-1234"),
221+
AdditionalProperties: map[string]string{"email": "alice@example.com"},
222+
}
223+
assert.Equal(t, "Alice", *reqBodyOwner.Name)
224+
assert.Equal(t, "alice@example.com", reqBodyOwner.AdditionalProperties["email"])
225+
216226
// CreatePet wrapper doesn't collide (unique name CreatePetResponse)
217227
var wrapper CreatePetResponse
218228
assert.Nil(t, wrapper.JSON200)
@@ -311,6 +321,17 @@ func TestInlineResponseWithRefProperties(t *testing.T) {
311321
// pointing to the inline response type.
312322
var wrapper ListEntitiesResponse
313323
assert.Nil(t, wrapper.JSON200)
324+
325+
// Inline nested object with additionalProperties in the response
326+
// must produce a named AdditionalType (not get silently dropped).
327+
pagination := ListEntities_200_Pagination{
328+
Page: ptr(1),
329+
TotalPages: ptr(10),
330+
AdditionalProperties: map[string]string{"cursor": "abc123"},
331+
}
332+
assert.Equal(t, 1, *pagination.Page)
333+
assert.Equal(t, 10, *pagination.TotalPages)
334+
assert.Equal(t, "abc123", pagination.AdditionalProperties["cursor"])
314335
}
315336

316337
func ptr[T any](v T) *T {

internal/test/name_conflict_resolution/spec.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ paths:
171171
$ref: '#/components/schemas/Widget'
172172
metadata:
173173
$ref: '#/components/schemas/Metadata'
174+
pagination:
175+
type: object
176+
properties:
177+
page:
178+
type: integer
179+
totalPages:
180+
type: integer
181+
additionalProperties:
182+
type: string
174183

175184
# Cross-section: requestBody vs schema (issues #254, #407)
176185
# "Pet" appears in both schemas and requestBodies.
@@ -338,6 +347,15 @@ components:
338347
type: string
339348
species:
340349
type: string
350+
owner:
351+
type: object
352+
properties:
353+
name:
354+
type: string
355+
phone:
356+
type: string
357+
additionalProperties:
358+
type: string
341359

342360
headers:
343361
Bar:

pkg/codegen/codegen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ func GenerateTypesForResponses(t *template.Template, responses openapi3.Response
733733
}
734734

735735
types = append(types, typeDef)
736+
types = append(types, goType.AdditionalTypes...)
736737
}
737738
}
738739
return types, nil
@@ -784,6 +785,7 @@ func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*open
784785
typeDef.TypeName = SchemaNameToTypeName(refType)
785786
}
786787
types = append(types, typeDef)
788+
types = append(types, goType.AdditionalTypes...)
787789
}
788790
}
789791
return types, nil

0 commit comments

Comments
 (0)