@@ -33,6 +33,7 @@ import (
33
33
"k8s.io/apiserver/pkg/endpoints"
34
34
"k8s.io/apiserver/pkg/features"
35
35
utilfeature "k8s.io/apiserver/pkg/util/feature"
36
+ utilpointer "k8s.io/utils/pointer"
36
37
)
37
38
38
39
func TestNewBuilder (t * testing.T ) {
@@ -554,50 +555,72 @@ func schemaDiff(a, b *spec.Schema) string {
554
555
555
556
func TestBuildSwagger (t * testing.T ) {
556
557
tests := []struct {
557
- name string
558
- schema string
559
- wantedSchema string
560
- opts Options
558
+ name string
559
+ schema string
560
+ preserveUnknownFields * bool
561
+ wantedSchema string
562
+ opts Options
561
563
}{
562
564
{
563
565
"nil" ,
564
566
"" ,
567
+ nil ,
565
568
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}` ,
566
569
Options {V2 : true , StripDefaults : true },
567
570
},
568
571
{
569
572
"with properties" ,
570
573
`{"type":"object","properties":{"spec":{"type":"object"},"status":{"type":"object"}}}` ,
574
+ nil ,
571
575
`{"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"}]}` ,
572
576
Options {V2 : true , StripDefaults : true },
573
577
},
574
578
{
575
579
"with invalid-typed properties" ,
576
580
`{"type":"object","properties":{"spec":{"type":"bug"},"status":{"type":"object"}}}` ,
581
+ nil ,
582
+ `{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}` ,
583
+ Options {V2 : true , StripDefaults : true },
584
+ },
585
+ {
586
+ "with non-structural schema" ,
587
+ `{"type":"object","properties":{"foo":{"type":"array"}}}` ,
588
+ nil ,
589
+ `{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}` ,
590
+ Options {V2 : true , StripDefaults : true },
591
+ },
592
+ {
593
+ "with spec.preseveUnknownFields=true" ,
594
+ `{"type":"object","properties":{"foo":{"type":"string"}}}` ,
595
+ utilpointer .BoolPtr (true ),
577
596
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}` ,
578
597
Options {V2 : true , StripDefaults : true },
579
598
},
580
599
{
581
600
"with stripped defaults" ,
582
601
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}` ,
602
+ nil ,
583
603
`{"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"}]}` ,
584
604
Options {V2 : true , StripDefaults : true },
585
605
},
586
606
{
587
607
"with stripped defaults" ,
588
608
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}` ,
609
+ nil ,
589
610
`{"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"}]}` ,
590
611
Options {V2 : true , StripDefaults : true },
591
612
},
592
613
{
593
614
"v2" ,
594
615
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}` ,
616
+ nil ,
595
617
`{"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"}]}` ,
596
618
Options {V2 : true , StripDefaults : true },
597
619
},
598
620
{
599
621
"v3" ,
600
622
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}` ,
623
+ nil ,
601
624
`{"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"}]}` ,
602
625
Options {V2 : false , StripDefaults : true },
603
626
},
@@ -628,8 +651,9 @@ func TestBuildSwagger(t *testing.T) {
628
651
Kind : "Foo" ,
629
652
ListKind : "FooList" ,
630
653
},
631
- Scope : apiextensions .NamespaceScoped ,
632
- Validation : validation ,
654
+ Scope : apiextensions .NamespaceScoped ,
655
+ Validation : validation ,
656
+ PreserveUnknownFields : tt .preserveUnknownFields ,
633
657
},
634
658
}, "v1" , tt .opts )
635
659
if err != nil {
0 commit comments