Skip to content

Commit

Permalink
Remove StripDefaults from BuildSwagger
Browse files Browse the repository at this point in the history
Kubernetes-commit: 89ac376f6be2eb1b5ad9997997fe59745acb9f88
  • Loading branch information
apelisse authored and k8s-publishing-bot committed Nov 12, 2020
1 parent beebb14 commit 7d479a1
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 49 deletions.
2 changes: 1 addition & 1 deletion pkg/apiserver/customresource_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ func buildOpenAPIModelsForApply(staticOpenAPISpec *goopenapispec.Swagger, crd *a
specs := []*goopenapispec.Swagger{}
for _, v := range crd.Spec.Versions {
// Defaults are not pruned here, but before being served.
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripDefaults: false, StripValueValidation: true, StripNullable: true, AllowNonStructural: true})
s, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: false, StripValueValidation: true, StripNullable: true, AllowNonStructural: true})
if err != nil {
return nil, err
}
Expand Down
17 changes: 0 additions & 17 deletions pkg/apiserver/schema/skeleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,6 @@ limitations under the License.

package schema

// StripDefaults returns a copy without defaults.
func (s *Structural) StripDefaults() *Structural {
s = s.DeepCopy()
v := Visitor{
Structural: func(s *Structural) bool {
changed := false
if s.Default.Object != nil {
s.Default.Object = nil
changed = true
}
return changed
},
}
v.Visit(s)
return s
}

// StripValueValidations returns a copy without value validations.
func (s *Structural) StripValueValidations() *Structural {
s = s.DeepCopy()
Expand Down
6 changes: 0 additions & 6 deletions pkg/controller/openapi/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ type Options struct {
// Convert to OpenAPI v2.
V2 bool

// Strip defaults.
StripDefaults bool

// Strip value validation.
StripValueValidation bool

Expand Down Expand Up @@ -106,9 +103,6 @@ func BuildSwagger(crd *apiextensionsv1.CustomResourceDefinition, version string,
if opts.AllowNonStructural || len(structuralschema.ValidateStructural(nil, ss)) == 0 {
schema = ss

if opts.StripDefaults {
schema = schema.StripDefaults()
}
if opts.StripValueValidation {
schema = schema.StripValueValidations()
}
Expand Down
34 changes: 10 additions & 24 deletions pkg/controller/openapi/builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestNewBuilder(t *testing.T) {
{"with extensions",
`
{
"type":"object",
"type":"object",
"properties": {
"int-or-string-1": {
"x-kubernetes-int-or-string": true,
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestNewBuilder(t *testing.T) {
{"with extensions as v3 schema",
`
{
"type":"object",
"type":"object",
"properties": {
"int-or-string-1": {
"x-kubernetes-int-or-string": true,
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestCRDRouteParameterBuilder(t *testing.T) {
},
},
}
swagger, err := BuildSwagger(testNamespacedCRD, testCRDVersion, Options{V2: true, StripDefaults: true})
swagger, err := BuildSwagger(testNamespacedCRD, testCRDVersion, Options{V2: true})
require.NoError(t, err)
require.Equal(t, len(testCase.paths), len(swagger.Paths.Paths), testCase.scope)
for path, expected := range testCase.paths {
Expand Down Expand Up @@ -580,63 +580,49 @@ func TestBuildSwagger(t *testing.T) {
"",
nil,
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
Options{V2: true},
},
{
"with properties",
`{"type":"object","properties":{"spec":{"type":"object"},"status":{"type":"object"}}}`,
nil,
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"spec":{"type":"object"},"status":{"type":"object"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
Options{V2: true},
},
{
"with invalid-typed properties",
`{"type":"object","properties":{"spec":{"type":"bug"},"status":{"type":"object"}}}`,
nil,
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
Options{V2: true},
},
{
"with non-structural schema",
`{"type":"object","properties":{"foo":{"type":"array"}}}`,
nil,
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
Options{V2: true},
},
{
"with spec.preseveUnknownFields=true",
`{"type":"object","properties":{"foo":{"type":"string"}}}`,
utilpointer.BoolPtr(true),
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with stripped defaults",
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}`,
nil,
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
},
{
"with stripped defaults",
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}`,
nil,
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
Options{V2: true},
},
{
"v2",
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
nil,
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string"}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: true, StripDefaults: true},
Options{V2: true},
},
{
"v3",
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
nil,
`{"type":"object","properties":{"apiVersion":{"type":"string"},"kind":{"type":"string"},"metadata":{"$ref":"#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta"},"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}},"x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
Options{V2: false, StripDefaults: true},
Options{V2: false},
},
}
for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/openapi/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func buildVersionSpecs(crd *apiextensionsv1.CustomResourceDefinition, oldSpecs m
continue
}
// Defaults are not pruned here, but before being served.
spec, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: true, StripDefaults: false})
spec, err := builder.BuildSwagger(crd, v.Name, builder.Options{V2: true})
if err != nil {
return nil, false, err
}
Expand Down

0 comments on commit 7d479a1

Please sign in to comment.