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
6 changes: 0 additions & 6 deletions pkg/generators/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,9 +1148,6 @@ func (g openAPITypeWriter) generateMapProperty(t *types.Type) error {

g.Do("Type: []string{\"object\"},\n", nil)
g.Do("AdditionalProperties: &spec.SchemaOrBool{\nAllows: true,\nSchema: &spec.Schema{\nSchemaProps: spec.SchemaProps{\n", nil)
if err := g.generateDefault(t.Elem.CommentLines, t.Elem, false, t.Elem); err != nil {
return err
}
typeString, format := openapi.OpenAPITypeFormat(elemType.String())
if typeString != "" {
g.generateSimpleProperty(typeString, format)
Expand Down Expand Up @@ -1185,9 +1182,6 @@ func (g openAPITypeWriter) generateSliceProperty(t *types.Type) error {
elemType := resolveAliasAndPtrType(t.Elem)
g.Do("Type: []string{\"array\"},\n", nil)
g.Do("Items: &spec.SchemaOrArray{\nSchema: &spec.Schema{\nSchemaProps: spec.SchemaProps{\n", nil)
if err := g.generateDefault(t.Elem.CommentLines, t.Elem, false, t.Elem); err != nil {
return err
}
typeString, format := openapi.OpenAPITypeFormat(elemType.String())
if typeString != "" {
g.generateSimpleProperty(typeString, format)
Expand Down
65 changes: 39 additions & 26 deletions pkg/generators/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand All @@ -364,7 +363,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -918,7 +916,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -987,7 +984,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: 0,
Type: []string{"integer"},
Format: "int32",
},
Expand Down Expand Up @@ -1056,7 +1052,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: false,
Type: []string{"boolean"},
Format: "",
},
Expand Down Expand Up @@ -1158,34 +1153,64 @@ func TestFailingDefaultEnforced(t *testing.T) {
}
}`,
expectedError: `failed to generate default in example.com/base/foo.Blah: Struct: invalid default value (map[string]interface {}{"foo":5}) for non-pointer/non-omitempty. If specified, must be: {}`,
}, {
}}

for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
e := packagestest.Export(t, x, []packagestest.Module{{
Name: "example.com/base/foo",
Files: map[string]interface{}{
"foo.go": test.definition,
},
}})
defer e.Cleanup()

_, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
if funcErr == nil {
t.Fatalf("An error was expected")
}
assertEqual(t, test.expectedError, funcErr.Error())
})
})
}
}

// TestSliceMapElementDefaultIgnored verifies that a +default annotation on a
// type used as a slice item or map value is silently ignored: the generator
// succeeds and produces a schema with no default on the items/value schema.
func TestSliceMapElementDefaultIgnored(t *testing.T) {
tests := []struct {
name string
definition string
}{{
name: "slice element default ignored",
definition: `
Comment thread
arthurbdiniz marked this conversation as resolved.
package foo

// +k8s:openapi-gen=true
type Blah struct {
List []Item

}

// +default="foo"
type Item string`,
expectedError: `failed to generate slice property in example.com/base/foo.Blah: List: invalid default value ("foo") for non-pointer/non-omitempty. If specified, must be: ""`,
}, {
name: "map element default ignored",
definition: `
package foo

// +k8s:openapi-gen=true
type Blah struct {
Map map[string]Item

}

// +default="foo"
type Item string`,
expectedError: `failed to generate map property in example.com/base/foo.Blah: Map: invalid default value ("foo") for non-pointer/non-omitempty. If specified, must be: ""`,
}}

for i, test := range tests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
e := packagestest.Export(t, x, []packagestest.Module{{
Name: "example.com/base/foo",
Expand All @@ -1196,10 +1221,9 @@ func TestFailingDefaultEnforced(t *testing.T) {
defer e.Cleanup()

_, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
if funcErr == nil {
t.Fatalf("An error was expected")
if funcErr != nil {
t.Fatalf("Unexpected error: %v", funcErr)
}
assertEqual(t, test.expectedError, funcErr.Error())
})
})
}
Expand Down Expand Up @@ -1589,7 +1613,6 @@ Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand All @@ -1605,7 +1628,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -1672,7 +1694,6 @@ Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: 0,
Type: []string{"integer"},
Format: "int64",
},
Expand Down Expand Up @@ -1750,7 +1771,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -1839,7 +1859,6 @@ Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand All @@ -1862,7 +1881,6 @@ Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -2141,7 +2159,6 @@ Type: []string{"array"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Enum: []interface{}{"a", "b"},
Expand All @@ -2157,7 +2174,6 @@ AdditionalProperties: &spec.SchemaOrBool{
Allows: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
Enum: []interface{}{"a", "b"},
Expand Down Expand Up @@ -2525,7 +2541,6 @@ func TestMarkerComments(t *testing.T) {
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -3104,7 +3119,6 @@ func TestNestedMarkers(t *testing.T) {
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down Expand Up @@ -3137,7 +3151,6 @@ func TestNestedMarkers(t *testing.T) {
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Default: "",
Type: []string{"string"},
Format: "",
},
Expand Down
48 changes: 19 additions & 29 deletions test/integration/pkg/generated/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading