Debian Codegraph @

debian/patches/0005-Remove-test-which-relies-on-packagestest.patch

raw
1 From: "Dr. Tobias Quathamer" <[email protected]>
2 Date: Sat, 14 Feb 2026 22:59:04 +0100
3 Subject: Remove test which relies on packagestest
4
5 The "packagestest" is deprecated and has been removed from
6 golang-golang-x-tools. The testfile below is removed to
7 fix the FTBFS with the latest upload of golang-golang-x-tools.
8 ---
9 pkg/generators/openapi_test.go | 3285 ----------------------------------------
10 1 file changed, 3285 deletions(-)
11 delete mode 100644 pkg/generators/openapi_test.go
12
13 diff --git a/pkg/generators/openapi_test.go b/pkg/generators/openapi_test.go
14 deleted file mode 100644
15 index c2c0647..0000000
16 --- a/pkg/generators/openapi_test.go
17 +++ /dev/null
18 @@ -1,3285 +0,0 @@
19 -/*
20 -Copyright 2016 The Kubernetes Authors.
21 -
22 -Licensed under the Apache License, Version 2.0 (the "License");
23 -you may not use this file except in compliance with the License.
24 -You may obtain a copy of the License at
25 -
26 - http://www.apache.org/licenses/LICENSE-2.0
27 -
28 -Unless required by applicable law or agreed to in writing, software
29 -distributed under the License is distributed on an "AS IS" BASIS,
30 -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 -See the License for the specific language governing permissions and
32 -limitations under the License.
33 -*/
34 -
35 -package generators
36 -
37 -import (
38 - "bytes"
39 - "fmt"
40 - "go/format"
41 - "path"
42 - "strings"
43 - "testing"
44 -
45 - "github.com/google/go-cmp/cmp"
46 - "golang.org/x/tools/go/packages"
47 - "golang.org/x/tools/go/packages/packagestest"
48 -
49 - "k8s.io/gengo/v2/generator"
50 - "k8s.io/gengo/v2/namer"
51 - "k8s.io/gengo/v2/parser"
52 - "k8s.io/gengo/v2/types"
53 -)
54 -
55 -func construct(t *testing.T, cfg *packages.Config, nameSystems namer.NameSystems, defaultSystem string, pkg string) *generator.Context {
56 - p := parser.New()
57 - if err := p.LoadPackagesWithConfigForTesting(cfg, pkg); err != nil {
58 - t.Fatalf("failed to load package: %v", err)
59 - }
60 - c, err := generator.NewContext(p, nameSystems, defaultSystem)
61 - if err != nil {
62 - t.Fatalf("failed to make a context: %v", err)
63 - }
64 - return c
65 -}
66 -
67 -func testOpenAPITypeWriter(t *testing.T, cfg *packages.Config) (error, error, *bytes.Buffer, *bytes.Buffer, []string) {
68 - pkgBase := "example.com/base"
69 - // `path` vs. `filepath` because packages use '/'
70 - inputPkg := path.Join(pkgBase, "foo")
71 - outputPkg := path.Join(pkgBase, "output")
72 - imports := generator.NewImportTrackerForPackage(outputPkg)
73 - rawNamer := namer.NewRawNamer(outputPkg, imports)
74 - namers := namer.NameSystems{
75 - "raw": rawNamer,
76 - "private": &namer.NameStrategy{
77 - Join: func(pre string, in []string, post string) string {
78 - return strings.Join(in, "_")
79 - },
80 - PrependPackageNames: 4, // enough to fully qualify from k8s.io/api/...
81 - },
82 - }
83 - context := construct(t, cfg, namers, "raw", inputPkg)
84 - universe := context.Universe
85 - blahT := universe.Type(types.Name{Package: inputPkg, Name: "Blah"})
86 -
87 - callBuffer := &bytes.Buffer{}
88 - callSW := generator.NewSnippetWriter(callBuffer, context, "$", "$")
89 - callError := newOpenAPITypeWriter(callSW, context).generateCall(blahT)
90 -
91 - funcBuffer := &bytes.Buffer{}
92 - funcSW := generator.NewSnippetWriter(funcBuffer, context, "$", "$")
93 - funcError := newOpenAPITypeWriter(funcSW, context).generate(blahT)
94 -
95 - return callError, funcError, callBuffer, funcBuffer, imports.ImportLines()
96 -}
97 -
98 -// NOTE: the usual order of arguments for an assertion would be want, got, but
99 -// this helper function flips that in favor of callsite readability.
100 -func assertEqual(t *testing.T, got, want string) {
101 - t.Helper()
102 - want = strings.TrimSpace(want)
103 - got = strings.TrimSpace(got)
104 - if !cmp.Equal(want, got) {
105 - t.Errorf("Wrong result:\n%s", cmp.Diff(want, got))
106 - }
107 -}
108 -
109 -func TestSimple(t *testing.T) {
110 - inputFile := `
111 - package foo
112 -
113 - // Blah is a test.
114 - // +k8s:openapi-gen=true
115 - // +k8s:openapi-gen=x-kubernetes-type-tag:type_test
116 - type Blah struct {
117 - // A simple string
118 - String string
119 - // A simple int
120 - Int int ` + "`" + `json:",omitempty"` + "`" + `
121 - // An int considered string simple int
122 - IntString int ` + "`" + `json:",string"` + "`" + `
123 - // A simple int64
124 - Int64 int64
125 - // A simple int32
126 - Int32 int32
127 - // A simple int16
128 - Int16 int16
129 - // A simple int8
130 - Int8 int8
131 - // A simple int
132 - Uint uint
133 - // A simple int64
134 - Uint64 uint64
135 - // A simple int32
136 - Uint32 uint32
137 - // A simple int16
138 - Uint16 uint16
139 - // A simple int8
140 - Uint8 uint8
141 - // A simple byte
142 - Byte byte
143 - // A simple boolean
144 - Bool bool
145 - // A simple float64
146 - Float64 float64
147 - // A simple float32
148 - Float32 float32
149 - // a base64 encoded characters
150 - ByteArray []byte
151 - // a member with an extension
152 - // +k8s:openapi-gen=x-kubernetes-member-tag:member_test
153 - WithExtension string
154 - // a member with struct tag as extension
155 - // +patchStrategy=merge
156 - // +patchMergeKey=pmk
157 - WithStructTagExtension string ` + "`" + `patchStrategy:"merge" patchMergeKey:"pmk"` + "`" + `
158 - // a member with a list type
159 - // +listType=atomic
160 - // +default=["foo", "bar"]
161 - WithListType []string
162 - // a member with a map type
163 - // +listType=atomic
164 - // +default={"foo": "bar", "fizz": "buzz"}
165 - Map map[string]string
166 - // a member with a string pointer
167 - // +default="foo"
168 - StringPointer *string
169 - // an int member with a default
170 - // +default=1
171 - OmittedInt int ` + "`" + `json:"omitted,omitempty"` + "`" + `
172 - // a field with an invalid escape sequence in comment
173 - // ex) regexp:^.*\.yaml$
174 - InvalidEscapeSequenceInComment string
175 - }`
176 -
177 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
178 - e := packagestest.Export(t, x, []packagestest.Module{{
179 - Name: "example.com/base/foo",
180 - Files: map[string]interface{}{
181 - "foo.go": inputFile,
182 - },
183 - }})
184 - defer e.Cleanup()
185 -
186 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
187 - if callErr != nil {
188 - t.Fatal(callErr)
189 - }
190 - if funcErr != nil {
191 - t.Fatal(funcErr)
192 - }
193 - assertEqual(t, callBuffer.String(),
194 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
195 -
196 - assertEqual(t, funcBuffer.String(),
197 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
198 -return common.OpenAPIDefinition{
199 -Schema: spec.Schema{
200 -SchemaProps: spec.SchemaProps{
201 -Description: "Blah is a test.",
202 -Type: []string{"object"},
203 -Properties: map[string]spec.Schema{
204 -"String": {
205 -SchemaProps: spec.SchemaProps{
206 -Description: "A simple string",
207 -Default: "",
208 -Type: []string{"string"},
209 -Format: "",
210 -},
211 -},
212 -"Int64": {
213 -SchemaProps: spec.SchemaProps{
214 -Description: "A simple int64",
215 -Default: 0,
216 -Type: []string{"integer"},
217 -Format: "int64",
218 -},
219 -},
220 -"Int32": {
221 -SchemaProps: spec.SchemaProps{
222 -Description: "A simple int32",
223 -Default: 0,
224 -Type: []string{"integer"},
225 -Format: "int32",
226 -},
227 -},
228 -"Int16": {
229 -SchemaProps: spec.SchemaProps{
230 -Description: "A simple int16",
231 -Default: 0,
232 -Type: []string{"integer"},
233 -Format: "int32",
234 -},
235 -},
236 -"Int8": {
237 -SchemaProps: spec.SchemaProps{
238 -Description: "A simple int8",
239 -Default: 0,
240 -Type: []string{"integer"},
241 -Format: "byte",
242 -},
243 -},
244 -"Uint": {
245 -SchemaProps: spec.SchemaProps{
246 -Description: "A simple int",
247 -Default: 0,
248 -Type: []string{"integer"},
249 -Format: "int32",
250 -},
251 -},
252 -"Uint64": {
253 -SchemaProps: spec.SchemaProps{
254 -Description: "A simple int64",
255 -Default: 0,
256 -Type: []string{"integer"},
257 -Format: "int64",
258 -},
259 -},
260 -"Uint32": {
261 -SchemaProps: spec.SchemaProps{
262 -Description: "A simple int32",
263 -Default: 0,
264 -Type: []string{"integer"},
265 -Format: "int64",
266 -},
267 -},
268 -"Uint16": {
269 -SchemaProps: spec.SchemaProps{
270 -Description: "A simple int16",
271 -Default: 0,
272 -Type: []string{"integer"},
273 -Format: "int32",
274 -},
275 -},
276 -"Uint8": {
277 -SchemaProps: spec.SchemaProps{
278 -Description: "A simple int8",
279 -Default: 0,
280 -Type: []string{"integer"},
281 -Format: "byte",
282 -},
283 -},
284 -"Byte": {
285 -SchemaProps: spec.SchemaProps{
286 -Description: "A simple byte",
287 -Default: 0,
288 -Type: []string{"integer"},
289 -Format: "byte",
290 -},
291 -},
292 -"Bool": {
293 -SchemaProps: spec.SchemaProps{
294 -Description: "A simple boolean",
295 -Default: false,
296 -Type: []string{"boolean"},
297 -Format: "",
298 -},
299 -},
300 -"Float64": {
301 -SchemaProps: spec.SchemaProps{
302 -Description: "A simple float64",
303 -Default: 0,
304 -Type: []string{"number"},
305 -Format: "double",
306 -},
307 -},
308 -"Float32": {
309 -SchemaProps: spec.SchemaProps{
310 -Description: "A simple float32",
311 -Default: 0,
312 -Type: []string{"number"},
313 -Format: "float",
314 -},
315 -},
316 -"ByteArray": {
317 -SchemaProps: spec.SchemaProps{
318 -Description: "a base64 encoded characters",
319 -Type: []string{"string"},
320 -Format: "byte",
321 -},
322 -},
323 -"WithExtension": {
324 -VendorExtensible: spec.VendorExtensible{
325 -Extensions: spec.Extensions{
326 -"x-kubernetes-member-tag": "member_test",
327 -},
328 -},
329 -SchemaProps: spec.SchemaProps{
330 -Description: "a member with an extension",
331 -Default: "",
332 -Type: []string{"string"},
333 -Format: "",
334 -},
335 -},
336 -"WithStructTagExtension": {
337 -VendorExtensible: spec.VendorExtensible{
338 -Extensions: spec.Extensions{
339 -"x-kubernetes-patch-merge-key": "pmk",
340 -"x-kubernetes-patch-strategy": "merge",
341 -},
342 -},
343 -SchemaProps: spec.SchemaProps{
344 -Description: "a member with struct tag as extension",
345 -Default: "",
346 -Type: []string{"string"},
347 -Format: "",
348 -},
349 -},
350 -"WithListType": {
351 -VendorExtensible: spec.VendorExtensible{
352 -Extensions: spec.Extensions{
353 -"x-kubernetes-list-type": "atomic",
354 -},
355 -},
356 -SchemaProps: spec.SchemaProps{
357 -Description: "a member with a list type",
358 -Default: []interface {}{"foo", "bar"},
359 -Type: []string{"array"},
360 -Items: &spec.SchemaOrArray{
361 -Schema: &spec.Schema{
362 -SchemaProps: spec.SchemaProps{
363 -Type: []string{"string"},
364 -Format: "",
365 -},
366 -},
367 -},
368 -},
369 -},
370 -"Map": {
371 -VendorExtensible: spec.VendorExtensible{
372 -Extensions: spec.Extensions{
373 -"x-kubernetes-list-type": "atomic",
374 -},
375 -},
376 -SchemaProps: spec.SchemaProps{
377 -Description: "a member with a map type",
378 -Default: map[string]interface {}{"fizz":"buzz", "foo":"bar"},
379 -Type: []string{"object"},
380 -AdditionalProperties: &spec.SchemaOrBool{
381 -Allows: true,
382 -Schema: &spec.Schema{
383 -SchemaProps: spec.SchemaProps{
384 -Type: []string{"string"},
385 -Format: "",
386 -},
387 -},
388 -},
389 -},
390 -},
391 -"StringPointer": {
392 -SchemaProps: spec.SchemaProps{
393 -Description: "a member with a string pointer",
394 -Default: "foo",
395 -Type: []string{"string"},
396 -Format: "",
397 -},
398 -},
399 -"omitted": {
400 -SchemaProps: spec.SchemaProps{
401 -Description: "an int member with a default",
402 -Default: 1,
403 -Type: []string{"integer"},
404 -Format: "int32",
405 -},
406 -},
407 -"InvalidEscapeSequenceInComment": {
408 -SchemaProps: spec.SchemaProps{
409 -Description: "a field with an invalid escape sequence in comment ex) regexp:^.*\\.yaml$",
410 -Default: "",
411 -Type: []string{"string"},
412 -Format: "",
413 -},
414 -},
415 -},
416 -Required: []string{"String","Int64","Int32","Int16","Int8","Uint","Uint64","Uint32","Uint16","Uint8","Byte","Bool","Float64","Float32","ByteArray","WithExtension","WithStructTagExtension","WithListType","Map","StringPointer","InvalidEscapeSequenceInComment"},
417 -},
418 -VendorExtensible: spec.VendorExtensible{
419 -Extensions: spec.Extensions{
420 -"x-kubernetes-type-tag": "type_test",
421 -},
422 -},
423 -},
424 -}
425 -}`)
426 - })
427 -}
428 -
429 -func TestEmptyProperties(t *testing.T) {
430 - inputFile := `
431 - package foo
432 -
433 - // Blah demonstrate a struct without fields.
434 - type Blah struct {
435 - }`
436 -
437 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
438 - e := packagestest.Export(t, x, []packagestest.Module{{
439 - Name: "example.com/base/foo",
440 - Files: map[string]interface{}{
441 - "foo.go": inputFile,
442 - },
443 - }})
444 - defer e.Cleanup()
445 -
446 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
447 - if callErr != nil {
448 - t.Fatal(callErr)
449 - }
450 - if funcErr != nil {
451 - t.Fatal(funcErr)
452 - }
453 - assertEqual(t, callBuffer.String(),
454 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
455 - assertEqual(t, funcBuffer.String(),
456 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
457 -return common.OpenAPIDefinition{
458 -Schema: spec.Schema{
459 -SchemaProps: spec.SchemaProps{
460 -Description: "Blah demonstrate a struct without fields.",
461 -Type: []string{"object"},
462 -},
463 -},
464 -}
465 -}`)
466 - })
467 -}
468 -
469 -func TestNestedStruct(t *testing.T) {
470 - inputFile := `
471 - package foo
472 -
473 - // Nested is used as struct field
474 - type Nested struct {
475 - // A simple string
476 - String string
477 - }
478 -
479 - // Blah demonstrate a struct with struct field.
480 - type Blah struct {
481 - // A struct field
482 - Field Nested
483 - }`
484 -
485 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
486 - e := packagestest.Export(t, x, []packagestest.Module{{
487 - Name: "example.com/base/foo",
488 - Files: map[string]interface{}{
489 - "foo.go": inputFile,
490 - },
491 - }})
492 - defer e.Cleanup()
493 -
494 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
495 - if callErr != nil {
496 - t.Fatal(callErr)
497 - }
498 - if funcErr != nil {
499 - t.Fatal(funcErr)
500 - }
501 - assertEqual(t, callBuffer.String(),
502 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
503 - assertEqual(t, funcBuffer.String(),
504 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
505 -return common.OpenAPIDefinition{
506 -Schema: spec.Schema{
507 -SchemaProps: spec.SchemaProps{
508 -Description: "Blah demonstrate a struct with struct field.",
509 -Type: []string{"object"},
510 -Properties: map[string]spec.Schema{
511 -"Field": {
512 -SchemaProps: spec.SchemaProps{
513 -Description: "A struct field",
514 -Default: map[string]interface {}{},
515 -Ref: ref("example.com/base/foo.Nested"),
516 -},
517 -},
518 -},
519 -Required: []string{"Field"},
520 -},
521 -},
522 -Dependencies: []string{
523 -"example.com/base/foo.Nested",},
524 -}
525 -}`)
526 - })
527 -}
528 -
529 -func TestNestedStructPointer(t *testing.T) {
530 - inputFile := `
531 - package foo
532 -
533 - // Nested is used as struct pointer field
534 - type Nested struct {
535 - // A simple string
536 - String string
537 - }
538 -
539 - // Blah demonstrate a struct with struct pointer field.
540 - type Blah struct {
541 - // A struct pointer field
542 - Field *Nested
543 - }`
544 -
545 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
546 - e := packagestest.Export(t, x, []packagestest.Module{{
547 - Name: "example.com/base/foo",
548 - Files: map[string]interface{}{
549 - "foo.go": inputFile,
550 - },
551 - }})
552 - defer e.Cleanup()
553 -
554 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
555 -
556 - if callErr != nil {
557 - t.Fatal(callErr)
558 - }
559 - if funcErr != nil {
560 - t.Fatal(funcErr)
561 - }
562 - assertEqual(t, callBuffer.String(),
563 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
564 - assertEqual(t, funcBuffer.String(),
565 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
566 -return common.OpenAPIDefinition{
567 -Schema: spec.Schema{
568 -SchemaProps: spec.SchemaProps{
569 -Description: "Blah demonstrate a struct with struct pointer field.",
570 -Type: []string{"object"},
571 -Properties: map[string]spec.Schema{
572 -"Field": {
573 -SchemaProps: spec.SchemaProps{
574 -Description: "A struct pointer field",
575 -Ref: ref("example.com/base/foo.Nested"),
576 -},
577 -},
578 -},
579 -Required: []string{"Field"},
580 -},
581 -},
582 -Dependencies: []string{
583 -"example.com/base/foo.Nested",},
584 -}
585 -}`)
586 - })
587 -}
588 -
589 -func TestEmbeddedStruct(t *testing.T) {
590 - inputFile := `
591 - package foo
592 -
593 - // Nested is used as embedded struct field
594 - type Nested struct {
595 - // A simple string
596 - String string
597 - }
598 -
599 - // Blah demonstrate a struct with embedded struct field.
600 - type Blah struct {
601 - // An embedded struct field
602 - Nested
603 - }`
604 -
605 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
606 - e := packagestest.Export(t, x, []packagestest.Module{{
607 - Name: "example.com/base/foo",
608 - Files: map[string]interface{}{
609 - "foo.go": inputFile,
610 - },
611 - }})
612 - defer e.Cleanup()
613 -
614 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
615 - if callErr != nil {
616 - t.Fatal(callErr)
617 - }
618 - if funcErr != nil {
619 - t.Fatal(funcErr)
620 - }
621 - assertEqual(t, callBuffer.String(),
622 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
623 - assertEqual(t, funcBuffer.String(),
624 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
625 -return common.OpenAPIDefinition{
626 -Schema: spec.Schema{
627 -SchemaProps: spec.SchemaProps{
628 -Description: "Blah demonstrate a struct with embedded struct field.",
629 -Type: []string{"object"},
630 -Properties: map[string]spec.Schema{
631 -"Nested": {
632 -SchemaProps: spec.SchemaProps{
633 -Description: "An embedded struct field",
634 -Default: map[string]interface {}{},
635 -Ref: ref("example.com/base/foo.Nested"),
636 -},
637 -},
638 -},
639 -Required: []string{"Nested"},
640 -},
641 -},
642 -Dependencies: []string{
643 -"example.com/base/foo.Nested",},
644 -}
645 -}`)
646 - })
647 -}
648 -
649 -func TestSingleEmbeddedStruct(t *testing.T) {
650 - inputFile := `
651 - package foo
652 -
653 - import "time"
654 -
655 - // Nested is used as embedded struct field
656 - type Nested struct {
657 - // A simple string
658 - time.Duration
659 - }
660 -
661 - // Blah demonstrate a struct with embedded struct field.
662 - type Blah struct {
663 - // An embedded struct field
664 - // +default="10ms"
665 - Nested ` + "`" + `json:"nested,omitempty" protobuf:"bytes,5,opt,name=nested"` + "`" + `
666 - }`
667 -
668 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
669 - e := packagestest.Export(t, x, []packagestest.Module{{
670 - Name: "example.com/base/foo",
671 - Files: map[string]interface{}{
672 - "foo.go": inputFile,
673 - },
674 - }})
675 - defer e.Cleanup()
676 -
677 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
678 - if callErr != nil {
679 - t.Fatal(callErr)
680 - }
681 - if funcErr != nil {
682 - t.Fatal(funcErr)
683 - }
684 - assertEqual(t, callBuffer.String(),
685 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
686 - assertEqual(t, funcBuffer.String(),
687 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
688 -return common.OpenAPIDefinition{
689 -Schema: spec.Schema{
690 -SchemaProps: spec.SchemaProps{
691 -Description: "Blah demonstrate a struct with embedded struct field.",
692 -Type: []string{"object"},
693 -Properties: map[string]spec.Schema{
694 -"nested": {
695 -SchemaProps: spec.SchemaProps{
696 -Description: "An embedded struct field",
697 -Default: "10ms",
698 -Ref: ref("example.com/base/foo.Nested"),
699 -},
700 -},
701 -},
702 -},
703 -},
704 -Dependencies: []string{
705 -"example.com/base/foo.Nested",},
706 -}
707 -}`)
708 - })
709 -}
710 -
711 -func TestEmbeddedInlineStruct(t *testing.T) {
712 - inputFile := `
713 - package foo
714 -
715 - // Nested is used as embedded inline struct field
716 - type Nested struct {
717 - // A simple string
718 - String string
719 - }
720 -
721 - // Blah demonstrate a struct with embedded inline struct field.
722 - type Blah struct {
723 - // An embedded inline struct field
724 - Nested ` + "`" + `json:",inline,omitempty"` + "`" + `
725 - }`
726 -
727 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
728 - e := packagestest.Export(t, x, []packagestest.Module{{
729 - Name: "example.com/base/foo",
730 - Files: map[string]interface{}{
731 - "foo.go": inputFile,
732 - },
733 - }})
734 - defer e.Cleanup()
735 -
736 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
737 - if callErr != nil {
738 - t.Fatal(callErr)
739 - }
740 - if funcErr != nil {
741 - t.Fatal(funcErr)
742 - }
743 - assertEqual(t, callBuffer.String(),
744 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
745 - assertEqual(t, funcBuffer.String(),
746 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
747 -return common.OpenAPIDefinition{
748 -Schema: spec.Schema{
749 -SchemaProps: spec.SchemaProps{
750 -Description: "Blah demonstrate a struct with embedded inline struct field.",
751 -Type: []string{"object"},
752 -Properties: map[string]spec.Schema{
753 -"String": {
754 -SchemaProps: spec.SchemaProps{
755 -Description: "A simple string",
756 -Default: "",
757 -Type: []string{"string"},
758 -Format: "",
759 -},
760 -},
761 -},
762 -Required: []string{"String"},
763 -},
764 -},
765 -}
766 -}`)
767 - })
768 -}
769 -
770 -func TestEmbeddedInlineStructPointer(t *testing.T) {
771 - inputFile := `
772 - package foo
773 -
774 - // Nested is used as embedded inline struct pointer field.
775 - type Nested struct {
776 - // A simple string
777 - String string
778 - }
779 -
780 - // Blah demonstrate a struct with embedded inline struct pointer field.
781 - type Blah struct {
782 - // An embedded inline struct pointer field
783 - *Nested ` + "`" + `json:",inline,omitempty"` + "`" + `
784 - }`
785 -
786 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
787 - e := packagestest.Export(t, x, []packagestest.Module{{
788 - Name: "example.com/base/foo",
789 - Files: map[string]interface{}{
790 - "foo.go": inputFile,
791 - },
792 - }})
793 - defer e.Cleanup()
794 -
795 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
796 - if callErr != nil {
797 - t.Fatal(callErr)
798 - }
799 - if funcErr != nil {
800 - t.Fatal(funcErr)
801 - }
802 - assertEqual(t, callBuffer.String(),
803 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
804 - assertEqual(t, funcBuffer.String(),
805 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
806 -return common.OpenAPIDefinition{
807 -Schema: spec.Schema{
808 -SchemaProps: spec.SchemaProps{
809 -Description: "Blah demonstrate a struct with embedded inline struct pointer field.",
810 -Type: []string{"object"},
811 -Properties: map[string]spec.Schema{
812 -"String": {
813 -SchemaProps: spec.SchemaProps{
814 -Description: "A simple string",
815 -Default: "",
816 -Type: []string{"string"},
817 -Format: "",
818 -},
819 -},
820 -},
821 -Required: []string{"String"},
822 -},
823 -},
824 -}
825 -}`)
826 - })
827 -}
828 -
829 -func TestEmbeddedInlineStructWithEmptyJSONTag(t *testing.T) {
830 - inputFile := `
831 - package foo
832 -
833 - // Nested is used as embedded inline struct field
834 - type Nested struct {
835 - // A simple string
836 - String string
837 - }
838 -
839 - // Blah demonstrate a struct with embedded inline struct field using empty json tag.
840 - type Blah struct {
841 - // An embedded inline struct field with empty json tag
842 - Nested ` + "`" + `json:""` + "`" + `
843 - }`
844 -
845 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
846 - e := packagestest.Export(t, x, []packagestest.Module{{
847 - Name: "example.com/base/foo",
848 - Files: map[string]interface{}{
849 - "foo.go": inputFile,
850 - },
851 - }})
852 - defer e.Cleanup()
853 -
854 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
855 - if callErr != nil {
856 - t.Fatal(callErr)
857 - }
858 - if funcErr != nil {
859 - t.Fatal(funcErr)
860 - }
861 - assertEqual(t, callBuffer.String(),
862 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
863 - assertEqual(t, funcBuffer.String(),
864 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
865 -return common.OpenAPIDefinition{
866 -Schema: spec.Schema{
867 -SchemaProps: spec.SchemaProps{
868 -Description: "Blah demonstrate a struct with embedded inline struct field using empty json tag.",
869 -Type: []string{"object"},
870 -Properties: map[string]spec.Schema{
871 -"String": {
872 -SchemaProps: spec.SchemaProps{
873 -Description: "A simple string",
874 -Default: "",
875 -Type: []string{"string"},
876 -Format: "",
877 -},
878 -},
879 -},
880 -Required: []string{"String"},
881 -},
882 -},
883 -}
884 -}`)
885 - })
886 -}
887 -
888 -func TestNestedMapString(t *testing.T) {
889 - inputFile := `
890 - package foo
891 -
892 - // Map sample tests openAPIGen.generateMapProperty method.
893 - type Blah struct {
894 - // A sample String to String map
895 - StringToArray map[string]map[string]string
896 - }`
897 -
898 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
899 - e := packagestest.Export(t, x, []packagestest.Module{{
900 - Name: "example.com/base/foo",
901 - Files: map[string]interface{}{
902 - "foo.go": inputFile,
903 - },
904 - }})
905 - defer e.Cleanup()
906 -
907 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
908 - if callErr != nil {
909 - t.Fatal(callErr)
910 - }
911 - if funcErr != nil {
912 - t.Fatal(funcErr)
913 - }
914 - assertEqual(t, callBuffer.String(),
915 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
916 - assertEqual(t, funcBuffer.String(),
917 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
918 -return common.OpenAPIDefinition{
919 -Schema: spec.Schema{
920 -SchemaProps: spec.SchemaProps{
921 -Description: "Map sample tests openAPIGen.generateMapProperty method.",
922 -Type: []string{"object"},
923 -Properties: map[string]spec.Schema{
924 -"StringToArray": {
925 -SchemaProps: spec.SchemaProps{
926 -Description: "A sample String to String map",
927 -Type: []string{"object"},
928 -AdditionalProperties: &spec.SchemaOrBool{
929 -Allows: true,
930 -Schema: &spec.Schema{
931 -SchemaProps: spec.SchemaProps{
932 -Type: []string{"object"},
933 -AdditionalProperties: &spec.SchemaOrBool{
934 -Allows: true,
935 -Schema: &spec.Schema{
936 -SchemaProps: spec.SchemaProps{
937 -Type: []string{"string"},
938 -Format: "",
939 -},
940 -},
941 -},
942 -},
943 -},
944 -},
945 -},
946 -},
947 -},
948 -Required: []string{"StringToArray"},
949 -},
950 -},
951 -}
952 -}`)
953 - })
954 -}
955 -
956 -func TestNestedMapInt(t *testing.T) {
957 - inputFile := `
958 - package foo
959 -
960 - // Map sample tests openAPIGen.generateMapProperty method.
961 - type Blah struct {
962 - // A sample String to String map
963 - StringToArray map[string]map[string]int
964 - }`
965 -
966 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
967 - e := packagestest.Export(t, x, []packagestest.Module{{
968 - Name: "example.com/base/foo",
969 - Files: map[string]interface{}{
970 - "foo.go": inputFile,
971 - },
972 - }})
973 - defer e.Cleanup()
974 -
975 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
976 - if callErr != nil {
977 - t.Fatal(callErr)
978 - }
979 - if funcErr != nil {
980 - t.Fatal(funcErr)
981 - }
982 - assertEqual(t, callBuffer.String(),
983 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
984 - assertEqual(t, funcBuffer.String(),
985 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
986 -return common.OpenAPIDefinition{
987 -Schema: spec.Schema{
988 -SchemaProps: spec.SchemaProps{
989 -Description: "Map sample tests openAPIGen.generateMapProperty method.",
990 -Type: []string{"object"},
991 -Properties: map[string]spec.Schema{
992 -"StringToArray": {
993 -SchemaProps: spec.SchemaProps{
994 -Description: "A sample String to String map",
995 -Type: []string{"object"},
996 -AdditionalProperties: &spec.SchemaOrBool{
997 -Allows: true,
998 -Schema: &spec.Schema{
999 -SchemaProps: spec.SchemaProps{
1000 -Type: []string{"object"},
1001 -AdditionalProperties: &spec.SchemaOrBool{
1002 -Allows: true,
1003 -Schema: &spec.Schema{
1004 -SchemaProps: spec.SchemaProps{
1005 -Type: []string{"integer"},
1006 -Format: "int32",
1007 -},
1008 -},
1009 -},
1010 -},
1011 -},
1012 -},
1013 -},
1014 -},
1015 -},
1016 -Required: []string{"StringToArray"},
1017 -},
1018 -},
1019 -}
1020 -}`)
1021 - })
1022 -}
1023 -
1024 -func TestNestedMapBoolean(t *testing.T) {
1025 - inputFile := `
1026 - package foo
1027 -
1028 - // Map sample tests openAPIGen.generateMapProperty method.
1029 - type Blah struct {
1030 - // A sample String to String map
1031 - StringToArray map[string]map[string]bool
1032 - }`
1033 -
1034 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1035 - e := packagestest.Export(t, x, []packagestest.Module{{
1036 - Name: "example.com/base/foo",
1037 - Files: map[string]interface{}{
1038 - "foo.go": inputFile,
1039 - },
1040 - }})
1041 - defer e.Cleanup()
1042 -
1043 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1044 - if callErr != nil {
1045 - t.Fatal(callErr)
1046 - }
1047 - if funcErr != nil {
1048 - t.Fatal(funcErr)
1049 - }
1050 - assertEqual(t, callBuffer.String(),
1051 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1052 - assertEqual(t, funcBuffer.String(),
1053 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1054 -return common.OpenAPIDefinition{
1055 -Schema: spec.Schema{
1056 -SchemaProps: spec.SchemaProps{
1057 -Description: "Map sample tests openAPIGen.generateMapProperty method.",
1058 -Type: []string{"object"},
1059 -Properties: map[string]spec.Schema{
1060 -"StringToArray": {
1061 -SchemaProps: spec.SchemaProps{
1062 -Description: "A sample String to String map",
1063 -Type: []string{"object"},
1064 -AdditionalProperties: &spec.SchemaOrBool{
1065 -Allows: true,
1066 -Schema: &spec.Schema{
1067 -SchemaProps: spec.SchemaProps{
1068 -Type: []string{"object"},
1069 -AdditionalProperties: &spec.SchemaOrBool{
1070 -Allows: true,
1071 -Schema: &spec.Schema{
1072 -SchemaProps: spec.SchemaProps{
1073 -Type: []string{"boolean"},
1074 -Format: "",
1075 -},
1076 -},
1077 -},
1078 -},
1079 -},
1080 -},
1081 -},
1082 -},
1083 -},
1084 -Required: []string{"StringToArray"},
1085 -},
1086 -},
1087 -}
1088 -}`)
1089 - })
1090 -}
1091 -
1092 -func TestFailingSample1(t *testing.T) {
1093 - inputFile := `
1094 - package foo
1095 -
1096 - // Map sample tests openAPIGen.generateMapProperty method.
1097 - type Blah struct {
1098 - // A sample String to String map
1099 - StringToArray map[string]map[string]map[int]string
1100 - }`
1101 -
1102 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1103 - e := packagestest.Export(t, x, []packagestest.Module{{
1104 - Name: "example.com/base/foo",
1105 - Files: map[string]interface{}{
1106 - "foo.go": inputFile,
1107 - },
1108 - }})
1109 - defer e.Cleanup()
1110 -
1111 - _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
1112 - if funcErr == nil {
1113 - t.Fatalf("An error was expected")
1114 - }
1115 - assertEqual(t,
1116 - "failed to generate map property in example.com/base/foo.Blah: StringToArray: map with non-string keys are not supported by OpenAPI in map[int]string",
1117 - funcErr.Error())
1118 - })
1119 -}
1120 -
1121 -func TestFailingSample2(t *testing.T) {
1122 - inputFile := `
1123 - package foo
1124 -
1125 - // Map sample tests openAPIGen.generateMapProperty method.
1126 - type Blah struct {
1127 - // A sample String to String map
1128 - StringToArray map[int]string
1129 - }`
1130 -
1131 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1132 - e := packagestest.Export(t, x, []packagestest.Module{{
1133 - Name: "example.com/base/foo",
1134 - Files: map[string]interface{}{
1135 - "foo.go": inputFile,
1136 - },
1137 - }})
1138 - defer e.Cleanup()
1139 -
1140 - _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
1141 - if funcErr == nil {
1142 - t.Fatalf("An error was expected")
1143 - }
1144 - assertEqual(t,
1145 - "failed to generate map property in example.com/base/foo.Blah: StringToArray: map with non-string keys are not supported by OpenAPI in map[int]string",
1146 - funcErr.Error())
1147 - })
1148 -}
1149 -
1150 -func TestFailingDefaultEnforced(t *testing.T) {
1151 - tests := []struct {
1152 - definition string
1153 - expectedError string
1154 - }{{
1155 - definition: `
1156 - package foo
1157 -
1158 - type Blah struct {
1159 - // +default=5
1160 - Int int
1161 - }`,
1162 - expectedError: "failed to generate default in example.com/base/foo.Blah: Int: invalid default value (5) for non-pointer/non-omitempty. If specified, must be: 0",
1163 - }, {
1164 - definition: `
1165 - package foo
1166 -
1167 - type Blah struct {
1168 - // +default={"foo": 5}
1169 - Struct struct{
1170 - foo int
1171 - }
1172 - }`,
1173 - 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: {}`,
1174 - }}
1175 -
1176 - for i, test := range tests {
1177 - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
1178 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1179 - e := packagestest.Export(t, x, []packagestest.Module{{
1180 - Name: "example.com/base/foo",
1181 - Files: map[string]interface{}{
1182 - "foo.go": test.definition,
1183 - },
1184 - }})
1185 - defer e.Cleanup()
1186 -
1187 - _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
1188 - if funcErr == nil {
1189 - t.Fatalf("An error was expected")
1190 - }
1191 - assertEqual(t, test.expectedError, funcErr.Error())
1192 - })
1193 - })
1194 - }
1195 -}
1196 -
1197 -// TestSliceMapElementDefaultIgnored verifies that a +default annotation on a
1198 -// type used as a slice item or map value is silently ignored: the generator
1199 -// succeeds and produces a schema with no default on the items/value schema.
1200 -func TestSliceMapElementDefaultIgnored(t *testing.T) {
1201 - tests := []struct {
1202 - name string
1203 - definition string
1204 - }{{
1205 - name: "slice element default ignored",
1206 - definition: `
1207 - package foo
1208 -
1209 - // +k8s:openapi-gen=true
1210 - type Blah struct {
1211 - List []Item
1212 - }
1213 -
1214 - // +default="foo"
1215 - type Item string`,
1216 - }, {
1217 - name: "map element default ignored",
1218 - definition: `
1219 - package foo
1220 -
1221 - // +k8s:openapi-gen=true
1222 - type Blah struct {
1223 - Map map[string]Item
1224 - }
1225 -
1226 - // +default="foo"
1227 - type Item string`,
1228 - }}
1229 -
1230 - for _, test := range tests {
1231 - t.Run(test.name, func(t *testing.T) {
1232 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1233 - e := packagestest.Export(t, x, []packagestest.Module{{
1234 - Name: "example.com/base/foo",
1235 - Files: map[string]interface{}{
1236 - "foo.go": test.definition,
1237 - },
1238 - }})
1239 - defer e.Cleanup()
1240 -
1241 - _, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
1242 - if funcErr != nil {
1243 - t.Fatalf("Unexpected error: %v", funcErr)
1244 - }
1245 - })
1246 - })
1247 - }
1248 -}
1249 -
1250 -func TestCustomDef(t *testing.T) {
1251 - inputFile := `
1252 - package foo
1253 -
1254 - import openapi "k8s.io/kube-openapi/pkg/common"
1255 -
1256 - type Blah struct {
1257 - }
1258 -
1259 - func (_ Blah) OpenAPIDefinition() openapi.OpenAPIDefinition {
1260 - return openapi.OpenAPIDefinition{
1261 - Schema: spec.Schema{
1262 - SchemaProps: spec.SchemaProps{
1263 - Type: []string{"string"},
1264 - Format: "date-time",
1265 - },
1266 - },
1267 - }
1268 - }`
1269 - commonFile := `package common
1270 -
1271 - type OpenAPIDefinition struct {}`
1272 -
1273 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1274 - e := packagestest.Export(t, x, []packagestest.Module{{
1275 - Name: "example.com/base/foo",
1276 - Files: map[string]interface{}{
1277 - "foo.go": inputFile,
1278 - },
1279 - }, {
1280 - Name: "k8s.io/kube-openapi/pkg/common",
1281 - Files: map[string]interface{}{
1282 - "common.go": commonFile,
1283 - },
1284 - }})
1285 - defer e.Cleanup()
1286 -
1287 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1288 - if callErr != nil {
1289 - t.Fatal(callErr)
1290 - }
1291 - if funcErr != nil {
1292 - t.Fatal(funcErr)
1293 - }
1294 - assertEqual(t, callBuffer.String(),
1295 - `"example.com/base/foo.Blah": foo.Blah{}.OpenAPIDefinition(),`)
1296 - assertEqual(t, "", funcBuffer.String())
1297 - })
1298 -}
1299 -
1300 -func TestCustomDefV3(t *testing.T) {
1301 - inputFile := `
1302 - package foo
1303 -
1304 - import openapi "k8s.io/kube-openapi/pkg/common"
1305 -
1306 - type Blah struct {
1307 - }
1308 -
1309 - func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition {
1310 - return openapi.OpenAPIDefinition{
1311 - Schema: spec.Schema{
1312 - SchemaProps: spec.SchemaProps{
1313 - Type: []string{"string"},
1314 - Format: "date-time",
1315 - },
1316 - },
1317 - }
1318 - }`
1319 - commonFile := `package common
1320 -
1321 - type OpenAPIDefinition struct {}`
1322 -
1323 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1324 - e := packagestest.Export(t, x, []packagestest.Module{{
1325 - Name: "example.com/base/foo",
1326 - Files: map[string]interface{}{
1327 - "foo.go": inputFile,
1328 - },
1329 - }, {
1330 - Name: "k8s.io/kube-openapi/pkg/common",
1331 - Files: map[string]interface{}{
1332 - "common.go": commonFile,
1333 - },
1334 - }})
1335 - defer e.Cleanup()
1336 -
1337 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1338 - if callErr != nil {
1339 - t.Fatal(callErr)
1340 - }
1341 - if funcErr != nil {
1342 - t.Fatal(funcErr)
1343 - }
1344 - assertEqual(t, callBuffer.String(),
1345 - `"example.com/base/foo.Blah": foo.Blah{}.OpenAPIV3Definition(),`)
1346 - assertEqual(t, "", funcBuffer.String())
1347 - })
1348 -}
1349 -
1350 -func TestCustomDefV2AndV3(t *testing.T) {
1351 - inputFile := `
1352 - package foo
1353 -
1354 - import openapi "k8s.io/kube-openapi/pkg/common"
1355 -
1356 - type Blah struct {
1357 - }
1358 -
1359 - func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition {
1360 - return openapi.OpenAPIDefinition{
1361 - Schema: spec.Schema{
1362 - SchemaProps: spec.SchemaProps{
1363 - Type: []string{"string"},
1364 - Format: "date-time",
1365 - },
1366 - },
1367 - }
1368 - }
1369 -
1370 - func (_ Blah) OpenAPIDefinition() openapi.OpenAPIDefinition {
1371 - return openapi.OpenAPIDefinition{
1372 - Schema: spec.Schema{
1373 - SchemaProps: spec.SchemaProps{
1374 - Type: []string{"string"},
1375 - Format: "date-time",
1376 - },
1377 - },
1378 - }
1379 - }`
1380 - commonFile := `package common
1381 -
1382 - type OpenAPIDefinition struct {}`
1383 -
1384 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1385 - e := packagestest.Export(t, x, []packagestest.Module{{
1386 - Name: "example.com/base/foo",
1387 - Files: map[string]interface{}{
1388 - "foo.go": inputFile,
1389 - },
1390 - }, {
1391 - Name: "k8s.io/kube-openapi/pkg/common",
1392 - Files: map[string]interface{}{
1393 - "common.go": commonFile,
1394 - },
1395 - }})
1396 - defer e.Cleanup()
1397 -
1398 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1399 - if callErr != nil {
1400 - t.Fatal(callErr)
1401 - }
1402 - if funcErr != nil {
1403 - t.Fatal(funcErr)
1404 - }
1405 - assertEqual(t, callBuffer.String(),
1406 - `"example.com/base/foo.Blah": common.EmbedOpenAPIDefinitionIntoV2Extension(foo.Blah{}.OpenAPIV3Definition(), foo.Blah{}.OpenAPIDefinition()),`)
1407 - assertEqual(t, "", funcBuffer.String())
1408 - })
1409 -}
1410 -
1411 -func TestCustomDefs(t *testing.T) {
1412 - inputFile := `
1413 - package foo
1414 -
1415 - // Blah is a custom type
1416 - type Blah struct {
1417 - }
1418 -
1419 - func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} }
1420 - func (_ Blah) OpenAPISchemaFormat() string { return "date-time" }`
1421 -
1422 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1423 - e := packagestest.Export(t, x, []packagestest.Module{{
1424 - Name: "example.com/base/foo",
1425 - Files: map[string]interface{}{
1426 - "foo.go": inputFile,
1427 - },
1428 - }})
1429 - defer e.Cleanup()
1430 -
1431 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1432 - if callErr != nil {
1433 - t.Fatal(callErr)
1434 - }
1435 - if funcErr != nil {
1436 - t.Fatal(funcErr)
1437 - }
1438 - assertEqual(t, callBuffer.String(),
1439 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1440 - assertEqual(t, funcBuffer.String(),
1441 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1442 -return common.OpenAPIDefinition{
1443 -Schema: spec.Schema{
1444 -SchemaProps: spec.SchemaProps{
1445 -Description: "Blah is a custom type",
1446 -Type:foo.Blah{}.OpenAPISchemaType(),
1447 -Format:foo.Blah{}.OpenAPISchemaFormat(),
1448 -},
1449 -},
1450 -}
1451 -}`)
1452 - })
1453 -}
1454 -
1455 -func TestCustomDefsV3(t *testing.T) {
1456 - inputFile := `
1457 - package foo
1458 -
1459 - import openapi "k8s.io/kube-openapi/pkg/common"
1460 -
1461 - // Blah is a custom type
1462 - type Blah struct {
1463 - }
1464 -
1465 - func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition {
1466 - return openapi.OpenAPIDefinition{
1467 - Schema: spec.Schema{
1468 - SchemaProps: spec.SchemaProps{
1469 - Type: []string{"string"},
1470 - Format: "date-time",
1471 - },
1472 - },
1473 - }
1474 - }
1475 -
1476 - func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} }
1477 - func (_ Blah) OpenAPISchemaFormat() string { return "date-time" }`
1478 - commonFile := `package common
1479 -
1480 - type OpenAPIDefinition struct {}`
1481 -
1482 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1483 - e := packagestest.Export(t, x, []packagestest.Module{{
1484 - Name: "example.com/base/foo",
1485 - Files: map[string]interface{}{
1486 - "foo.go": inputFile,
1487 - },
1488 - }, {
1489 - Name: "k8s.io/kube-openapi/pkg/common",
1490 - Files: map[string]interface{}{
1491 - "common.go": commonFile,
1492 - },
1493 - }})
1494 - defer e.Cleanup()
1495 -
1496 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1497 - if callErr != nil {
1498 - t.Fatal(callErr)
1499 - }
1500 - if funcErr != nil {
1501 - t.Fatal(funcErr)
1502 - }
1503 - assertEqual(t, callBuffer.String(),
1504 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1505 - assertEqual(t, funcBuffer.String(),
1506 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1507 -return common.EmbedOpenAPIDefinitionIntoV2Extension(foo.Blah{}.OpenAPIV3Definition(), common.OpenAPIDefinition{
1508 -Schema: spec.Schema{
1509 -SchemaProps: spec.SchemaProps{
1510 -Description: "Blah is a custom type",
1511 -Type:foo.Blah{}.OpenAPISchemaType(),
1512 -Format:foo.Blah{}.OpenAPISchemaFormat(),
1513 -},
1514 -},
1515 -})
1516 -}`)
1517 - })
1518 -}
1519 -
1520 -func TestV3OneOfTypes(t *testing.T) {
1521 - inputFile := `
1522 - package foo
1523 -
1524 - // Blah is a custom type
1525 - type Blah struct {
1526 - }
1527 -
1528 - func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} }
1529 - func (_ Blah) OpenAPISchemaFormat() string { return "date-time" }
1530 - func (_ Blah) OpenAPIV3OneOfTypes() []string { return []string{"string", "number"} }`
1531 -
1532 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1533 - e := packagestest.Export(t, x, []packagestest.Module{{
1534 - Name: "example.com/base/foo",
1535 - Files: map[string]interface{}{
1536 - "foo.go": inputFile,
1537 - },
1538 - }})
1539 - defer e.Cleanup()
1540 -
1541 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1542 - if callErr != nil {
1543 - t.Fatal(callErr)
1544 - }
1545 - if funcErr != nil {
1546 - t.Fatal(funcErr)
1547 - }
1548 - assertEqual(t, callBuffer.String(),
1549 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1550 - assertEqual(t, funcBuffer.String(),
1551 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1552 -return common.EmbedOpenAPIDefinitionIntoV2Extension(common.OpenAPIDefinition{
1553 -Schema: spec.Schema{
1554 -SchemaProps: spec.SchemaProps{
1555 -Description: "Blah is a custom type",
1556 -OneOf:common.GenerateOpenAPIV3OneOfSchema(foo.Blah{}.OpenAPIV3OneOfTypes()),
1557 -Format:foo.Blah{}.OpenAPISchemaFormat(),
1558 -},
1559 -},
1560 -},common.OpenAPIDefinition{
1561 -Schema: spec.Schema{
1562 -SchemaProps: spec.SchemaProps{
1563 -Description: "Blah is a custom type",
1564 -Type:foo.Blah{}.OpenAPISchemaType(),
1565 -Format:foo.Blah{}.OpenAPISchemaFormat(),
1566 -},
1567 -},
1568 -})
1569 -}`)
1570 - })
1571 -}
1572 -
1573 -func TestPointer(t *testing.T) {
1574 - inputFile := `
1575 - package foo
1576 -
1577 - // PointerSample demonstrate pointer's properties
1578 - type Blah struct {
1579 - // A string pointer
1580 - StringPointer *string
1581 - // A struct pointer
1582 - StructPointer *Blah
1583 - // A slice pointer
1584 - SlicePointer *[]string
1585 - // A map pointer
1586 - MapPointer *map[string]string
1587 - }`
1588 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1589 - e := packagestest.Export(t, x, []packagestest.Module{{
1590 - Name: "example.com/base/foo",
1591 - Files: map[string]interface{}{
1592 - "foo.go": inputFile,
1593 - },
1594 - }})
1595 - defer e.Cleanup()
1596 -
1597 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1598 - if callErr != nil {
1599 - t.Fatal(callErr)
1600 - }
1601 - if funcErr != nil {
1602 - t.Fatal(funcErr)
1603 - }
1604 - assertEqual(t, callBuffer.String(),
1605 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1606 - assertEqual(t, funcBuffer.String(),
1607 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1608 -return common.OpenAPIDefinition{
1609 -Schema: spec.Schema{
1610 -SchemaProps: spec.SchemaProps{
1611 -Description: "PointerSample demonstrate pointer's properties",
1612 -Type: []string{"object"},
1613 -Properties: map[string]spec.Schema{
1614 -"StringPointer": {
1615 -SchemaProps: spec.SchemaProps{
1616 -Description: "A string pointer",
1617 -Type: []string{"string"},
1618 -Format: "",
1619 -},
1620 -},
1621 -"StructPointer": {
1622 -SchemaProps: spec.SchemaProps{
1623 -Description: "A struct pointer",
1624 -Ref: ref("example.com/base/foo.Blah"),
1625 -},
1626 -},
1627 -"SlicePointer": {
1628 -SchemaProps: spec.SchemaProps{
1629 -Description: "A slice pointer",
1630 -Type: []string{"array"},
1631 -Items: &spec.SchemaOrArray{
1632 -Schema: &spec.Schema{
1633 -SchemaProps: spec.SchemaProps{
1634 -Type: []string{"string"},
1635 -Format: "",
1636 -},
1637 -},
1638 -},
1639 -},
1640 -},
1641 -"MapPointer": {
1642 -SchemaProps: spec.SchemaProps{
1643 -Description: "A map pointer",
1644 -Type: []string{"object"},
1645 -AdditionalProperties: &spec.SchemaOrBool{
1646 -Allows: true,
1647 -Schema: &spec.Schema{
1648 -SchemaProps: spec.SchemaProps{
1649 -Type: []string{"string"},
1650 -Format: "",
1651 -},
1652 -},
1653 -},
1654 -},
1655 -},
1656 -},
1657 -Required: []string{"StringPointer","StructPointer","SlicePointer","MapPointer"},
1658 -},
1659 -},
1660 -Dependencies: []string{
1661 -"example.com/base/foo.Blah",},
1662 -}
1663 -}`)
1664 - })
1665 -}
1666 -
1667 -func TestNestedLists(t *testing.T) {
1668 - inputFile := `
1669 - package foo
1670 -
1671 - // Blah is a test.
1672 - // +k8s:openapi-gen=true
1673 - // +k8s:openapi-gen=x-kubernetes-type-tag:type_test
1674 - type Blah struct {
1675 - // Nested list
1676 - NestedList [][]int64
1677 - }`
1678 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1679 - e := packagestest.Export(t, x, []packagestest.Module{{
1680 - Name: "example.com/base/foo",
1681 - Files: map[string]interface{}{
1682 - "foo.go": inputFile,
1683 - },
1684 - }})
1685 - defer e.Cleanup()
1686 -
1687 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1688 - if callErr != nil {
1689 - t.Fatal(callErr)
1690 - }
1691 - if funcErr != nil {
1692 - t.Fatal(funcErr)
1693 - }
1694 - assertEqual(t, callBuffer.String(),
1695 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1696 - assertEqual(t, funcBuffer.String(),
1697 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1698 -return common.OpenAPIDefinition{
1699 -Schema: spec.Schema{
1700 -SchemaProps: spec.SchemaProps{
1701 -Description: "Blah is a test.",
1702 -Type: []string{"object"},
1703 -Properties: map[string]spec.Schema{
1704 -"NestedList": {
1705 -SchemaProps: spec.SchemaProps{
1706 -Description: "Nested list",
1707 -Type: []string{"array"},
1708 -Items: &spec.SchemaOrArray{
1709 -Schema: &spec.Schema{
1710 -SchemaProps: spec.SchemaProps{
1711 -Type: []string{"array"},
1712 -Items: &spec.SchemaOrArray{
1713 -Schema: &spec.Schema{
1714 -SchemaProps: spec.SchemaProps{
1715 -Type: []string{"integer"},
1716 -Format: "int64",
1717 -},
1718 -},
1719 -},
1720 -},
1721 -},
1722 -},
1723 -},
1724 -},
1725 -},
1726 -Required: []string{"NestedList"},
1727 -},
1728 -VendorExtensible: spec.VendorExtensible{
1729 -Extensions: spec.Extensions{
1730 -"x-kubernetes-type-tag": "type_test",
1731 -},
1732 -},
1733 -},
1734 -}
1735 -}`)
1736 - })
1737 -}
1738 -
1739 -func TestNestListOfMaps(t *testing.T) {
1740 - inputFile := `
1741 - package foo
1742 -
1743 - // Blah is a test.
1744 - // +k8s:openapi-gen=true
1745 - // +k8s:openapi-gen=x-kubernetes-type-tag:type_test
1746 - type Blah struct {
1747 - // Nested list of maps
1748 - NestedListOfMaps [][]map[string]string
1749 - }`
1750 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1751 - e := packagestest.Export(t, x, []packagestest.Module{{
1752 - Name: "example.com/base/foo",
1753 - Files: map[string]interface{}{
1754 - "foo.go": inputFile,
1755 - },
1756 - }})
1757 - defer e.Cleanup()
1758 -
1759 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1760 - if callErr != nil {
1761 - t.Fatal(callErr)
1762 - }
1763 - if funcErr != nil {
1764 - t.Fatal(funcErr)
1765 - }
1766 - assertEqual(t, callBuffer.String(),
1767 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1768 - assertEqual(t, funcBuffer.String(),
1769 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1770 -return common.OpenAPIDefinition{
1771 -Schema: spec.Schema{
1772 -SchemaProps: spec.SchemaProps{
1773 -Description: "Blah is a test.",
1774 -Type: []string{"object"},
1775 -Properties: map[string]spec.Schema{
1776 -"NestedListOfMaps": {
1777 -SchemaProps: spec.SchemaProps{
1778 -Description: "Nested list of maps",
1779 -Type: []string{"array"},
1780 -Items: &spec.SchemaOrArray{
1781 -Schema: &spec.Schema{
1782 -SchemaProps: spec.SchemaProps{
1783 -Type: []string{"array"},
1784 -Items: &spec.SchemaOrArray{
1785 -Schema: &spec.Schema{
1786 -SchemaProps: spec.SchemaProps{
1787 -Type: []string{"object"},
1788 -AdditionalProperties: &spec.SchemaOrBool{
1789 -Allows: true,
1790 -Schema: &spec.Schema{
1791 -SchemaProps: spec.SchemaProps{
1792 -Type: []string{"string"},
1793 -Format: "",
1794 -},
1795 -},
1796 -},
1797 -},
1798 -},
1799 -},
1800 -},
1801 -},
1802 -},
1803 -},
1804 -},
1805 -},
1806 -Required: []string{"NestedListOfMaps"},
1807 -},
1808 -VendorExtensible: spec.VendorExtensible{
1809 -Extensions: spec.Extensions{
1810 -"x-kubernetes-type-tag": "type_test",
1811 -},
1812 -},
1813 -},
1814 -}
1815 -}`)
1816 - })
1817 -}
1818 -
1819 -func TestExtensions(t *testing.T) {
1820 - inputFile := `
1821 - package foo
1822 -
1823 - // Blah is a test.
1824 - // +k8s:openapi-gen=true
1825 - // +k8s:openapi-gen=x-kubernetes-type-tag:type_test
1826 - type Blah struct {
1827 - // a member with a list type with two map keys
1828 - // +listType=map
1829 - // +listMapKey=port
1830 - // +listMapKey=protocol
1831 - WithListField []string
1832 -
1833 - // another member with a list type with one map key
1834 - // +listType=map
1835 - // +listMapKey=port
1836 - WithListField2 []string
1837 - }`
1838 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1839 - e := packagestest.Export(t, x, []packagestest.Module{{
1840 - Name: "example.com/base/foo",
1841 - Files: map[string]interface{}{
1842 - "foo.go": inputFile,
1843 - },
1844 - }})
1845 - defer e.Cleanup()
1846 -
1847 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1848 - if callErr != nil {
1849 - t.Fatal(callErr)
1850 - }
1851 - if funcErr != nil {
1852 - t.Fatal(funcErr)
1853 - }
1854 - assertEqual(t, callBuffer.String(),
1855 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1856 - assertEqual(t, funcBuffer.String(),
1857 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1858 -return common.OpenAPIDefinition{
1859 -Schema: spec.Schema{
1860 -SchemaProps: spec.SchemaProps{
1861 -Description: "Blah is a test.",
1862 -Type: []string{"object"},
1863 -Properties: map[string]spec.Schema{
1864 -"WithListField": {
1865 -VendorExtensible: spec.VendorExtensible{
1866 -Extensions: spec.Extensions{
1867 -"x-kubernetes-list-map-keys": []interface{}{
1868 -"port",
1869 -"protocol",
1870 -},
1871 -"x-kubernetes-list-type": "map",
1872 -},
1873 -},
1874 -SchemaProps: spec.SchemaProps{
1875 -Description: "a member with a list type with two map keys",
1876 -Type: []string{"array"},
1877 -Items: &spec.SchemaOrArray{
1878 -Schema: &spec.Schema{
1879 -SchemaProps: spec.SchemaProps{
1880 -Type: []string{"string"},
1881 -Format: "",
1882 -},
1883 -},
1884 -},
1885 -},
1886 -},
1887 -"WithListField2": {
1888 -VendorExtensible: spec.VendorExtensible{
1889 -Extensions: spec.Extensions{
1890 -"x-kubernetes-list-map-keys": []interface{}{
1891 -"port",
1892 -},
1893 -"x-kubernetes-list-type": "map",
1894 -},
1895 -},
1896 -SchemaProps: spec.SchemaProps{
1897 -Description: "another member with a list type with one map key",
1898 -Type: []string{"array"},
1899 -Items: &spec.SchemaOrArray{
1900 -Schema: &spec.Schema{
1901 -SchemaProps: spec.SchemaProps{
1902 -Type: []string{"string"},
1903 -Format: "",
1904 -},
1905 -},
1906 -},
1907 -},
1908 -},
1909 -},
1910 -Required: []string{"WithListField","WithListField2"},
1911 -},
1912 -VendorExtensible: spec.VendorExtensible{
1913 -Extensions: spec.Extensions{
1914 -"x-kubernetes-type-tag": "type_test",
1915 -},
1916 -},
1917 -},
1918 -}
1919 -}`)
1920 - })
1921 -}
1922 -
1923 -func TestUnion(t *testing.T) {
1924 - inputFile := `
1925 - package foo
1926 -
1927 - // Blah is a test.
1928 - // +k8s:openapi-gen=true
1929 - // +k8s:openapi-gen=x-kubernetes-type-tag:type_test
1930 - // +union
1931 - type Blah struct {
1932 - // +unionDiscriminator
1933 - Discriminator *string ` + "`" + `json:"discriminator"` + "`" + `
1934 - // +optional
1935 - Numeric int ` + "`" + `json:"numeric"` + "`" + `
1936 - // +optional
1937 - String string ` + "`" + `json:"string"` + "`" + `
1938 - // +optional
1939 - Float float64 ` + "`" + `json:"float"` + "`" + `
1940 - }`
1941 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
1942 - e := packagestest.Export(t, x, []packagestest.Module{{
1943 - Name: "example.com/base/foo",
1944 - Files: map[string]interface{}{
1945 - "foo.go": inputFile,
1946 - },
1947 - }})
1948 - defer e.Cleanup()
1949 -
1950 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
1951 - if callErr != nil {
1952 - t.Fatal(callErr)
1953 - }
1954 - if funcErr != nil {
1955 - t.Fatal(funcErr)
1956 - }
1957 - assertEqual(t, callBuffer.String(),
1958 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
1959 - assertEqual(t, funcBuffer.String(),
1960 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
1961 -return common.OpenAPIDefinition{
1962 -Schema: spec.Schema{
1963 -SchemaProps: spec.SchemaProps{
1964 -Description: "Blah is a test.",
1965 -Type: []string{"object"},
1966 -Properties: map[string]spec.Schema{
1967 -"discriminator": {
1968 -SchemaProps: spec.SchemaProps{
1969 -Type: []string{"string"},
1970 -Format: "",
1971 -},
1972 -},
1973 -"numeric": {
1974 -SchemaProps: spec.SchemaProps{
1975 -Default: 0,
1976 -Type: []string{"integer"},
1977 -Format: "int32",
1978 -},
1979 -},
1980 -"string": {
1981 -SchemaProps: spec.SchemaProps{
1982 -Default: "",
1983 -Type: []string{"string"},
1984 -Format: "",
1985 -},
1986 -},
1987 -"float": {
1988 -SchemaProps: spec.SchemaProps{
1989 -Default: 0,
1990 -Type: []string{"number"},
1991 -Format: "double",
1992 -},
1993 -},
1994 -},
1995 -Required: []string{"discriminator"},
1996 -},
1997 -VendorExtensible: spec.VendorExtensible{
1998 -Extensions: spec.Extensions{
1999 -"x-kubernetes-type-tag": "type_test",
2000 -"x-kubernetes-unions": []interface{}{
2001 -map[string]interface{}{
2002 -"discriminator": "discriminator",
2003 -"fields-to-discriminateBy": map[string]interface{}{
2004 -"float": "Float",
2005 -"numeric": "Numeric",
2006 -"string": "String",
2007 -},
2008 -},
2009 -},
2010 -},
2011 -},
2012 -},
2013 -}
2014 -}`)
2015 - })
2016 -}
2017 -
2018 -func TestEnumAlias(t *testing.T) {
2019 - inputFile := `
2020 - package foo
2021 -
2022 - import "example.com/base/bar"
2023 -
2024 - // EnumType is the enumType.
2025 - // +enum
2026 - type EnumType = bar.EnumType
2027 -
2028 - // EnumA is a.
2029 - const EnumA EnumType = bar.EnumA
2030 - // EnumB is b.
2031 - const EnumB EnumType = bar.EnumB
2032 -
2033 - // Blah is a test.
2034 - // +k8s:openapi-gen=true
2035 - type Blah struct {
2036 - // Value is the value.
2037 - Value EnumType
2038 - }`
2039 - otherFile := `
2040 - package bar
2041 -
2042 - // EnumType is the enumType.
2043 - // +enum
2044 - type EnumType string
2045 -
2046 - // EnumA is a.
2047 - const EnumA EnumType = "a"
2048 - // EnumB is b.
2049 - const EnumB EnumType = "b"`
2050 -
2051 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2052 - e := packagestest.Export(t, x, []packagestest.Module{{
2053 - Name: "example.com/base/foo",
2054 - Files: map[string]interface{}{
2055 - "foo.go": inputFile,
2056 - },
2057 - }, {
2058 - Name: "example.com/base/bar",
2059 - Files: map[string]interface{}{
2060 - "bar.go": otherFile,
2061 - },
2062 - }})
2063 - defer e.Cleanup()
2064 -
2065 - callErr, funcErr, _, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
2066 - if callErr != nil {
2067 - t.Fatal(callErr)
2068 - }
2069 - if funcErr != nil {
2070 - t.Fatal(funcErr)
2071 - }
2072 - assertEqual(t, funcBuffer.String(),
2073 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2074 -return common.OpenAPIDefinition{
2075 -Schema: spec.Schema{
2076 -SchemaProps: spec.SchemaProps{
2077 -Description: "Blah is a test.",
2078 -Type: []string{"object"},
2079 -Properties: map[string]spec.Schema{
2080 -"Value": {
2081 -SchemaProps: spec.SchemaProps{`+"\n"+
2082 - "Description: \"Value is the value.\\n\\nPossible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+`
2083 -Default: "",
2084 -Type: []string{"string"},
2085 -Format: "",
2086 -Enum: []interface{}{"a", "b"},
2087 -},
2088 -},
2089 -},
2090 -Required: []string{"Value"},
2091 -},
2092 -},
2093 -}
2094 -}`)
2095 - })
2096 -}
2097 -
2098 -func TestEnum(t *testing.T) {
2099 - inputFile := `
2100 - package foo
2101 -
2102 - // EnumType is the enumType.
2103 - // +enum
2104 - type EnumType string
2105 -
2106 - // EnumA is a.
2107 - const EnumA EnumType = "a"
2108 - // EnumB is b.
2109 - const EnumB EnumType = "b"
2110 -
2111 - // Blah is a test.
2112 - // +k8s:openapi-gen=true
2113 - // +k8s:openapi-gen=x-kubernetes-type-tag:type_test
2114 - type Blah struct {
2115 - // Value is the value.
2116 - Value EnumType
2117 - NoCommentEnum EnumType
2118 - // +optional
2119 - OptionalEnum *EnumType
2120 - List []EnumType
2121 - Map map[string]EnumType
2122 - }`
2123 -
2124 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2125 - e := packagestest.Export(t, x, []packagestest.Module{{
2126 - Name: "example.com/base/foo",
2127 - Files: map[string]interface{}{
2128 - "foo.go": inputFile,
2129 - },
2130 - }})
2131 - defer e.Cleanup()
2132 -
2133 - callErr, funcErr, _, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
2134 - if callErr != nil {
2135 - t.Fatal(callErr)
2136 - }
2137 - if funcErr != nil {
2138 - t.Fatal(funcErr)
2139 - }
2140 - assertEqual(t, funcBuffer.String(),
2141 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2142 -return common.OpenAPIDefinition{
2143 -Schema: spec.Schema{
2144 -SchemaProps: spec.SchemaProps{
2145 -Description: "Blah is a test.",
2146 -Type: []string{"object"},
2147 -Properties: map[string]spec.Schema{
2148 -"Value": {
2149 -SchemaProps: spec.SchemaProps{`+"\n"+
2150 - "Description: \"Value is the value.\\n\\nPossible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+`
2151 -Default: "",
2152 -Type: []string{"string"},
2153 -Format: "",
2154 -Enum: []interface{}{"a", "b"},
2155 -},
2156 -},
2157 -"NoCommentEnum": {
2158 -SchemaProps: spec.SchemaProps{`+"\n"+
2159 - "Description: \"Possible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+`
2160 -Default: "",
2161 -Type: []string{"string"},
2162 -Format: "",
2163 -Enum: []interface{}{"a", "b"},
2164 -},
2165 -},
2166 -"OptionalEnum": {
2167 -SchemaProps: spec.SchemaProps{`+"\n"+
2168 - "Description: \"Possible enum values:\\n - `\\\"a\\\"` is a.\\n - `\\\"b\\\"` is b.\","+`
2169 -Type: []string{"string"},
2170 -Format: "",
2171 -Enum: []interface{}{"a", "b"},
2172 -},
2173 -},
2174 -"List": {
2175 -SchemaProps: spec.SchemaProps{
2176 -Type: []string{"array"},
2177 -Items: &spec.SchemaOrArray{
2178 -Schema: &spec.Schema{
2179 -SchemaProps: spec.SchemaProps{
2180 -Type: []string{"string"},
2181 -Format: "",
2182 -Enum: []interface{}{"a", "b"},
2183 -},
2184 -},
2185 -},
2186 -},
2187 -},
2188 -"Map": {
2189 -SchemaProps: spec.SchemaProps{
2190 -Type: []string{"object"},
2191 -AdditionalProperties: &spec.SchemaOrBool{
2192 -Allows: true,
2193 -Schema: &spec.Schema{
2194 -SchemaProps: spec.SchemaProps{
2195 -Type: []string{"string"},
2196 -Format: "",
2197 -Enum: []interface{}{"a", "b"},
2198 -},
2199 -},
2200 -},
2201 -},
2202 -},
2203 -},
2204 -Required: []string{"Value","NoCommentEnum","List","Map"},
2205 -},
2206 -VendorExtensible: spec.VendorExtensible{
2207 -Extensions: spec.Extensions{
2208 -"x-kubernetes-type-tag": "type_test",
2209 -},
2210 -},
2211 -},
2212 -}
2213 -}`)
2214 - })
2215 -}
2216 -
2217 -func TestSymbolReference(t *testing.T) {
2218 - inputFile := `
2219 - package foo
2220 -
2221 - // +k8s:openapi-gen=true
2222 - type Blah struct {
2223 - // +default="A Default Value"
2224 - // +optional
2225 - Value *string
2226 -
2227 - // User constant local to the output package fully qualified
2228 - // +default=ref(example.com/base/output.MyConst)
2229 - // +optional
2230 - FullyQualifiedOutputValue *string
2231 -
2232 - // Local to types but not to output
2233 - // +default=ref(MyConst)
2234 - // +optional
2235 - LocalValue *string
2236 -
2237 - // +default=ref(example.com/base/foo.MyConst)
2238 - // +optional
2239 - FullyQualifiedLocalValue *string
2240 -
2241 - // +default=ref(k8s.io/api/v1.TerminationPathDefault)
2242 - // +optional
2243 - FullyQualifiedExternalValue *string
2244 - }`
2245 -
2246 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2247 - e := packagestest.Export(t, x, []packagestest.Module{{
2248 - Name: "example.com/base/foo",
2249 - Files: map[string]interface{}{
2250 - "foo.go": inputFile,
2251 - },
2252 - }})
2253 - defer e.Cleanup()
2254 -
2255 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
2256 - if funcErr != nil {
2257 - t.Fatalf("Unexpected funcErr: %v", funcErr)
2258 - }
2259 - if callErr != nil {
2260 - t.Fatalf("Unexpected callErr: %v", callErr)
2261 - }
2262 - expImports := []string{
2263 - `foo "example.com/base/foo"`,
2264 - `v1 "k8s.io/api/v1"`,
2265 - `common "k8s.io/kube-openapi/pkg/common"`,
2266 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
2267 - }
2268 - if !cmp.Equal(imports, expImports) {
2269 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
2270 - }
2271 -
2272 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
2273 - t.Fatal(err)
2274 - } else {
2275 - assertEqual(t, string(formatted), `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2276 - return common.OpenAPIDefinition{
2277 - Schema: spec.Schema{
2278 - SchemaProps: spec.SchemaProps{
2279 - Type: []string{"object"},
2280 - Properties: map[string]spec.Schema{
2281 - "Value": {
2282 - SchemaProps: spec.SchemaProps{
2283 - Default: "A Default Value",
2284 - Type: []string{"string"},
2285 - Format: "",
2286 - },
2287 - },
2288 - "FullyQualifiedOutputValue": {
2289 - SchemaProps: spec.SchemaProps{
2290 - Description: "User constant local to the output package fully qualified",
2291 - Default: MyConst,
2292 - Type: []string{"string"},
2293 - Format: "",
2294 - },
2295 - },
2296 - "LocalValue": {
2297 - SchemaProps: spec.SchemaProps{
2298 - Description: "Local to types but not to output",
2299 - Default: foo.MyConst,
2300 - Type: []string{"string"},
2301 - Format: "",
2302 - },
2303 - },
2304 - "FullyQualifiedLocalValue": {
2305 - SchemaProps: spec.SchemaProps{
2306 - Default: foo.MyConst,
2307 - Type: []string{"string"},
2308 - Format: "",
2309 - },
2310 - },
2311 - "FullyQualifiedExternalValue": {
2312 - SchemaProps: spec.SchemaProps{
2313 - Default: v1.TerminationPathDefault,
2314 - Type: []string{"string"},
2315 - Format: "",
2316 - },
2317 - },
2318 - },
2319 - },
2320 - },
2321 - }
2322 -}`)
2323 - }
2324 - })
2325 -}
2326 -
2327 -// Show that types with unmarshalJSON in their hierarchy do not have struct
2328 -// defaults enforced, and that aliases and embededd types are respected
2329 -func TestMustEnforceDefaultStruct(t *testing.T) {
2330 - inputFile := `
2331 - package foo
2332 -
2333 - type Time struct {
2334 - value interface{}
2335 - }
2336 -
2337 -
2338 - type TimeWithoutUnmarshal struct {
2339 - value interface{}
2340 - }
2341 -
2342 - func (_ TimeWithoutUnmarshal) OpenAPISchemaType() []string { return []string{"string"} }
2343 - func (_ TimeWithoutUnmarshal) OpenAPISchemaFormat() string { return "date-time" }
2344 -
2345 - func (_ Time) UnmarshalJSON([]byte) error {
2346 - return nil
2347 - }
2348 -
2349 -
2350 - func (_ Time) OpenAPISchemaType() []string { return []string{"string"} }
2351 - func (_ Time) OpenAPISchemaFormat() string { return "date-time" }
2352 -
2353 - // Time with UnmarshalJSON defined on pointer instead of struct
2354 - type MicroTime struct {
2355 - value interface{}
2356 - }
2357 -
2358 - func (t *MicroTime) UnmarshalJSON([]byte) error {
2359 - return nil
2360 - }
2361 -
2362 - func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} }
2363 - func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" }
2364 -
2365 - type Int64 int64
2366 -
2367 - type Duration struct {
2368 - Int64
2369 - }
2370 -
2371 - func (_ Duration) OpenAPISchemaType() []string { return []string{"string"} }
2372 - func (_ Duration) OpenAPISchemaFormat() string { return "" }
2373 -
2374 - type NothingSpecial struct {
2375 - Field string
2376 - }
2377 -
2378 - // +k8s:openapi-gen=true
2379 - type Blah struct {
2380 - Embedded Duration
2381 - PointerUnmarshal MicroTime
2382 - StructUnmarshal Time
2383 - NoUnmarshal TimeWithoutUnmarshal
2384 - Regular NothingSpecial
2385 - }`
2386 -
2387 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2388 - e := packagestest.Export(t, x, []packagestest.Module{{
2389 - Name: "example.com/base/foo",
2390 - Files: map[string]interface{}{
2391 - "foo.go": inputFile,
2392 - },
2393 - }})
2394 - defer e.Cleanup()
2395 -
2396 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
2397 - if funcErr != nil {
2398 - t.Fatalf("Unexpected funcErr: %v", funcErr)
2399 - }
2400 - if callErr != nil {
2401 - t.Fatalf("Unexpected callErr: %v", callErr)
2402 - }
2403 - expImports := []string{
2404 - `foo "example.com/base/foo"`,
2405 - `common "k8s.io/kube-openapi/pkg/common"`,
2406 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
2407 - }
2408 - if !cmp.Equal(imports, expImports) {
2409 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
2410 - }
2411 -
2412 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
2413 - t.Fatal(err)
2414 - } else {
2415 - assertEqual(t, string(formatted), `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2416 - return common.OpenAPIDefinition{
2417 - Schema: spec.Schema{
2418 - SchemaProps: spec.SchemaProps{
2419 - Type: []string{"object"},
2420 - Properties: map[string]spec.Schema{
2421 - "Embedded": {
2422 - SchemaProps: spec.SchemaProps{
2423 - Default: 0,
2424 - Ref: ref("example.com/base/foo.Duration"),
2425 - },
2426 - },
2427 - "PointerUnmarshal": {
2428 - SchemaProps: spec.SchemaProps{
2429 - Ref: ref("example.com/base/foo.MicroTime"),
2430 - },
2431 - },
2432 - "StructUnmarshal": {
2433 - SchemaProps: spec.SchemaProps{
2434 - Ref: ref("example.com/base/foo.Time"),
2435 - },
2436 - },
2437 - "NoUnmarshal": {
2438 - SchemaProps: spec.SchemaProps{
2439 - Default: map[string]interface{}{},
2440 - Ref: ref("example.com/base/foo.TimeWithoutUnmarshal"),
2441 - },
2442 - },
2443 - "Regular": {
2444 - SchemaProps: spec.SchemaProps{
2445 - Default: map[string]interface{}{},
2446 - Ref: ref("example.com/base/foo.NothingSpecial"),
2447 - },
2448 - },
2449 - },
2450 - Required: []string{"Embedded", "PointerUnmarshal", "StructUnmarshal", "NoUnmarshal", "Regular"},
2451 - },
2452 - },
2453 - Dependencies: []string{
2454 - "example.com/base/foo.Duration", "example.com/base/foo.MicroTime", "example.com/base/foo.NothingSpecial", "example.com/base/foo.Time", "example.com/base/foo.TimeWithoutUnmarshal"},
2455 - }
2456 -}`)
2457 - }
2458 - })
2459 -}
2460 -
2461 -func TestMarkerComments(t *testing.T) {
2462 - inputFile := `
2463 - package foo
2464 -
2465 - // +k8s:openapi-gen=true
2466 - // +k8s:validation:maxProperties=10
2467 - // +k8s:validation:minProperties=1
2468 - type Blah struct {
2469 -
2470 - // Integer with min and max values
2471 - // +k8s:validation:minimum=0
2472 - // +k8s:validation:maximum=10
2473 - // +k8s:validation:exclusiveMinimum
2474 - // +k8s:validation:exclusiveMaximum
2475 - IntValue int
2476 -
2477 - // String with min and max lengths
2478 - // +k8s:validation:minLength=1
2479 - // +k8s:validation:maxLength=10
2480 - // +k8s:validation:pattern="^foo$[0-9]+"
2481 - StringValue string
2482 -
2483 - // +k8s:validation:maxItems=10
2484 - // +k8s:validation:minItems=1
2485 - // +k8s:validation:uniqueItems
2486 - ArrayValue []string
2487 -
2488 - // +k8s:validation:maxProperties=10
2489 - // +k8s:validation:minProperties=1
2490 - ObjValue map[string]interface{}
2491 - }`
2492 -
2493 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2494 - e := packagestest.Export(t, x, []packagestest.Module{{
2495 - Name: "example.com/base/foo",
2496 - Files: map[string]interface{}{
2497 - "foo.go": inputFile,
2498 - },
2499 - }})
2500 - defer e.Cleanup()
2501 -
2502 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
2503 - if funcErr != nil {
2504 - t.Fatalf("Unexpected funcErr: %v", funcErr)
2505 - }
2506 - if callErr != nil {
2507 - t.Fatalf("Unexpected callErr: %v", callErr)
2508 - }
2509 - expImports := []string{
2510 - `foo "example.com/base/foo"`,
2511 - `common "k8s.io/kube-openapi/pkg/common"`,
2512 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
2513 - `ptr "k8s.io/utils/ptr"`,
2514 - }
2515 - if !cmp.Equal(imports, expImports) {
2516 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
2517 - }
2518 -
2519 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
2520 - t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes()))
2521 - } else {
2522 - formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2523 - return common.OpenAPIDefinition{
2524 - Schema: spec.Schema{
2525 - SchemaProps: spec.SchemaProps{
2526 - Type: []string{"object"},
2527 - MinProperties: ptr.To[int64](1),
2528 - MaxProperties: ptr.To[int64](10),
2529 - Properties: map[string]spec.Schema{
2530 - "IntValue": {
2531 - SchemaProps: spec.SchemaProps{
2532 - Description: "Integer with min and max values",
2533 - Default: 0,
2534 - Minimum: ptr.To[float64](0),
2535 - Maximum: ptr.To[float64](10),
2536 - ExclusiveMinimum: true,
2537 - ExclusiveMaximum: true,
2538 - Type: []string{"integer"},
2539 - Format: "int32",
2540 - },
2541 - },
2542 - "StringValue": {
2543 - SchemaProps: spec.SchemaProps{
2544 - Description: "String with min and max lengths",
2545 - Default: "",
2546 - MinLength: ptr.To[int64](1),
2547 - MaxLength: ptr.To[int64](10),
2548 - Pattern: "^foo$[0-9]+",
2549 - Type: []string{"string"},
2550 - Format: "",
2551 - },
2552 - },
2553 - "ArrayValue": {
2554 - SchemaProps: spec.SchemaProps{
2555 - MinItems: ptr.To[int64](1),
2556 - MaxItems: ptr.To[int64](10),
2557 - UniqueItems: true,
2558 - Type: []string{"array"},
2559 - Items: &spec.SchemaOrArray{
2560 - Schema: &spec.Schema{
2561 - SchemaProps: spec.SchemaProps{
2562 - Type: []string{"string"},
2563 - Format: "",
2564 - },
2565 - },
2566 - },
2567 - },
2568 - },
2569 - "ObjValue": {
2570 - SchemaProps: spec.SchemaProps{
2571 - MinProperties: ptr.To[int64](1),
2572 - MaxProperties: ptr.To[int64](10),
2573 - Type: []string{"object"},
2574 - AdditionalProperties: &spec.SchemaOrBool{
2575 - Allows: true,
2576 - Schema: &spec.Schema{
2577 - SchemaProps: spec.SchemaProps{
2578 - Type: []string{"object"},
2579 - Format: "",
2580 - },
2581 - },
2582 - },
2583 - },
2584 - },
2585 - },
2586 - Required: []string{"IntValue", "StringValue", "ArrayValue", "ObjValue"},
2587 - },
2588 - },
2589 - }
2590 - }`))
2591 - if ree != nil {
2592 - t.Fatal(ree)
2593 - }
2594 - assertEqual(t, string(formatted), string(formatted_expected))
2595 - }
2596 - })
2597 -}
2598 -
2599 -func TestCELMarkerComments(t *testing.T) {
2600 - inputFile := `
2601 - package foo
2602 -
2603 - // +k8s:openapi-gen=true
2604 - // +k8s:validation:cel[0]:rule="self == oldSelf"
2605 - // +k8s:validation:cel[0]:message="message1"
2606 - type Blah struct {
2607 - // +k8s:validation:cel[0]:rule="self.length() > 0"
2608 - // +k8s:validation:cel[0]:message="string message"
2609 - // +k8s:validation:cel[1]:rule="self.length() % 2 == 0"
2610 - // +k8s:validation:cel[1]:messageExpression="self + ' hello'"
2611 - // +k8s:validation:cel[1]:optionalOldSelf
2612 - // +optional
2613 - Field string
2614 - }`
2615 -
2616 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2617 - e := packagestest.Export(t, x, []packagestest.Module{{
2618 - Name: "example.com/base/foo",
2619 - Files: map[string]interface{}{
2620 - "foo.go": inputFile,
2621 - },
2622 - }})
2623 - defer e.Cleanup()
2624 -
2625 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
2626 - if funcErr != nil {
2627 - t.Fatalf("Unexpected funcErr: %v", funcErr)
2628 - }
2629 - if callErr != nil {
2630 - t.Fatalf("Unexpected callErr: %v", callErr)
2631 - }
2632 - expImports := []string{
2633 - `foo "example.com/base/foo"`,
2634 - `common "k8s.io/kube-openapi/pkg/common"`,
2635 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
2636 - }
2637 - if !cmp.Equal(imports, expImports) {
2638 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
2639 - }
2640 -
2641 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
2642 - t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes()))
2643 - } else {
2644 - formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2645 - return common.OpenAPIDefinition{
2646 - Schema: spec.Schema{
2647 - SchemaProps: spec.SchemaProps{
2648 - Type: []string{"object"},
2649 - Properties: map[string]spec.Schema{
2650 - "Field": {
2651 - VendorExtensible: spec.VendorExtensible{
2652 - Extensions: spec.Extensions{
2653 - "x-kubernetes-validations": []interface{}{map[string]interface{}{"message": "string message", "rule": "self.length() > 0"}, map[string]interface{}{"messageExpression": "self + ' hello'", "optionalOldSelf": true, "rule": "self.length() % 2 == 0"}},
2654 - },
2655 - },
2656 - SchemaProps: spec.SchemaProps{
2657 - Default: "",
2658 - Type: []string{"string"},
2659 - Format: "",
2660 - },
2661 - },
2662 - },
2663 - },
2664 - VendorExtensible: spec.VendorExtensible{
2665 - Extensions: spec.Extensions{
2666 - "x-kubernetes-validations": []interface{}{map[string]interface{}{"message": "message1", "rule": "self == oldSelf"}},
2667 - },
2668 - },
2669 - },
2670 - }
2671 - }`))
2672 - if ree != nil {
2673 - t.Fatal(ree)
2674 - }
2675 - assertEqual(t, string(formatted_expected), string(formatted))
2676 - }
2677 - })
2678 -}
2679 -
2680 -func TestMultilineCELMarkerComments(t *testing.T) {
2681 - inputFile := `
2682 - package foo
2683 -
2684 - // +k8s:openapi-gen=true
2685 - // +k8s:validation:cel[0]:rule="self == oldSelf"
2686 - // +k8s:validation:cel[0]:message="message1"
2687 - // +k8s:validation:cel[0]:fieldPath="field"
2688 - type Blah struct {
2689 - // +k8s:validation:cel[0]:rule="self.length() > 0"
2690 - // +k8s:validation:cel[0]:message="string message"
2691 - // +k8s:validation:cel[0]:reason="Invalid"
2692 - // +k8s:validation:cel[1]:rule> !oldSelf.hasValue() || self.length() % 2 == 0
2693 - // +k8s:validation:cel[1]:rule> ? self.field == "even"
2694 - // +k8s:validation:cel[1]:rule> : self.field == "odd"
2695 - // +k8s:validation:cel[1]:messageExpression="field must be whether the length of the string is even or odd"
2696 - // +k8s:validation:cel[1]:optionalOldSelf
2697 - // +optional
2698 - Field string
2699 - }`
2700 -
2701 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2702 - e := packagestest.Export(t, x, []packagestest.Module{{
2703 - Name: "example.com/base/foo",
2704 - Files: map[string]interface{}{
2705 - "foo.go": inputFile,
2706 - },
2707 - }})
2708 - defer e.Cleanup()
2709 -
2710 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
2711 - if funcErr != nil {
2712 - t.Fatalf("Unexpected funcErr: %v", funcErr)
2713 - }
2714 - if callErr != nil {
2715 - t.Fatalf("Unexpected callErr: %v", callErr)
2716 - }
2717 - expImports := []string{
2718 - `foo "example.com/base/foo"`,
2719 - `common "k8s.io/kube-openapi/pkg/common"`,
2720 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
2721 - }
2722 - if !cmp.Equal(imports, expImports) {
2723 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
2724 - }
2725 -
2726 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
2727 - t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes()))
2728 - } else {
2729 - formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2730 - return common.OpenAPIDefinition{
2731 - Schema: spec.Schema{
2732 - SchemaProps: spec.SchemaProps{
2733 - Type: []string{"object"},
2734 - Properties: map[string]spec.Schema{
2735 - "Field": {
2736 - VendorExtensible: spec.VendorExtensible{
2737 - Extensions: spec.Extensions{
2738 - "x-kubernetes-validations": []interface{}{map[string]interface{}{"message": "string message", "reason": "Invalid", "rule": "self.length() > 0"}, map[string]interface{}{"messageExpression": "field must be whether the length of the string is even or odd", "optionalOldSelf": true, "rule": "!oldSelf.hasValue() || self.length() % 2 == 0\n? self.field == \"even\"\n: self.field == \"odd\""}},
2739 - },
2740 - },
2741 - SchemaProps: spec.SchemaProps{
2742 - Default: "",
2743 - Type: []string{"string"},
2744 - Format: "",
2745 - },
2746 - },
2747 - },
2748 - },
2749 - VendorExtensible: spec.VendorExtensible{
2750 - Extensions: spec.Extensions{
2751 - "x-kubernetes-validations": []interface{}{map[string]interface{}{"fieldPath": "field", "message": "message1", "rule": "self == oldSelf"}},
2752 - },
2753 - },
2754 - },
2755 - }
2756 - }`))
2757 - if ree != nil {
2758 - t.Fatal(ree)
2759 - }
2760 - assertEqual(t, string(formatted_expected), string(formatted))
2761 - }
2762 - })
2763 -}
2764 -
2765 -func TestRequired(t *testing.T) {
2766 - inputFile := `
2767 - package foo
2768 -
2769 - // +k8s:openapi-gen=true
2770 - type Blah struct {
2771 - // +optional
2772 - OptionalField string
2773 -
2774 - // +required
2775 - RequiredField string
2776 -
2777 - // +required
2778 - RequiredPointerField *string ` + "`json:\"requiredPointerField,omitempty\"`" + `
2779 -
2780 - // +optional
2781 - OptionalPointerField *string ` + "`json:\"optionalPointerField,omitempty\"`" + `
2782 -
2783 - ImplicitlyRequiredField string
2784 - ImplicitlyOptionalField string ` + "`json:\"implicitlyOptionalField,omitempty\"`" + `
2785 - }`
2786 -
2787 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2788 - e := packagestest.Export(t, x, []packagestest.Module{{
2789 - Name: "example.com/base/foo",
2790 - Files: map[string]interface{}{
2791 - "foo.go": inputFile,
2792 - },
2793 - }})
2794 - defer e.Cleanup()
2795 -
2796 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
2797 - if funcErr != nil {
2798 - t.Fatalf("Unexpected funcErr: %v", funcErr)
2799 - }
2800 - if callErr != nil {
2801 - t.Fatalf("Unexpected callErr: %v", callErr)
2802 - }
2803 - expImports := []string{
2804 - `foo "example.com/base/foo"`,
2805 - `common "k8s.io/kube-openapi/pkg/common"`,
2806 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
2807 - }
2808 - if !cmp.Equal(imports, expImports) {
2809 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
2810 - }
2811 -
2812 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
2813 - t.Fatalf("%v\n%v", err, string(funcBuffer.Bytes()))
2814 - } else {
2815 - formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2816 - return common.OpenAPIDefinition{
2817 - Schema: spec.Schema{
2818 - SchemaProps: spec.SchemaProps{
2819 - Type: []string{"object"},
2820 - Properties: map[string]spec.Schema{
2821 - "OptionalField": {
2822 - SchemaProps: spec.SchemaProps{
2823 - Default: "",
2824 - Type: []string{"string"},
2825 - Format: "",
2826 - },
2827 - },
2828 - "RequiredField": {
2829 - SchemaProps: spec.SchemaProps{
2830 - Default: "",
2831 - Type: []string{"string"},
2832 - Format: "",
2833 - },
2834 - },
2835 - "requiredPointerField": {
2836 - SchemaProps: spec.SchemaProps{
2837 - Type: []string{"string"},
2838 - Format: "",
2839 - },
2840 - },
2841 - "optionalPointerField": {
2842 - SchemaProps: spec.SchemaProps{
2843 - Type: []string{"string"},
2844 - Format: "",
2845 - },
2846 - },
2847 - "ImplicitlyRequiredField": {
2848 - SchemaProps: spec.SchemaProps{
2849 - Default: "",
2850 - Type: []string{"string"},
2851 - Format: "",
2852 - },
2853 - },
2854 - "implicitlyOptionalField": {
2855 - SchemaProps: spec.SchemaProps{
2856 - Type: []string{"string"},
2857 - Format: "",
2858 - },
2859 - },
2860 - },
2861 - Required: []string{"RequiredField", "requiredPointerField", "ImplicitlyRequiredField"},
2862 - },
2863 - },
2864 - }
2865 - }`))
2866 - if ree != nil {
2867 - t.Fatal(ree)
2868 - }
2869 - assertEqual(t, string(formatted_expected), string(formatted))
2870 - }
2871 - })
2872 -
2873 - // Show specifying both is an error
2874 - badFile := `
2875 - package foo
2876 -
2877 - // +k8s:openapi-gen=true
2878 - type Blah struct {
2879 - // +optional
2880 - // +required
2881 - ConfusingField string
2882 - }`
2883 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2884 - e := packagestest.Export(t, x, []packagestest.Module{{
2885 - Name: "example.com/base/foo",
2886 - Files: map[string]interface{}{
2887 - "foo.go": badFile,
2888 - },
2889 - }})
2890 - defer e.Cleanup()
2891 -
2892 - callErr, funcErr, _, _, _ := testOpenAPITypeWriter(t, e.Config)
2893 - if callErr != nil {
2894 - t.Errorf("Unexpected callErr: %v", callErr)
2895 - }
2896 - if funcErr == nil {
2897 - t.Fatalf("Expected funcErr")
2898 - }
2899 - if !strings.Contains(funcErr.Error(), "cannot be both optional and required") {
2900 - t.Errorf("Unexpected error: %v", funcErr)
2901 - }
2902 - })
2903 -}
2904 -
2905 -func TestMarkerCommentsCustomDefsV3(t *testing.T) {
2906 - inputFile := `
2907 - package foo
2908 -
2909 - import openapi "k8s.io/kube-openapi/pkg/common"
2910 -
2911 - // +k8s:validation:maxProperties=10
2912 - type Blah struct {
2913 - }
2914 -
2915 - func (_ Blah) OpenAPIV3Definition() openapi.OpenAPIDefinition {
2916 - return openapi.OpenAPIDefinition{
2917 - Schema: spec.Schema{
2918 - SchemaProps: spec.SchemaProps{
2919 - Type: []string{"object"},
2920 - MaxProperties: ptr.To[int64](10),
2921 - Format: "ipv4",
2922 - },
2923 - },
2924 - }
2925 - }
2926 -
2927 - func (_ Blah) OpenAPISchemaType() []string { return []string{"object"} }
2928 - func (_ Blah) OpenAPISchemaFormat() string { return "ipv4" }`
2929 - commonFile := `package common
2930 -
2931 - type OpenAPIDefinition struct {}`
2932 -
2933 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2934 - e := packagestest.Export(t, x, []packagestest.Module{{
2935 - Name: "example.com/base/foo",
2936 - Files: map[string]interface{}{
2937 - "foo.go": inputFile,
2938 - },
2939 - }, {
2940 - Name: "k8s.io/kube-openapi/pkg/common",
2941 - Files: map[string]interface{}{
2942 - "common.go": commonFile,
2943 - },
2944 - }})
2945 - defer e.Cleanup()
2946 -
2947 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
2948 - if callErr != nil {
2949 - t.Fatal(callErr)
2950 - }
2951 - if funcErr != nil {
2952 - t.Fatal(funcErr)
2953 - }
2954 - assertEqual(t, callBuffer.String(),
2955 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
2956 - assertEqual(t, funcBuffer.String(),
2957 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
2958 -return common.EmbedOpenAPIDefinitionIntoV2Extension(foo.Blah{}.OpenAPIV3Definition(), common.OpenAPIDefinition{
2959 -Schema: spec.Schema{
2960 -SchemaProps: spec.SchemaProps{
2961 -Type:foo.Blah{}.OpenAPISchemaType(),
2962 -Format:foo.Blah{}.OpenAPISchemaFormat(),
2963 -MaxProperties: ptr.To[int64](10),
2964 -},
2965 -},
2966 -})
2967 -}`)
2968 - })
2969 -}
2970 -
2971 -func TestMarkerCommentsV3OneOfTypes(t *testing.T) {
2972 - inputFile := `
2973 - package foo
2974 -
2975 - // +k8s:validation:maxLength=10
2976 - type Blah struct {
2977 - }
2978 -
2979 - func (_ Blah) OpenAPISchemaType() []string { return []string{"string"} }
2980 - func (_ Blah) OpenAPIV3OneOfTypes() []string { return []string{"string", "array"} }
2981 - func (_ Blah) OpenAPISchemaFormat() string { return "ipv4" }`
2982 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
2983 - e := packagestest.Export(t, x, []packagestest.Module{{
2984 - Name: "example.com/base/foo",
2985 - Files: map[string]interface{}{
2986 - "foo.go": inputFile,
2987 - },
2988 - }})
2989 - defer e.Cleanup()
2990 -
2991 - callErr, funcErr, callBuffer, funcBuffer, _ := testOpenAPITypeWriter(t, e.Config)
2992 - if callErr != nil {
2993 - t.Fatal(callErr)
2994 - }
2995 - if funcErr != nil {
2996 - t.Fatal(funcErr)
2997 - }
2998 - assertEqual(t, callBuffer.String(),
2999 - `"example.com/base/foo.Blah": schema_examplecom_base_foo_Blah(ref),`)
3000 - assertEqual(t, funcBuffer.String(),
3001 - `func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
3002 -return common.EmbedOpenAPIDefinitionIntoV2Extension(common.OpenAPIDefinition{
3003 -Schema: spec.Schema{
3004 -SchemaProps: spec.SchemaProps{
3005 -OneOf:common.GenerateOpenAPIV3OneOfSchema(foo.Blah{}.OpenAPIV3OneOfTypes()),
3006 -Format:foo.Blah{}.OpenAPISchemaFormat(),
3007 -MaxLength: ptr.To[int64](10),
3008 -},
3009 -},
3010 -},common.OpenAPIDefinition{
3011 -Schema: spec.Schema{
3012 -SchemaProps: spec.SchemaProps{
3013 -Type:foo.Blah{}.OpenAPISchemaType(),
3014 -Format:foo.Blah{}.OpenAPISchemaFormat(),
3015 -MaxLength: ptr.To[int64](10),
3016 -},
3017 -},
3018 -})
3019 -}`)
3020 - })
3021 -}
3022 -
3023 -func TestNestedMarkers(t *testing.T) {
3024 - inputFile := `
3025 - package foo
3026 -
3027 - // +k8s:openapi-gen=true
3028 - // +k8s:validation:properties:field:items:maxLength=10
3029 - // +k8s:validation:properties:aliasMap:additionalProperties:pattern>^foo$
3030 - type Blah struct {
3031 - // +k8s:validation:items:cel[0]:rule="self.length() % 2 == 0"
3032 - Field MyAlias ` + "`json:\"field,omitempty\"`" + `
3033 -
3034 - // +k8s:validation:additionalProperties:maxLength=10
3035 - AliasMap MyAliasMap ` + "`json:\"aliasMap,omitempty\"`" + `
3036 - }
3037 -
3038 - type MyAliasMap map[string]MyAlias
3039 - type MyAlias []string`
3040 -
3041 - packagestest.TestAll(t, func(t *testing.T, x packagestest.Exporter) {
3042 - e := packagestest.Export(t, x, []packagestest.Module{{
3043 - Name: "example.com/base/foo",
3044 - Files: map[string]interface{}{
3045 - "foo.go": inputFile,
3046 - },
3047 - }})
3048 - defer e.Cleanup()
3049 -
3050 - callErr, funcErr, _, funcBuffer, imports := testOpenAPITypeWriter(t, e.Config)
3051 - if funcErr != nil {
3052 - t.Fatalf("Unexpected funcErr: %v", funcErr)
3053 - }
3054 - if callErr != nil {
3055 - t.Fatalf("Unexpected callErr: %v", callErr)
3056 - }
3057 - expImports := []string{
3058 - `foo "example.com/base/foo"`,
3059 - `common "k8s.io/kube-openapi/pkg/common"`,
3060 - `spec "k8s.io/kube-openapi/pkg/validation/spec"`,
3061 - `ptr "k8s.io/utils/ptr"`,
3062 - }
3063 - if !cmp.Equal(imports, expImports) {
3064 - t.Errorf("wrong imports:\n%s", cmp.Diff(expImports, imports))
3065 - }
3066 -
3067 - if formatted, err := format.Source(funcBuffer.Bytes()); err != nil {
3068 - t.Fatalf("%v\n%v", err, funcBuffer.String())
3069 - } else {
3070 - formatted_expected, ree := format.Source([]byte(`func schema_examplecom_base_foo_Blah(ref common.ReferenceCallback) common.OpenAPIDefinition {
3071 - return common.OpenAPIDefinition{
3072 - Schema: spec.Schema{
3073 - SchemaProps: spec.SchemaProps{
3074 - Type: []string{"object"},
3075 - AllOf: []spec.Schema{
3076 - {
3077 - SchemaProps: spec.SchemaProps{
3078 - Properties: map[string]spec.Schema{
3079 - "aliasMap": {
3080 - SchemaProps: spec.SchemaProps{
3081 - AllOf: []spec.Schema{
3082 - {
3083 - SchemaProps: spec.SchemaProps{
3084 - AdditionalProperties: &spec.SchemaOrBool{
3085 - Allows: true,
3086 - Schema: &spec.Schema{
3087 - SchemaProps: spec.SchemaProps{
3088 - Pattern: "^foo$",
3089 - },
3090 - },
3091 - },
3092 - },
3093 - },
3094 - },
3095 - },
3096 - },
3097 - "field": {
3098 - SchemaProps: spec.SchemaProps{
3099 - AllOf: []spec.Schema{
3100 - {
3101 - SchemaProps: spec.SchemaProps{
3102 - Items: &spec.SchemaOrArray{
3103 - Schema: &spec.Schema{
3104 - SchemaProps: spec.SchemaProps{
3105 - MaxLength: ptr.To[int64](10),
3106 - },
3107 - },
3108 - },
3109 - },
3110 - },
3111 - },
3112 - },
3113 - },
3114 - },
3115 - },
3116 - },
3117 - },
3118 - Properties: map[string]spec.Schema{
3119 - "field": {
3120 - SchemaProps: spec.SchemaProps{
3121 - AllOf: []spec.Schema{
3122 - {
3123 - SchemaProps: spec.SchemaProps{
3124 - Items: &spec.SchemaOrArray{
3125 - Schema: &spec.Schema{
3126 - VendorExtensible: spec.VendorExtensible{
3127 - Extensions: spec.Extensions{
3128 - "x-kubernetes-validations": []interface{}{map[string]interface{}{"rule": "self.length() % 2 == 0"}},
3129 - },
3130 - },
3131 - },
3132 - },
3133 - },
3134 - },
3135 - },
3136 - Type: []string{"array"},
3137 - Items: &spec.SchemaOrArray{
3138 - Schema: &spec.Schema{
3139 - SchemaProps: spec.SchemaProps{
3140 - Type: []string{"string"},
3141 - Format: "",
3142 - },
3143 - },
3144 - },
3145 - },
3146 - },
3147 - "aliasMap": {
3148 - SchemaProps: spec.SchemaProps{
3149 - AllOf: []spec.Schema{
3150 - {
3151 - SchemaProps: spec.SchemaProps{
3152 - AdditionalProperties: &spec.SchemaOrBool{
3153 - Allows: true,
3154 - Schema: &spec.Schema{
3155 - SchemaProps: spec.SchemaProps{
3156 - MaxLength: ptr.To[int64](10),
3157 - },
3158 - },
3159 - },
3160 - },
3161 - },
3162 - },
3163 - Type: []string{"object"},
3164 - AdditionalProperties: &spec.SchemaOrBool{
3165 - Allows: true,
3166 - Schema: &spec.Schema{
3167 - SchemaProps: spec.SchemaProps{
3168 - Type: []string{"array"},
3169 - Items: &spec.SchemaOrArray{
3170 - Schema: &spec.Schema{
3171 - SchemaProps: spec.SchemaProps{
3172 - Type: []string{"string"},
3173 - Format: "",
3174 - },
3175 - },
3176 - },
3177 - },
3178 - },
3179 - },
3180 - },
3181 - },
3182 - },
3183 - },
3184 - },
3185 - }
3186 - }`))
3187 - if ree != nil {
3188 - t.Fatal(ree)
3189 - }
3190 - assertEqual(t, string(formatted), string(formatted_expected))
3191 - }
3192 - })
3193 -
3194 -}
3195 -
3196 -func TestShouldInlineMembers(t *testing.T) {
3197 - tests := []struct {
3198 - name string
3199 - member types.Member
3200 - expected bool
3201 - }{
3202 - {
3203 - name: "embedded with ,inline tag",
3204 - member: types.Member{Name: "TypeMeta", Embedded: true, Tags: `json:",inline"`},
3205 - expected: true,
3206 - },
3207 - {
3208 - name: "embedded with empty json tag",
3209 - member: types.Member{Name: "TypeMeta", Embedded: true, Tags: `json:""`},
3210 - expected: true,
3211 - },
3212 - {
3213 - name: "embedded with ,inline,omitempty tag",
3214 - member: types.Member{Name: "ObjectMeta", Embedded: true, Tags: `json:",inline,omitempty"`},
3215 - expected: true,
3216 - },
3217 - {
3218 - name: "embedded with no json tag",
3219 - member: types.Member{Name: "TypeMeta", Embedded: true, Tags: ``},
3220 - expected: false,
3221 - },
3222 - {
3223 - name: "embedded with explicit name",
3224 - member: types.Member{Name: "TypeMeta", Embedded: true, Tags: `json:"typemeta"`},
3225 - expected: false,
3226 - },
3227 - {
3228 - name: "non-embedded with ,inline tag",
3229 - member: types.Member{Name: "TypeMeta", Embedded: false, Tags: `json:",inline"`},
3230 - expected: false,
3231 - },
3232 - {
3233 - name: "non-embedded with empty json tag",
3234 - member: types.Member{Name: "TypeMeta", Embedded: false, Tags: `json:""`},
3235 - expected: false,
3236 - },
3237 - }
3238 -
3239 - for _, tc := range tests {
3240 - t.Run(tc.name, func(t *testing.T) {
3241 - if got := shouldInlineMembers(&tc.member); got != tc.expected {
3242 - t.Errorf("shouldInlineMembers(%+v) = %v, want %v", tc.member, got, tc.expected)
3243 - }
3244 - })
3245 - }
3246 -}
3247 -
3248 -func TestHasOmitemptyTag(t *testing.T) {
3249 - tests := []struct {
3250 - name string
3251 - member types.Member
3252 - expected bool
3253 - }{
3254 - {
3255 - name: "standard omitempty",
3256 - member: types.Member{Tags: `json:"foo,omitempty"`},
3257 - expected: true,
3258 - },
3259 - {
3260 - name: "omitempty with no name",
3261 - member: types.Member{Tags: `json:",omitempty"`},
3262 - expected: true,
3263 - },
3264 - {
3265 - name: "omitempty in the middle of options",
3266 - member: types.Member{Tags: `json:"foo,omitempty,string"`},
3267 - expected: true,
3268 - },
3269 - {
3270 - name: "no omitempty",
3271 - member: types.Member{Tags: `json:"foo"`},
3272 - expected: false,
3273 - },
3274 - {
3275 - name: "no json tag",
3276 - member: types.Member{Tags: ``},
3277 - expected: false,
3278 - },
3279 - {
3280 - name: "field name contains omitempty - should not match",
3281 - member: types.Member{Tags: `json:"omitemptyTest"`},
3282 - expected: false,
3283 - },
3284 - {
3285 - name: "field name ends with omitempty - should not match",
3286 - member: types.Member{Tags: `json:"fooOmitempty"`},
3287 - expected: false,
3288 - },
3289 - {
3290 - name: "field name is omitempty with no options - should not match",
3291 - member: types.Member{Tags: `json:"omitempty"`},
3292 - expected: false,
3293 - },
3294 - }
3295 -
3296 - for _, tc := range tests {
3297 - t.Run(tc.name, func(t *testing.T) {
3298 - if got := hasOmitemptyTag(&tc.member); got != tc.expected {
3299 - t.Errorf("hasOmitemptyTag(%+v) = %v, want %v", tc.member, got, tc.expected)
3300 - }
3301 - })
3302 - }
3303 -}