Skip to content

Commit fca3f0f

Browse files
mromaszewiczclaude
andauthored
Fix schema gathering oversight (oapi-codegen#2219)
Response and RequestBodies need to include any additional types into model generation. This is an ancient bug, from the very first days of oapi-codegen. Extend the issue-200 spec with inline objects containing additionalProperties inside components/responses and components/requestBodies. The test confirms the codegen produces named types (Bar_Pagination, Bar_Metadata) for these inline schemas. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1650807 commit fca3f0f

File tree

4 files changed

+285
-6
lines changed

4 files changed

+285
-6
lines changed

internal/test/issues/issue-200/issue200.gen.go

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

internal/test/issues/issue-200/issue200_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ func TestDuplicateTypeNamesCompile(t *testing.T) {
3535
// RequestBody type: BarRequestBody (was "Bar" in components/requestBodies)
3636
_ = BarRequestBody{Value: ptr(42)}
3737

38+
// Inline nested object with additionalProperties inside a response
39+
// must produce a named AdditionalType (not get silently dropped).
40+
_ = Bar_Pagination{
41+
Page: ptr(1),
42+
TotalPages: ptr(10),
43+
AdditionalProperties: map[string]string{"cursor": "abc"},
44+
}
45+
46+
// Inline nested object with additionalProperties inside a requestBody
47+
// must produce a named AdditionalType (not get silently dropped).
48+
_ = Bar_Metadata{
49+
Key: ptr("k"),
50+
AdditionalProperties: map[string]string{"extra": "val"},
51+
}
52+
3853
// Operation-derived types
3954
_ = PostFooParams{Bar: &Bar{}}
4055
_ = PostFooJSONBody{Value: ptr(99)}

internal/test/issues/issue-200/spec.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ components:
5858
properties:
5959
value:
6060
type: integer
61+
metadata:
62+
type: object
63+
properties:
64+
key:
65+
type: string
66+
additionalProperties:
67+
type: string
6168

6269
responses:
6370
Bar:
@@ -78,3 +85,12 @@ components:
7885
$ref: '#/components/schemas/BarParam'
7986
value4:
8087
$ref: '#/components/schemas/BarParam2'
88+
pagination:
89+
type: object
90+
properties:
91+
page:
92+
type: integer
93+
totalPages:
94+
type: integer
95+
additionalProperties:
96+
type: string

pkg/codegen/codegen.go

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

716716
types = append(types, typeDef)
717+
types = append(types, goType.AdditionalTypes...)
717718
}
718719
}
719720
return types, nil
@@ -762,6 +763,7 @@ func GenerateTypesForRequestBodies(t *template.Template, bodies map[string]*open
762763
typeDef.TypeName = SchemaNameToTypeName(refType)
763764
}
764765
types = append(types, typeDef)
766+
types = append(types, goType.AdditionalTypes...)
765767
}
766768
}
767769
return types, nil

0 commit comments

Comments
 (0)