Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions pkg/codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,49 @@ type GetTestByNameResponse struct {
checkLint(t, "test.gen.go", []byte(code))
}

// Validate any types nested under AdditionalPropertiesTypes is propagated up as with normal Property AdditionalTypes
func TestExampleOpenAPICodeGenerationSchemaAdditionalPropertyTypes(t *testing.T) {

// Input vars for code generation:
packageName := "testswagger"
opts := Configuration{
PackageName: packageName,
Generate: GenerateOptions{
EchoServer: false,
Client: false,
Models: true,
EmbeddedSpec: false,
},
}

loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

// Get a spec from the test definition in this file:
swagger, err := loader.LoadFromData([]byte(testSchemaArrayTypes))
assert.NoError(t, err)

// Run our code generation:
code, err := Generate(swagger, opts)
assert.NoError(t, err)
assert.NotEmpty(t, code)

// Check that we have valid (formattable) code:
_, err = format.Source([]byte(code))
assert.NoError(t, err)

// Check that we have a package:
assert.Contains(t, code, "package testswagger")

// Check that AdditionalPropertyTypes structs are propagated up properly:
assert.Contains(t, code, "[]PatchedBulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item")
assert.Contains(t, code, "type PatchedBulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item struct {")
assert.Contains(t, code, "[]BulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item")
assert.Contains(t, code, "type BulkWritableCircuitTerminationRequest_Relationships_Destination_Objects_Item struct {")
assert.Contains(t, code, "[]WritableCircuitTerminationRequest_Relationships_Destination_Objects_Item ")
assert.Contains(t, code, "type WritableCircuitTerminationRequest_Relationships_Destination_Objects_Item struct {")
}

func TestGoTypeImport(t *testing.T) {
packageName := "api"
opts := Configuration{
Expand Down Expand Up @@ -309,5 +352,8 @@ func (t *ExampleSchema_Item) FromExternalRef0NewPet(v externalRef0.NewPet) error

}

//go:embed test_schema_additional_properties_types.yaml
var testSchemaArrayTypes string

//go:embed test_spec.yaml
var testOpenAPIDefinition string
5 changes: 5 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (s Schema) GetAdditionalTypeDefs() []TypeDefinition {
for _, p := range s.Properties {
result = append(result, p.Schema.GetAdditionalTypeDefs()...)
}
// Some schema definitions may be used for key/value and not specify any properties, but provide properties
// within the additionalProperties. See test_schema_array_types.yaml in tests.
if s.AdditionalPropertiesType != nil {
result = append(result, s.AdditionalPropertiesType.GetAdditionalTypeDefs()...)
}
result = append(result, s.AdditionalTypes...)
return result
}
Expand Down
Loading