Skip to content

Commit 0cdf909

Browse files
Updated code to find AdditionalTypes under AdditionalPropertiesType key as well. (oapi-codegen#1017)
1 parent e70addf commit 0cdf909

3 files changed

Lines changed: 564 additions & 0 deletions

File tree

pkg/codegen/codegen_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,49 @@ type GetTestByNameResponse struct {
201201
checkLint(t, "test.gen.go", []byte(code))
202202
}
203203

204+
// Validate any types nested under AdditionalPropertiesTypes is propagated up as with normal Property AdditionalTypes
205+
func TestExampleOpenAPICodeGenerationSchemaAdditionalPropertyTypes(t *testing.T) {
206+
207+
// Input vars for code generation:
208+
packageName := "testswagger"
209+
opts := Configuration{
210+
PackageName: packageName,
211+
Generate: GenerateOptions{
212+
EchoServer: false,
213+
Client: false,
214+
Models: true,
215+
EmbeddedSpec: false,
216+
},
217+
}
218+
219+
loader := openapi3.NewLoader()
220+
loader.IsExternalRefsAllowed = true
221+
222+
// Get a spec from the test definition in this file:
223+
swagger, err := loader.LoadFromData([]byte(testSchemaArrayTypes))
224+
assert.NoError(t, err)
225+
226+
// Run our code generation:
227+
code, err := Generate(swagger, opts)
228+
assert.NoError(t, err)
229+
assert.NotEmpty(t, code)
230+
231+
// Check that we have valid (formattable) code:
232+
_, err = format.Source([]byte(code))
233+
assert.NoError(t, err)
234+
235+
// Check that we have a package:
236+
assert.Contains(t, code, "package testswagger")
237+
238+
// Check that AdditionalPropertyTypes structs are propagated up properly:
239+
assert.Contains(t, code, "[]PatchedBulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item")
240+
assert.Contains(t, code, "type PatchedBulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item struct {")
241+
assert.Contains(t, code, "[]BulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item")
242+
assert.Contains(t, code, "type BulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item struct {")
243+
assert.Contains(t, code, "[]WritableCircuitTerminationRequest_Relationships_Destination_Objects_Item ")
244+
assert.Contains(t, code, "type WritableCircuitTerminationRequest_Relationships_Destination_Objects_Item struct {")
245+
}
246+
204247
func TestGoTypeImport(t *testing.T) {
205248
packageName := "api"
206249
opts := Configuration{
@@ -309,5 +352,8 @@ func (t *ExampleSchema_Item) FromExternalRef0NewPet(v externalRef0.NewPet) error
309352

310353
}
311354

355+
//go:embed test_schema_additional_properties_types.yaml
356+
var testSchemaArrayTypes string
357+
312358
//go:embed test_spec.yaml
313359
var testOpenAPIDefinition string

pkg/codegen/schema.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ func (s Schema) GetAdditionalTypeDefs() []TypeDefinition {
7171
for _, p := range s.Properties {
7272
result = append(result, p.Schema.GetAdditionalTypeDefs()...)
7373
}
74+
// Some schema definitions may be used for key/value and not specify any properties, but provide properties
75+
// within the additionalProperties. See test_schema_array_types.yaml in tests.
76+
if s.AdditionalPropertiesType != nil {
77+
result = append(result, s.AdditionalPropertiesType.GetAdditionalTypeDefs()...)
78+
}
7479
result = append(result, s.AdditionalTypes...)
7580
return result
7681
}

0 commit comments

Comments
 (0)