mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Remove StripDefaults from BuildSwagger
This commit is contained in:
parent
d3e641e84e
commit
89ac376f6b
@ -1321,7 +1321,7 @@ func buildOpenAPIModelsForApply(staticOpenAPISpec *goopenapispec.Swagger, crd *a
|
|||||||
specs := []*goopenapispec.Swagger{}
|
specs := []*goopenapispec.Swagger{}
|
||||||
for _, v := range crd.Spec.Versions {
|
for _, v := range crd.Spec.Versions {
|
||||||
// Defaults are not pruned here, but before being served.
|
// 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -16,23 +16,6 @@ limitations under the License.
|
|||||||
|
|
||||||
package schema
|
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.
|
// StripValueValidations returns a copy without value validations.
|
||||||
func (s *Structural) StripValueValidations() *Structural {
|
func (s *Structural) StripValueValidations() *Structural {
|
||||||
s = s.DeepCopy()
|
s = s.DeepCopy()
|
||||||
|
@ -74,9 +74,6 @@ type Options struct {
|
|||||||
// Convert to OpenAPI v2.
|
// Convert to OpenAPI v2.
|
||||||
V2 bool
|
V2 bool
|
||||||
|
|
||||||
// Strip defaults.
|
|
||||||
StripDefaults bool
|
|
||||||
|
|
||||||
// Strip value validation.
|
// Strip value validation.
|
||||||
StripValueValidation bool
|
StripValueValidation bool
|
||||||
|
|
||||||
@ -106,9 +103,6 @@ func BuildSwagger(crd *apiextensionsv1.CustomResourceDefinition, version string,
|
|||||||
if opts.AllowNonStructural || len(structuralschema.ValidateStructural(nil, ss)) == 0 {
|
if opts.AllowNonStructural || len(structuralschema.ValidateStructural(nil, ss)) == 0 {
|
||||||
schema = ss
|
schema = ss
|
||||||
|
|
||||||
if opts.StripDefaults {
|
|
||||||
schema = schema.StripDefaults()
|
|
||||||
}
|
|
||||||
if opts.StripValueValidation {
|
if opts.StripValueValidation {
|
||||||
schema = schema.StripValueValidations()
|
schema = schema.StripValueValidations()
|
||||||
}
|
}
|
||||||
|
@ -80,7 +80,7 @@ func TestNewBuilder(t *testing.T) {
|
|||||||
{"with extensions",
|
{"with extensions",
|
||||||
`
|
`
|
||||||
{
|
{
|
||||||
"type":"object",
|
"type":"object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"int-or-string-1": {
|
"int-or-string-1": {
|
||||||
"x-kubernetes-int-or-string": true,
|
"x-kubernetes-int-or-string": true,
|
||||||
@ -183,7 +183,7 @@ func TestNewBuilder(t *testing.T) {
|
|||||||
{"with extensions as v3 schema",
|
{"with extensions as v3 schema",
|
||||||
`
|
`
|
||||||
{
|
{
|
||||||
"type":"object",
|
"type":"object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"int-or-string-1": {
|
"int-or-string-1": {
|
||||||
"x-kubernetes-int-or-string": true,
|
"x-kubernetes-int-or-string": true,
|
||||||
@ -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.NoError(t, err)
|
||||||
require.Equal(t, len(testCase.paths), len(swagger.Paths.Paths), testCase.scope)
|
require.Equal(t, len(testCase.paths), len(swagger.Paths.Paths), testCase.scope)
|
||||||
for path, expected := range testCase.paths {
|
for path, expected := range testCase.paths {
|
||||||
@ -580,63 +580,49 @@ func TestBuildSwagger(t *testing.T) {
|
|||||||
"",
|
"",
|
||||||
nil,
|
nil,
|
||||||
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
|
`{"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",
|
"with properties",
|
||||||
`{"type":"object","properties":{"spec":{"type":"object"},"status":{"type":"object"}}}`,
|
`{"type":"object","properties":{"spec":{"type":"object"},"status":{"type":"object"}}}`,
|
||||||
nil,
|
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"}]}`,
|
`{"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",
|
"with invalid-typed properties",
|
||||||
`{"type":"object","properties":{"spec":{"type":"bug"},"status":{"type":"object"}}}`,
|
`{"type":"object","properties":{"spec":{"type":"bug"},"status":{"type":"object"}}}`,
|
||||||
nil,
|
nil,
|
||||||
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
|
`{"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",
|
"with non-structural schema",
|
||||||
`{"type":"object","properties":{"foo":{"type":"array"}}}`,
|
`{"type":"object","properties":{"foo":{"type":"array"}}}`,
|
||||||
nil,
|
nil,
|
||||||
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
|
`{"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",
|
"with spec.preseveUnknownFields=true",
|
||||||
`{"type":"object","properties":{"foo":{"type":"string"}}}`,
|
`{"type":"object","properties":{"foo":{"type":"string"}}}`,
|
||||||
utilpointer.BoolPtr(true),
|
utilpointer.BoolPtr(true),
|
||||||
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
|
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}`,
|
||||||
Options{V2: true, StripDefaults: true},
|
Options{V2: 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},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"v2",
|
"v2",
|
||||||
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
|
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
|
||||||
nil,
|
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"}]}`,
|
`{"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",
|
"v3",
|
||||||
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
|
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}`,
|
||||||
nil,
|
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"}]}`,
|
`{"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 {
|
for _, tt := range tests {
|
||||||
|
@ -203,7 +203,7 @@ func buildVersionSpecs(crd *apiextensionsv1.CustomResourceDefinition, oldSpecs m
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Defaults are not pruned here, but before being served.
|
// 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 {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user