@@ -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,65 @@ 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 spec.preseveUnknownFields=true" ,
587
+ `{"type":"object","properties":{"foo":{"type":"string"}}}` ,
588
+ utilpointer .BoolPtr (true ),
577
589
`{"type":"object","x-kubernetes-group-version-kind":[{"group":"bar.k8s.io","kind":"Foo","version":"v1"}]}` ,
578
590
Options {V2 : true , StripDefaults : true },
579
591
},
580
592
{
581
593
"with stripped defaults" ,
582
594
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}` ,
595
+ nil ,
583
596
`{"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
597
Options {V2 : true , StripDefaults : true },
585
598
},
586
599
{
587
600
"with stripped defaults" ,
588
601
`{"type":"object","properties":{"foo":{"type":"string","default":"bar"}}}` ,
602
+ nil ,
589
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"}]}` ,
590
604
Options {V2 : true , StripDefaults : true },
591
605
},
592
606
{
593
607
"v2" ,
594
608
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}` ,
609
+ nil ,
595
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"}]}` ,
596
611
Options {V2 : true , StripDefaults : true },
597
612
},
598
613
{
599
614
"v3" ,
600
615
`{"type":"object","properties":{"foo":{"type":"string","oneOf":[{"pattern":"a"},{"pattern":"b"}]}}}` ,
616
+ nil ,
601
617
`{"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
618
Options {V2 : false , StripDefaults : true },
603
619
},
@@ -628,8 +644,9 @@ func TestBuildSwagger(t *testing.T) {
628
644
Kind : "Foo" ,
629
645
ListKind : "FooList" ,
630
646
},
631
- Scope : apiextensions .NamespaceScoped ,
632
- Validation : validation ,
647
+ Scope : apiextensions .NamespaceScoped ,
648
+ Validation : validation ,
649
+ PreserveUnknownFields : tt .preserveUnknownFields ,
633
650
},
634
651
}, "v1" , tt .opts )
635
652
if err != nil {
0 commit comments