Debian Codegraph @

pkg/aggregator/aggregator_test.go

raw
1 /*
2 Copyright 2017 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package aggregator
18
19 import (
20 "encoding/json"
21 "fmt"
22 "os"
23 "path/filepath"
24 "reflect"
25 "testing"
26
27 "github.com/stretchr/testify/assert"
28 "github.com/stretchr/testify/require"
29
30 "k8s.io/kube-openapi/pkg/handler"
31 "k8s.io/kube-openapi/pkg/validation/spec"
32 "sigs.k8s.io/yaml"
33 )
34
35 type DebugSpec struct {
36 *spec.Swagger
37 }
38
39 func (d DebugSpec) String() string {
40 bytes, err := d.Swagger.MarshalJSON()
41 if err != nil {
42 return fmt.Sprintf("DebugSpec.String failed: %s", err)
43 }
44 return string(bytes)
45 }
46 func TestFilterSpecs(t *testing.T) {
47 var spec1, spec1_filtered *spec.Swagger
48 ast := assert.New(t)
49
50 err := yaml.Unmarshal([]byte(`
51 swagger: "2.0"
52 paths:
53 /test:
54 post:
55 tags:
56 - "test"
57 summary: "Test API"
58 operationId: "addTest"
59 parameters:
60 - in: "body"
61 name: "body"
62 description: "test object"
63 required: true
64 schema:
65 $ref: "#/definitions/Test"
66 responses:
67 405:
68 description: "Invalid input"
69 $ref: "#/definitions/InvalidInput"
70 delete:
71 summary: "Test API Delete"
72 operationId: "deleteTest"
73 parameters:
74 - $ref: "#/parameters/body-deleteoptions"
75 /othertest:
76 post:
77 tags:
78 - "test2"
79 summary: "Test2 API"
80 operationId: "addTest2"
81 consumes:
82 - "application/json"
83 produces:
84 - "application/xml"
85 parameters:
86 - in: "body"
87 name: "body"
88 description: "test2 object"
89 required: true
90 schema:
91 $ref: "#/definitions/Test2"
92 delete:
93 summary: "Test2 API Delete"
94 operationId: "deleteTest2"
95 parameters:
96 - schema:
97 $ref: "#/definitions/DeleteOptions"
98 definitions:
99 Test:
100 type: "object"
101 properties:
102 id:
103 type: "integer"
104 format: "int64"
105 status:
106 type: "string"
107 description: "Status"
108 InvalidInput:
109 type: "string"
110 format: "string"
111 Test2:
112 type: "object"
113 properties:
114 other:
115 $ref: "#/definitions/Other"
116 Other:
117 type: "string"
118 DeleteOptions:
119 type: "object"
120 properties:
121 preconditions:
122 $ref: "#/definitions/Preconditions"
123 Preconditions:
124 type: "string"
125 parameters:
126 body-deleteoptions:
127 name: body
128 in: body
129 schema:
130 $ref: "#/definitions/DeleteOptions"
131 `), &spec1)
132
133 ast.NoError(err)
134
135 err = yaml.Unmarshal([]byte(`
136 swagger: "2.0"
137 paths:
138 /test:
139 post:
140 tags:
141 - "test"
142 summary: "Test API"
143 operationId: "addTest"
144 parameters:
145 - in: "body"
146 name: "body"
147 description: "test object"
148 required: true
149 schema:
150 $ref: "#/definitions/Test"
151 responses:
152 405:
153 description: "Invalid input"
154 $ref: "#/definitions/InvalidInput"
155 delete:
156 summary: "Test API Delete"
157 operationId: "deleteTest"
158 parameters:
159 - $ref: "#/parameters/body-deleteoptions"
160 definitions:
161 Test:
162 type: "object"
163 properties:
164 id:
165 type: "integer"
166 format: "int64"
167 status:
168 type: "string"
169 description: "Status"
170 InvalidInput:
171 type: "string"
172 format: "string"
173 DeleteOptions:
174 type: "object"
175 properties:
176 preconditions:
177 $ref: "#/definitions/Preconditions"
178 Preconditions:
179 type: "string"
180 parameters:
181 body-deleteoptions:
182 name: body
183 in: body
184 schema:
185 $ref: "#/definitions/DeleteOptions"
186 `), &spec1_filtered)
187 ast.NoError(err)
188
189 orig_spec1, _ := cloneSpec(spec1)
190 new_spec1 := FilterSpecByPathsWithoutSideEffects(spec1, []string{"/test"})
191 ast.Equal(DebugSpec{spec1_filtered}, DebugSpec{new_spec1})
192 ast.Equal(DebugSpec{orig_spec1}, DebugSpec{spec1}, "unexpected mutation of input")
193 }
194
195 func TestFilterSpecsWithUnusedDefinitions(t *testing.T) {
196 var spec1, spec1Filtered *spec.Swagger
197 yaml.Unmarshal([]byte(`
198 swagger: "2.0"
199 paths:
200 /test:
201 post:
202 tags:
203 - "test"
204 summary: "Test API"
205 operationId: "addTest"
206 parameters:
207 - in: "body"
208 name: "body"
209 description: "test object"
210 required: true
211 schema:
212 $ref: "#/definitions/Test"
213 responses:
214 405:
215 description: "Invalid input"
216 $ref: "#/definitions/InvalidInput"
217 /othertest:
218 post:
219 tags:
220 - "test2"
221 summary: "Test2 API"
222 operationId: "addTest2"
223 consumes:
224 - "application/json"
225 produces:
226 - "application/xml"
227 parameters:
228 - in: "body"
229 name: "body"
230 description: "test2 object"
231 required: true
232 schema:
233 $ref: "#/definitions/Test2"
234 definitions:
235 Test:
236 type: "object"
237 properties:
238 id:
239 type: "integer"
240 format: "int64"
241 status:
242 type: "string"
243 description: "Status"
244 InvalidInput:
245 type: "string"
246 format: "string"
247 Test2:
248 type: "object"
249 properties:
250 other:
251 $ref: "#/definitions/Other"
252 Other:
253 type: "string"
254 Unused:
255 type: "object"
256 `), &spec1)
257
258 yaml.Unmarshal([]byte(`
259 swagger: "2.0"
260 paths:
261 /test:
262 post:
263 tags:
264 - "test"
265 summary: "Test API"
266 operationId: "addTest"
267 parameters:
268 - in: "body"
269 name: "body"
270 description: "test object"
271 required: true
272 schema:
273 $ref: "#/definitions/Test"
274 responses:
275 405:
276 description: "Invalid input"
277 $ref: "#/definitions/InvalidInput"
278 definitions:
279 Test:
280 type: "object"
281 properties:
282 id:
283 type: "integer"
284 format: "int64"
285 status:
286 type: "string"
287 description: "Status"
288 InvalidInput:
289 type: "string"
290 format: "string"
291 Unused:
292 type: "object"
293 `), &spec1Filtered)
294
295 ast := assert.New(t)
296 orig_spec1, _ := cloneSpec(spec1)
297 new_spec1 := FilterSpecByPathsWithoutSideEffects(spec1, []string{"/test"})
298 ast.Equal(DebugSpec{spec1Filtered}, DebugSpec{new_spec1})
299 ast.Equal(DebugSpec{orig_spec1}, DebugSpec{spec1}, "unexpected mutation of input")
300 }
301
302 func TestMergeSpecsSimple(t *testing.T) {
303 var spec1, spec2, expected *spec.Swagger
304 require.NoError(t, yaml.Unmarshal([]byte(`
305 swagger: "2.0"
306 paths:
307 /test:
308 post:
309 tags:
310 - "test"
311 summary: "Test API"
312 operationId: "addTest"
313 parameters:
314 - in: "body"
315 name: "body"
316 description: "test object"
317 required: true
318 schema:
319 $ref: "#/definitions/Test"
320 - $ref: "#/parameters/a"
321 responses:
322 405:
323 description: "Invalid input"
324 $ref: "#/definitions/InvalidInput"
325 definitions:
326 Test:
327 type: "object"
328 properties:
329 id:
330 type: "integer"
331 format: "int64"
332 status:
333 type: "string"
334 description: "Status"
335 InvalidInput:
336 type: "string"
337 format: "string"
338 parameters:
339 a:
340 in: query
341 name: a
342 schema:
343 $ref: "#/definitions/Test"
344 `), &spec1))
345
346 require.NoError(t, yaml.Unmarshal([]byte(`
347 swagger: "2.0"
348 paths:
349 /othertest:
350 post:
351 tags:
352 - "test2"
353 summary: "Test2 API"
354 operationId: "addTest2"
355 consumes:
356 - "application/json"
357 produces:
358 - "application/xml"
359 parameters:
360 - in: "body"
361 name: "body"
362 description: "test2 object"
363 required: true
364 schema:
365 $ref: "#/definitions/Test2"
366 - $ref: "#/parameters/b"
367 definitions:
368 Test2:
369 type: "object"
370 properties:
371 other:
372 $ref: "#/definitions/Other"
373 Other:
374 type: "string"
375 parameters:
376 b:
377 in: query
378 name: b
379 schema:
380 $ref: "#/definitions/Test2"
381 `), &spec2))
382
383 require.NoError(t, yaml.Unmarshal([]byte(`
384 swagger: "2.0"
385 paths:
386 /test:
387 post:
388 tags:
389 - "test"
390 summary: "Test API"
391 operationId: "addTest"
392 parameters:
393 - in: "body"
394 name: "body"
395 description: "test object"
396 required: true
397 schema:
398 $ref: "#/definitions/Test"
399 - $ref: "#/parameters/a"
400 responses:
401 405:
402 description: "Invalid input"
403 $ref: "#/definitions/InvalidInput"
404 /othertest:
405 post:
406 tags:
407 - "test2"
408 summary: "Test2 API"
409 operationId: "addTest2"
410 consumes:
411 - "application/json"
412 produces:
413 - "application/xml"
414 parameters:
415 - in: "body"
416 name: "body"
417 description: "test2 object"
418 required: true
419 schema:
420 $ref: "#/definitions/Test2"
421 - $ref: "#/parameters/b"
422 definitions:
423 Test:
424 type: "object"
425 properties:
426 id:
427 type: "integer"
428 format: "int64"
429 status:
430 type: "string"
431 description: "Status"
432 InvalidInput:
433 type: "string"
434 format: "string"
435 Test2:
436 type: "object"
437 properties:
438 other:
439 $ref: "#/definitions/Other"
440 Other:
441 type: "string"
442 parameters:
443 a:
444 in: query
445 name: a
446 schema:
447 $ref: "#/definitions/Test"
448 b:
449 in: query
450 name: b
451 schema:
452 $ref: "#/definitions/Test2"
453 `), &expected))
454
455 ast := assert.New(t)
456 orig_spec2, _ := cloneSpec(spec2)
457 if !ast.NoError(MergeSpecs(spec1, spec2)) {
458 return
459 }
460 ast.Equal(DebugSpec{expected}.String(), DebugSpec{spec1}.String())
461 ast.Equal(DebugSpec{orig_spec2}.String(), DebugSpec{spec2}.String(), "unexpected mutation of input")
462 }
463
464 func TestMergeSpecsEmptyDefinitions(t *testing.T) {
465 var spec1, spec2, expected *spec.Swagger
466 require.NoError(t, yaml.Unmarshal([]byte(`
467 swagger: "2.0"
468 paths:
469 /test:
470 post:
471 tags:
472 - "test"
473 summary: "Test API"
474 operationId: "addTest"
475 parameters:
476 - in: "body"
477 name: "body"
478 description: "test object"
479 required: true
480 responses:
481 405:
482 description: "Invalid input"
483 `), &spec1))
484
485 require.NoError(t, yaml.Unmarshal([]byte(`
486 swagger: "2.0"
487 paths:
488 /othertest:
489 post:
490 tags:
491 - "test2"
492 summary: "Test2 API"
493 operationId: "addTest2"
494 consumes:
495 - "application/json"
496 produces:
497 - "application/xml"
498 parameters:
499 - in: "body"
500 name: "body"
501 description: "test2 object"
502 required: true
503 schema:
504 $ref: "#/definitions/Test2"
505 definitions:
506 Test2:
507 type: "object"
508 properties:
509 other:
510 $ref: "#/definitions/Other"
511 Other:
512 type: "string"
513 `), &spec2))
514
515 require.NoError(t, yaml.Unmarshal([]byte(`
516 swagger: "2.0"
517 paths:
518 /test:
519 post:
520 tags:
521 - "test"
522 summary: "Test API"
523 operationId: "addTest"
524 parameters:
525 - in: "body"
526 name: "body"
527 description: "test object"
528 required: true
529 responses:
530 405:
531 description: "Invalid input"
532 /othertest:
533 post:
534 tags:
535 - "test2"
536 summary: "Test2 API"
537 operationId: "addTest2"
538 consumes:
539 - "application/json"
540 produces:
541 - "application/xml"
542 parameters:
543 - in: "body"
544 name: "body"
545 description: "test2 object"
546 required: true
547 schema:
548 $ref: "#/definitions/Test2"
549 definitions:
550 Test2:
551 type: "object"
552 properties:
553 other:
554 $ref: "#/definitions/Other"
555 Other:
556 type: "string"
557 `), &expected))
558
559 ast := assert.New(t)
560 orig_spec2, _ := cloneSpec(spec2)
561 if !ast.NoError(MergeSpecs(spec1, spec2)) {
562 return
563 }
564 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
565 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
566 }
567
568 func TestMergeSpecsEmptyPaths(t *testing.T) {
569 var spec1, spec2, expected *spec.Swagger
570 yaml.Unmarshal([]byte(`
571 swagger: "2.0"
572 definitions:
573 Test:
574 type: "object"
575 properties:
576 id:
577 type: "integer"
578 format: "int64"
579 status:
580 type: "string"
581 description: "Status"
582 InvalidInput:
583 type: "string"
584 format: "string"
585 `), &spec1)
586
587 yaml.Unmarshal([]byte(`
588 swagger: "2.0"
589 paths:
590 /othertest:
591 post:
592 tags:
593 - "test2"
594 summary: "Test2 API"
595 operationId: "addTest2"
596 consumes:
597 - "application/json"
598 produces:
599 - "application/xml"
600 parameters:
601 - in: "body"
602 name: "body"
603 description: "test2 object"
604 required: true
605 schema:
606 $ref: "#/definitions/Test2"
607 definitions:
608 Test2:
609 type: "object"
610 properties:
611 other:
612 $ref: "#/definitions/Other"
613 Other:
614 type: "string"
615 `), &spec2)
616
617 yaml.Unmarshal([]byte(`
618 swagger: "2.0"
619 paths:
620 /othertest:
621 post:
622 tags:
623 - "test2"
624 summary: "Test2 API"
625 operationId: "addTest2"
626 consumes:
627 - "application/json"
628 produces:
629 - "application/xml"
630 parameters:
631 - in: "body"
632 name: "body"
633 description: "test2 object"
634 required: true
635 schema:
636 $ref: "#/definitions/Test2"
637 definitions:
638 Test:
639 type: "object"
640 properties:
641 id:
642 type: "integer"
643 format: "int64"
644 status:
645 type: "string"
646 description: "Status"
647 InvalidInput:
648 type: "string"
649 format: "string"
650 Test2:
651 type: "object"
652 properties:
653 other:
654 $ref: "#/definitions/Other"
655 Other:
656 type: "string"
657 `), &expected)
658
659 ast := assert.New(t)
660 orig_spec2, _ := cloneSpec(spec2)
661 if !ast.NoError(MergeSpecs(spec1, spec2)) {
662 return
663 }
664 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
665 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
666 }
667
668 func TestMergeSpecsReuseModel(t *testing.T) {
669 var spec1, spec2, expected *spec.Swagger
670 require.NoError(t, yaml.Unmarshal([]byte(`
671 swagger: "2.0"
672 paths:
673 /test:
674 post:
675 tags:
676 - "test"
677 summary: "Test API"
678 operationId: "addTest"
679 parameters:
680 - in: "body"
681 name: "body"
682 description: "test object"
683 required: true
684 schema:
685 $ref: "#/definitions/Test"
686 - $ref: "#/parameters/a"
687 responses:
688 405:
689 description: "Invalid input"
690 $ref: "#/definitions/InvalidInput"
691 definitions:
692 Test:
693 type: "object"
694 properties:
695 id:
696 type: "integer"
697 format: "int64"
698 status:
699 type: "string"
700 description: "Status"
701 InvalidInput:
702 type: "string"
703 format: "string"
704 parameters:
705 a:
706 in: query
707 name: a
708 schema:
709 $ref: "#/definitions/Test"
710 `), &spec1))
711
712 require.NoError(t, yaml.Unmarshal([]byte(`
713 swagger: "2.0"
714 paths:
715 /othertest:
716 post:
717 tags:
718 - "test2"
719 summary: "Test2 API"
720 operationId: "addTest2"
721 consumes:
722 - "application/json"
723 produces:
724 - "application/xml"
725 parameters:
726 - in: "body"
727 name: "body"
728 description: "test2 object"
729 required: true
730 schema:
731 $ref: "#/definitions/Test"
732 - $ref: "#/parameters/a"
733 definitions:
734 Test:
735 type: "object"
736 properties:
737 id:
738 type: "integer"
739 format: "int64"
740 status:
741 type: "string"
742 description: "Status"
743 InvalidInput:
744 type: "string"
745 format: "string"
746 parameters:
747 a:
748 in: query
749 name: a
750 schema:
751 $ref: "#/definitions/Test"
752 `), &spec2))
753
754 require.NoError(t, yaml.Unmarshal([]byte(`
755 swagger: "2.0"
756 paths:
757 /test:
758 post:
759 tags:
760 - "test"
761 summary: "Test API"
762 operationId: "addTest"
763 parameters:
764 - in: "body"
765 name: "body"
766 description: "test object"
767 required: true
768 schema:
769 $ref: "#/definitions/Test"
770 - $ref: "#/parameters/a"
771 responses:
772 405:
773 description: "Invalid input"
774 $ref: "#/definitions/InvalidInput"
775 /othertest:
776 post:
777 tags:
778 - "test2"
779 summary: "Test2 API"
780 operationId: "addTest2"
781 consumes:
782 - "application/json"
783 produces:
784 - "application/xml"
785 parameters:
786 - in: "body"
787 name: "body"
788 description: "test2 object"
789 required: true
790 schema:
791 $ref: "#/definitions/Test"
792 - $ref: "#/parameters/a"
793 definitions:
794 Test:
795 type: "object"
796 properties:
797 id:
798 type: "integer"
799 format: "int64"
800 status:
801 type: "string"
802 description: "Status"
803 InvalidInput:
804 type: "string"
805 format: "string"
806 parameters:
807 a:
808 in: query
809 name: a
810 schema:
811 $ref: "#/definitions/Test"
812 `), &expected))
813
814 ast := assert.New(t)
815 orig_spec2, _ := cloneSpec(spec2)
816 if !ast.NoError(MergeSpecs(spec1, spec2)) {
817 return
818 }
819 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
820 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
821 }
822
823 func TestMergeSpecsRenameModel(t *testing.T) {
824 var spec1, spec2, expected *spec.Swagger
825 require.NoError(t, yaml.Unmarshal([]byte(`
826 swagger: "2.0"
827 paths:
828 /test:
829 post:
830 tags:
831 - "test"
832 summary: "Test API"
833 operationId: "addTest"
834 parameters:
835 - in: "body"
836 name: "body"
837 description: "test object"
838 required: true
839 schema:
840 $ref: "#/definitions/Test"
841 - $ref: "#/parameters/a"
842 responses:
843 405:
844 description: "Invalid input"
845 $ref: "#/definitions/InvalidInput"
846 definitions:
847 Test:
848 type: "object"
849 properties:
850 id:
851 type: "integer"
852 format: "int64"
853 status:
854 type: "string"
855 description: "Status"
856 InvalidInput:
857 type: "string"
858 format: "string"
859 parameters:
860 a:
861 in: query
862 name: a
863 schema:
864 $ref: "#/definitions/Test"
865 `), &spec1))
866
867 require.NoError(t, yaml.Unmarshal([]byte(`
868 swagger: "2.0"
869 paths:
870 /othertest:
871 post:
872 tags:
873 - "test2"
874 summary: "Test2 API"
875 operationId: "addTest2"
876 consumes:
877 - "application/json"
878 produces:
879 - "application/xml"
880 parameters:
881 - in: "body"
882 name: "body"
883 description: "test2 object"
884 required: true
885 schema:
886 $ref: "#/definitions/Test"
887 - $ref: "#/parameters/a"
888 definitions:
889 Test:
890 description: "This Test has a description"
891 type: "object"
892 properties:
893 id:
894 type: "integer"
895 format: "int64"
896 InvalidInput:
897 type: "string"
898 format: "string"
899 parameters:
900 a:
901 in: query
902 name: a
903 schema:
904 $ref: "#/definitions/Test"
905 `), &spec2))
906
907 require.NoError(t, yaml.Unmarshal([]byte(`
908 swagger: "2.0"
909 paths:
910 /test:
911 post:
912 tags:
913 - "test"
914 summary: "Test API"
915 operationId: "addTest"
916 parameters:
917 - in: "body"
918 name: "body"
919 description: "test object"
920 required: true
921 schema:
922 $ref: "#/definitions/Test"
923 - $ref: "#/parameters/a"
924 responses:
925 405:
926 description: "Invalid input"
927 $ref: "#/definitions/InvalidInput"
928 /othertest:
929 post:
930 tags:
931 - "test2"
932 summary: "Test2 API"
933 operationId: "addTest2"
934 consumes:
935 - "application/json"
936 produces:
937 - "application/xml"
938 parameters:
939 - in: "body"
940 name: "body"
941 description: "test2 object"
942 required: true
943 schema:
944 $ref: "#/definitions/Test_v2"
945 - $ref: "#/parameters/a_v2"
946 definitions:
947 Test:
948 type: "object"
949 properties:
950 id:
951 type: "integer"
952 format: "int64"
953 status:
954 type: "string"
955 description: "Status"
956 Test_v2:
957 description: "This Test has a description"
958 type: "object"
959 properties:
960 id:
961 type: "integer"
962 format: "int64"
963 InvalidInput:
964 type: "string"
965 format: "string"
966 parameters:
967 a:
968 in: query
969 name: a
970 schema:
971 $ref: "#/definitions/Test"
972 a_v2:
973 in: query
974 name: a
975 schema:
976 $ref: "#/definitions/Test_v2"
977 `), &expected))
978
979 ast := assert.New(t)
980 orig_spec2, _ := cloneSpec(spec2)
981 if !ast.NoError(MergeSpecs(spec1, spec2)) {
982 return
983 }
984 ast.Equal(DebugSpec{expected}, DebugSpec{spec1}, DebugSpec{spec1}.String())
985 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
986 }
987
988 func TestMergeSpecsRenameModelWithExistingV2InDestination(t *testing.T) {
989 var spec1, spec2, expected *spec.Swagger
990 require.NoError(t, yaml.Unmarshal([]byte(`
991 swagger: "2.0"
992 paths:
993 /test:
994 post:
995 parameters:
996 - name: "body"
997 schema:
998 $ref: "#/definitions/Test"
999 - $ref: "#/parameters/a"
1000 /testv2:
1001 post:
1002 parameters:
1003 - name: "body"
1004 schema:
1005 $ref: "#/definitions/Test_v2"
1006 - $ref: "#/parameters/a_v2"
1007 definitions:
1008 Test:
1009 type: "object"
1010 Test_v2:
1011 description: "This is an existing Test_v2 in destination schema"
1012 type: "object"
1013 parameters:
1014 a:
1015 in: query
1016 name: a
1017 schema:
1018 $ref: "#/definitions/Test"
1019 a_v2:
1020 in: query
1021 name: a
1022 schema:
1023 $ref: "#/definitions/Test_v2"
1024 `), &spec1))
1025
1026 require.NoError(t, yaml.Unmarshal([]byte(`
1027 swagger: "2.0"
1028 paths:
1029 /othertest:
1030 post:
1031 parameters:
1032 - name: "body"
1033 schema:
1034 $ref: "#/definitions/Test"
1035 - $ref: "#/parameters/a"
1036 definitions:
1037 Test:
1038 description: "This Test has a description"
1039 type: "object"
1040 parameters:
1041 a:
1042 in: query
1043 name: a
1044 schema:
1045 $ref: "#/definitions/Test"
1046 `), &spec2))
1047
1048 require.NoError(t, yaml.Unmarshal([]byte(`
1049 swagger: "2.0"
1050 paths:
1051 /test:
1052 post:
1053 parameters:
1054 - name: "body"
1055 schema:
1056 $ref: "#/definitions/Test"
1057 - $ref: "#/parameters/a"
1058 /testv2:
1059 post:
1060 parameters:
1061 - name: "body"
1062 schema:
1063 $ref: "#/definitions/Test_v2"
1064 - $ref: "#/parameters/a_v2"
1065 /othertest:
1066 post:
1067 parameters:
1068 - name: "body"
1069 schema:
1070 $ref: "#/definitions/Test_v3"
1071 - $ref: "#/parameters/a_v3"
1072 definitions:
1073 Test:
1074 type: "object"
1075 Test_v2:
1076 description: "This is an existing Test_v2 in destination schema"
1077 type: "object"
1078 Test_v3:
1079 description: "This Test has a description"
1080 type: "object"
1081 parameters:
1082 a:
1083 in: query
1084 name: a
1085 schema:
1086 $ref: "#/definitions/Test"
1087 a_v2:
1088 in: query
1089 name: a
1090 schema:
1091 $ref: "#/definitions/Test_v2"
1092 a_v3:
1093 in: query
1094 name: a
1095 schema:
1096 $ref: "#/definitions/Test_v3"
1097 `), &expected))
1098
1099 ast := assert.New(t)
1100 orig_spec2, _ := cloneSpec(spec2)
1101 if !ast.NoError(MergeSpecs(spec1, spec2)) {
1102 return
1103 }
1104 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
1105 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
1106 }
1107
1108 func TestMergeSpecsMultipleRenamesOfModelsAndLateConflict(t *testing.T) {
1109 var spec1, spec2, expected *spec.Swagger
1110 require.NoError(t, yaml.Unmarshal([]byte(`
1111 swagger: "2.0"
1112 paths:
1113 /test:
1114 post:
1115 parameters:
1116 - name: "body"
1117 schema:
1118 $ref: "#/definitions/Test"
1119 - $ref: "#/parameters/a"
1120 /test3:
1121 post:
1122 parameters:
1123 - name: "body"
1124 schema:
1125 $ref: "#/definitions/Test_v3"
1126 - $ref: "#/parameters/a_v3"
1127 definitions:
1128 Test:
1129 description: "I used to be Test in destination"
1130 type: "object"
1131 Test_v3:
1132 description: "I used to be Test_v3 in destination"
1133 type: "object"
1134 parameters:
1135 a:
1136 in: query
1137 name: a
1138 schema:
1139 $ref: "#/definitions/Test"
1140 a_v3:
1141 in: query
1142 name: a
1143 schema:
1144 $ref: "#/definitions/Test_v3"
1145 `), &spec1))
1146
1147 require.NoError(t, yaml.Unmarshal([]byte(`
1148 swagger: "2.0"
1149 paths:
1150 /othertest:
1151 post:
1152 parameters:
1153 - name: "body"
1154 schema:
1155 $ref: "#/definitions/Test"
1156 - $ref: "#/parameters/a"
1157 /othertest2:
1158 post:
1159 parameters:
1160 - name: "body"
1161 schema:
1162 $ref: "#/definitions/Test_v2"
1163 - $ref: "#/parameters/a_v2"
1164 definitions:
1165 Test:
1166 description: "I used to be Test in source"
1167 type: "object"
1168 Test_v2:
1169 description: "I used to be Test_v2 in source"
1170 type: "object"
1171 parameters:
1172 a:
1173 in: query
1174 name: a
1175 schema:
1176 $ref: "#/definitions/Test"
1177 a_v2:
1178 in: query
1179 name: a
1180 schema:
1181 $ref: "#/definitions/Test_v2"
1182 `), &spec2))
1183
1184 require.NoError(t, yaml.Unmarshal([]byte(`
1185 swagger: "2.0"
1186 paths:
1187 /test:
1188 post:
1189 parameters:
1190 - name: "body"
1191 schema:
1192 $ref: "#/definitions/Test"
1193 - $ref: "#/parameters/a"
1194 /test3:
1195 post:
1196 parameters:
1197 - name: "body"
1198 schema:
1199 $ref: "#/definitions/Test_v3"
1200 - $ref: "#/parameters/a_v3"
1201 /othertest2:
1202 post:
1203 parameters:
1204 - name: "body"
1205 schema:
1206 $ref: "#/definitions/Test_v2"
1207 - $ref: "#/parameters/a_v2"
1208 /othertest:
1209 post:
1210 parameters:
1211 - name: "body"
1212 schema:
1213 $ref: "#/definitions/Test_v4"
1214 - $ref: "#/parameters/a_v4"
1215 definitions:
1216 Test:
1217 description: "I used to be Test in destination"
1218 type: "object"
1219 Test_v2:
1220 description: "I used to be Test_v2 in source"
1221 type: "object"
1222 Test_v3:
1223 description: "I used to be Test_v3 in destination"
1224 type: "object"
1225 Test_v4:
1226 description: "I used to be Test in source"
1227 type: "object"
1228 parameters:
1229 a:
1230 in: query
1231 name: a
1232 schema:
1233 $ref: "#/definitions/Test"
1234 a_v2:
1235 in: query
1236 name: a
1237 schema:
1238 $ref: "#/definitions/Test_v2"
1239 a_v3:
1240 in: query
1241 name: a
1242 schema:
1243 $ref: "#/definitions/Test_v3"
1244 a_v4:
1245 in: query
1246 name: a
1247 schema:
1248 $ref: "#/definitions/Test_v4"
1249 `), &expected))
1250
1251 ast := assert.New(t)
1252 orig_spec2, _ := cloneSpec(spec2)
1253 if !ast.NoError(MergeSpecs(spec1, spec2)) {
1254 return
1255 }
1256 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
1257 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
1258 }
1259
1260 func TestMergeSpecsRenameModelWithExistingV2InSource(t *testing.T) {
1261 var spec1, spec2, expected *spec.Swagger
1262 require.NoError(t, yaml.Unmarshal([]byte(`
1263 swagger: "2.0"
1264 paths:
1265 /test:
1266 post:
1267 parameters:
1268 - name: "body"
1269 schema:
1270 $ref: "#/definitions/Test"
1271 - $ref: "#/parameters/a"
1272 definitions:
1273 Test:
1274 type: "object"
1275 parameters:
1276 a:
1277 in: query
1278 name: a
1279 schema:
1280 $ref: "#/definitions/Test"
1281 `), &spec1))
1282
1283 require.NoError(t, yaml.Unmarshal([]byte(`
1284 swagger: "2.0"
1285 paths:
1286 /othertest:
1287 post:
1288 parameters:
1289 - name: "body"
1290 schema:
1291 $ref: "#/definitions/Test"
1292 - $ref: "#/parameters/a"
1293 /testv2:
1294 post:
1295 parameters:
1296 - name: "body"
1297 schema:
1298 $ref: "#/definitions/Test_v2"
1299 - $ref: "#/parameters/a_v2"
1300 definitions:
1301 Test:
1302 description: "This Test has a description"
1303 type: "object"
1304 Test_v2:
1305 description: "This is an existing Test_v2 in source schema"
1306 type: "object"
1307 parameters:
1308 a:
1309 in: query
1310 name: a
1311 schema:
1312 $ref: "#/definitions/Test"
1313 a_v2:
1314 in: query
1315 name: a
1316 schema:
1317 $ref: "#/definitions/Test_v2"
1318 `), &spec2))
1319
1320 require.NoError(t, yaml.Unmarshal([]byte(`
1321 swagger: "2.0"
1322 paths:
1323 /test:
1324 post:
1325 parameters:
1326 - name: "body"
1327 schema:
1328 $ref: "#/definitions/Test"
1329 - $ref: "#/parameters/a"
1330 /testv2:
1331 post:
1332 parameters:
1333 - name: "body"
1334 schema:
1335 $ref: "#/definitions/Test_v2"
1336 - $ref: "#/parameters/a_v2"
1337 /othertest:
1338 post:
1339 parameters:
1340 - name: "body"
1341 schema:
1342 $ref: "#/definitions/Test_v3"
1343 - $ref: "#/parameters/a_v3"
1344 definitions:
1345 Test:
1346 type: "object"
1347 Test_v2:
1348 description: "This is an existing Test_v2 in source schema"
1349 type: "object"
1350 Test_v3:
1351 description: "This Test has a description"
1352 type: "object"
1353 parameters:
1354 a:
1355 in: query
1356 name: a
1357 schema:
1358 $ref: "#/definitions/Test"
1359 a_v2:
1360 in: query
1361 name: a
1362 schema:
1363 $ref: "#/definitions/Test_v2"
1364 a_v3:
1365 in: query
1366 name: a
1367 schema:
1368 $ref: "#/definitions/Test_v3"
1369 `), &expected))
1370
1371 ast := assert.New(t)
1372 orig_spec2, _ := cloneSpec(spec2)
1373 if !ast.NoError(MergeSpecs(spec1, spec2)) {
1374 return
1375 }
1376 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
1377 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
1378 }
1379
1380 // This tests if there are three specs, where the first two use the same object definition,
1381 // while the third one uses its own.
1382 // We expect the merged schema to contain two versions of the object, not three
1383 func TestTwoMergeSpecsFirstTwoSchemasHaveSameDefinition(t *testing.T) {
1384 var spec1, spec2, spec3, expected *spec.Swagger
1385 yaml.Unmarshal([]byte(`
1386 swagger: "2.0"
1387 paths:
1388 /test:
1389 post:
1390 parameters:
1391 - name: "body"
1392 schema:
1393 $ref: "#/definitions/Test"
1394 definitions:
1395 Test:
1396 description: "spec1 and spec2 use the same object definition, while spec3 doesn't"
1397 type: "object"
1398 `), &spec1)
1399
1400 yaml.Unmarshal([]byte(`
1401 swagger: "2.0"
1402 paths:
1403 /test2:
1404 post:
1405 parameters:
1406 - name: "body"
1407 schema:
1408 $ref: "#/definitions/Test"
1409 definitions:
1410 Test:
1411 description: "spec1 and spec2 use the same object definition, while spec3 doesn't"
1412 type: "object"
1413 `), &spec2)
1414
1415 yaml.Unmarshal([]byte(`
1416 swagger: "2.0"
1417 paths:
1418 /test3:
1419 post:
1420 parameters:
1421 - name: "body"
1422 schema:
1423 $ref: "#/definitions/Test"
1424 definitions:
1425 Test:
1426 description: "spec3 has its own definition (the description doesn't match)"
1427 type: "object"
1428 `), &spec3)
1429
1430 yaml.Unmarshal([]byte(`
1431 swagger: "2.0"
1432 paths:
1433 /test:
1434 post:
1435 parameters:
1436 - name: "body"
1437 schema:
1438 $ref: "#/definitions/Test"
1439 /test2:
1440 post:
1441 parameters:
1442 - name: "body"
1443 schema:
1444 $ref: "#/definitions/Test"
1445 /test3:
1446 post:
1447 parameters:
1448 - name: "body"
1449 schema:
1450 $ref: "#/definitions/Test_v2"
1451 definitions:
1452 Test:
1453 description: "spec1 and spec2 use the same object definition, while spec3 doesn't"
1454 type: "object"
1455 Test_v2:
1456 description: "spec3 has its own definition (the description doesn't match)"
1457 type: "object"
1458 `), &expected)
1459
1460 ast := assert.New(t)
1461 orig_spec2, _ := cloneSpec(spec2)
1462 orig_spec3, _ := cloneSpec(spec3)
1463 if !ast.NoError(MergeSpecs(spec1, spec2)) {
1464 return
1465 }
1466 if !ast.NoError(MergeSpecs(spec1, spec3)) {
1467 return
1468 }
1469 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
1470 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of spec2 input")
1471 ast.Equal(DebugSpec{orig_spec3}, DebugSpec{spec3}, "unexpected mutation of spec3 input")
1472 }
1473
1474 // This tests if there are three specs, where the last two use the same object definition,
1475 // while the first one uses its own.
1476 // We expect the merged schema to contain two versions of the object, not three
1477 func TestTwoMergeSpecsLastTwoSchemasHaveSameDefinition(t *testing.T) {
1478 var spec1, spec2, spec3, expected *spec.Swagger
1479 yaml.Unmarshal([]byte(`
1480 swagger: "2.0"
1481 paths:
1482 /test:
1483 post:
1484 parameters:
1485 - name: "body"
1486 schema:
1487 $ref: "#/definitions/Test"
1488 definitions:
1489 Test:
1490 type: "object"
1491 `), &spec1)
1492
1493 yaml.Unmarshal([]byte(`
1494 swagger: "2.0"
1495 paths:
1496 /othertest:
1497 post:
1498 parameters:
1499 - name: "body"
1500 schema:
1501 $ref: "#/definitions/Test"
1502 definitions:
1503 Test:
1504 description: "spec2 and spec3 use the same object definition, while spec1 doesn't"
1505 type: "object"
1506 `), &spec2)
1507
1508 yaml.Unmarshal([]byte(`
1509 swagger: "2.0"
1510 paths:
1511 /othertest2:
1512 post:
1513 parameters:
1514 - name: "body"
1515 schema:
1516 $ref: "#/definitions/Test"
1517 definitions:
1518 Test:
1519 description: "spec2 and spec3 use the same object definition, while spec1 doesn't"
1520 type: "object"
1521 `), &spec3)
1522
1523 yaml.Unmarshal([]byte(`
1524 swagger: "2.0"
1525 paths:
1526 /test:
1527 post:
1528 parameters:
1529 - name: "body"
1530 schema:
1531 $ref: "#/definitions/Test"
1532 /othertest:
1533 post:
1534 parameters:
1535 - name: "body"
1536 schema:
1537 $ref: "#/definitions/Test_v2"
1538 /othertest2:
1539 post:
1540 parameters:
1541 - name: "body"
1542 schema:
1543 $ref: "#/definitions/Test_v2"
1544 definitions:
1545 Test:
1546 type: "object"
1547 Test_v2:
1548 description: "spec2 and spec3 use the same object definition, while spec1 doesn't"
1549 type: "object"
1550 `), &expected)
1551
1552 ast := assert.New(t)
1553 orig_spec2, _ := cloneSpec(spec2)
1554 orig_spec3, _ := cloneSpec(spec3)
1555 if !ast.NoError(MergeSpecs(spec1, spec2)) {
1556 return
1557 }
1558 if !ast.NoError(MergeSpecs(spec1, spec3)) {
1559 return
1560 }
1561 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
1562 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of spec2 input")
1563 ast.Equal(DebugSpec{orig_spec3}, DebugSpec{spec3}, "unexpected mutation of spec3 input")
1564
1565 }
1566
1567 func TestSafeMergeSpecsSimple(t *testing.T) {
1568 var fooSpec, barSpec, expected *spec.Swagger
1569 yaml.Unmarshal([]byte(`
1570 swagger: "2.0"
1571 paths:
1572 /foo:
1573 post:
1574 summary: "Foo API"
1575 operationId: "fooTest"
1576 parameters:
1577 - in: "body"
1578 name: "body"
1579 description: "foo object"
1580 required: true
1581 schema:
1582 $ref: "#/definitions/Foo"
1583 responses:
1584 200:
1585 description: "OK"
1586 definitions:
1587 Foo:
1588 type: "object"
1589 properties:
1590 id:
1591 type: "integer"
1592 format: "int64"
1593 `), &fooSpec)
1594
1595 yaml.Unmarshal([]byte(`
1596 swagger: "2.0"
1597 paths:
1598 /bar:
1599 post:
1600 summary: "Bar API"
1601 operationId: "barTest"
1602 parameters:
1603 - in: "body"
1604 name: "body"
1605 description: "bar object"
1606 required: true
1607 schema:
1608 $ref: "#/definitions/Bar"
1609 responses:
1610 200:
1611 description: "OK"
1612 definitions:
1613 Bar:
1614 type: "object"
1615 properties:
1616 id:
1617 type: "integer"
1618 format: "int64"
1619 `), &barSpec)
1620
1621 yaml.Unmarshal([]byte(`
1622 swagger: "2.0"
1623 paths:
1624 /foo:
1625 post:
1626 summary: "Foo API"
1627 operationId: "fooTest"
1628 parameters:
1629 - in: "body"
1630 name: "body"
1631 description: "foo object"
1632 required: true
1633 schema:
1634 $ref: "#/definitions/Foo"
1635 responses:
1636 200:
1637 description: "OK"
1638 /bar:
1639 post:
1640 summary: "Bar API"
1641 operationId: "barTest"
1642 parameters:
1643 - in: "body"
1644 name: "body"
1645 description: "bar object"
1646 required: true
1647 schema:
1648 $ref: "#/definitions/Bar"
1649 responses:
1650 200:
1651 description: "OK"
1652 definitions:
1653 Foo:
1654 type: "object"
1655 properties:
1656 id:
1657 type: "integer"
1658 format: "int64"
1659 Bar:
1660 type: "object"
1661 properties:
1662 id:
1663 type: "integer"
1664 format: "int64"
1665 `), &expected)
1666
1667 ast := assert.New(t)
1668 orig_barSpec, err := cloneSpec(barSpec)
1669 if !ast.NoError(err) {
1670 return
1671 }
1672 if !ast.NoError(MergeSpecsFailOnDefinitionConflict(fooSpec, barSpec)) {
1673 return
1674 }
1675 ast.Equal(DebugSpec{expected}, DebugSpec{fooSpec})
1676 ast.Equal(DebugSpec{orig_barSpec}, DebugSpec{barSpec}, "unexpected mutation of input")
1677 }
1678
1679 func TestSafeMergeSpecsReuseModel(t *testing.T) {
1680 var fooSpec, barSpec, expected *spec.Swagger
1681 if err := yaml.Unmarshal([]byte(`
1682 swagger: "2.0"
1683 paths:
1684 /foo:
1685 post:
1686 summary: "Foo API"
1687 operationId: "fooTest"
1688 parameters:
1689 - in: "body"
1690 name: "body"
1691 description: "foo object"
1692 required: true
1693 schema:
1694 $ref: "#/definitions/Foo"
1695 responses:
1696 200:
1697 description: "OK"
1698 definitions:
1699 Foo:
1700 type: "object"
1701 properties:
1702 id:
1703 type: "integer"
1704 format: "int64"
1705 x-kubernetes-group-version-kind:
1706 - group: group1
1707 version: v1
1708 kind: Foo
1709 - group: group3
1710 version: v1
1711 kind: Foo
1712 `), &fooSpec); err != nil {
1713 t.Fatal(err)
1714 }
1715
1716 if err := yaml.Unmarshal([]byte(`
1717 swagger: "2.0"
1718 paths:
1719 /refoo:
1720 post:
1721 summary: "Refoo API"
1722 operationId: "refooTest"
1723 parameters:
1724 - in: "body"
1725 name: "body"
1726 description: "foo object"
1727 required: true
1728 schema:
1729 $ref: "#/definitions/Foo"
1730 responses:
1731 200:
1732 description: "OK"
1733 definitions:
1734 Foo:
1735 type: "object"
1736 properties:
1737 id:
1738 type: "integer"
1739 format: "int64"
1740 x-kubernetes-group-version-kind:
1741 - group: group2
1742 version: v1
1743 kind: Foo
1744 `), &barSpec); err != nil {
1745 t.Fatal(err)
1746 }
1747
1748 if err := yaml.Unmarshal([]byte(`
1749 swagger: "2.0"
1750 paths:
1751 /foo:
1752 post:
1753 summary: "Foo API"
1754 operationId: "fooTest"
1755 parameters:
1756 - in: "body"
1757 name: "body"
1758 description: "foo object"
1759 required: true
1760 schema:
1761 $ref: "#/definitions/Foo"
1762 responses:
1763 200:
1764 description: "OK"
1765 /refoo:
1766 post:
1767 summary: "Refoo API"
1768 operationId: "refooTest"
1769 parameters:
1770 - in: "body"
1771 name: "body"
1772 description: "foo object"
1773 required: true
1774 schema:
1775 $ref: "#/definitions/Foo"
1776 responses:
1777 200:
1778 description: "OK"
1779 definitions:
1780 Foo:
1781 type: "object"
1782 properties:
1783 id:
1784 type: "integer"
1785 format: "int64"
1786 x-kubernetes-group-version-kind:
1787 - group: group1
1788 version: v1
1789 kind: Foo
1790 - group: group2
1791 version: v1
1792 kind: Foo
1793 - group: group3
1794 version: v1
1795 kind: Foo
1796 `), &expected); err != nil {
1797 t.Fatal(err)
1798 }
1799
1800 ast := assert.New(t)
1801 orig_barSpec, err := cloneSpec(barSpec)
1802 if !ast.NoError(err) {
1803 return
1804 }
1805 if !ast.NoError(MergeSpecsFailOnDefinitionConflict(fooSpec, barSpec)) {
1806 return
1807 }
1808 ast.Equal(DebugSpec{expected}, DebugSpec{fooSpec})
1809 ast.Equal(DebugSpec{orig_barSpec}, DebugSpec{barSpec}, "unexpected mutation of input")
1810 }
1811
1812 func TestSafeMergeSpecsReuseModelFails(t *testing.T) {
1813 var fooSpec, barSpec, expected *spec.Swagger
1814 yaml.Unmarshal([]byte(`
1815 swagger: "2.0"
1816 paths:
1817 /foo:
1818 post:
1819 summary: "Foo API"
1820 operationId: "fooTest"
1821 parameters:
1822 - in: "body"
1823 name: "body"
1824 description: "foo object"
1825 required: true
1826 schema:
1827 $ref: "#/definitions/Foo"
1828 responses:
1829 200:
1830 description: "OK"
1831 definitions:
1832 Foo:
1833 type: "object"
1834 properties:
1835 id:
1836 type: "integer"
1837 format: "int64"
1838 `), &fooSpec)
1839
1840 yaml.Unmarshal([]byte(`
1841 swagger: "2.0"
1842 paths:
1843 /refoo:
1844 post:
1845 summary: "Refoo API"
1846 operationId: "refooTest"
1847 parameters:
1848 - in: "body"
1849 name: "body"
1850 description: "foo object"
1851 required: true
1852 schema:
1853 $ref: "#/definitions/Foo"
1854 responses:
1855 200:
1856 description: "OK"
1857 definitions:
1858 Foo:
1859 type: "object"
1860 properties:
1861 id:
1862 type: "integer"
1863 format: "int64"
1864 new_field:
1865 type: "string"
1866 `), &barSpec)
1867
1868 yaml.Unmarshal([]byte(`
1869 swagger: "2.0"
1870 paths:
1871 /foo:
1872 post:
1873 summary: "Foo API"
1874 operationId: "fooTest"
1875 parameters:
1876 - in: "body"
1877 name: "body"
1878 description: "foo object"
1879 required: true
1880 schema:
1881 $ref: "#/definitions/Foo"
1882 responses:
1883 200:
1884 description: "OK"
1885 /refoo:
1886 post:
1887 summary: "Refoo API"
1888 operationId: "refooTest"
1889 parameters:
1890 - in: "body"
1891 name: "body"
1892 description: "foo object"
1893 required: true
1894 schema:
1895 $ref: "#/definitions/Foo"
1896 responses:
1897 200:
1898 description: "OK"
1899 definitions:
1900 Foo:
1901 type: "object"
1902 properties:
1903 id:
1904 type: "integer"
1905 format: "int64"
1906 `), &expected)
1907
1908 ast := assert.New(t)
1909 ast.Error(MergeSpecsFailOnDefinitionConflict(fooSpec, barSpec))
1910 }
1911
1912 func TestMergeSpecsIgnorePathConflicts(t *testing.T) {
1913 var fooSpec, barSpec, expected *spec.Swagger
1914 yaml.Unmarshal([]byte(`
1915 swagger: "2.0"
1916 paths:
1917 /foo:
1918 post:
1919 summary: "Foo API"
1920 operationId: "fooTest"
1921 parameters:
1922 - in: "body"
1923 name: "body"
1924 description: "foo object"
1925 required: true
1926 schema:
1927 $ref: "#/definitions/Foo"
1928 responses:
1929 200:
1930 description: "OK"
1931 definitions:
1932 Foo:
1933 type: "object"
1934 properties:
1935 id:
1936 type: "integer"
1937 format: "int64"
1938 `), &fooSpec)
1939
1940 yaml.Unmarshal([]byte(`
1941 swagger: "2.0"
1942 paths:
1943 /foo:
1944 post:
1945 summary: "Should be ignored"
1946 /bar:
1947 post:
1948 summary: "Bar API"
1949 operationId: "barTest"
1950 parameters:
1951 - in: "body"
1952 name: "body"
1953 description: "bar object"
1954 required: true
1955 schema:
1956 $ref: "#/definitions/Bar"
1957 responses:
1958 200:
1959 description: "OK"
1960 definitions:
1961 Bar:
1962 type: "object"
1963 properties:
1964 id:
1965 type: "integer"
1966 format: "int64"
1967 `), &barSpec)
1968
1969 yaml.Unmarshal([]byte(`
1970 swagger: "2.0"
1971 paths:
1972 /foo:
1973 post:
1974 summary: "Foo API"
1975 operationId: "fooTest"
1976 parameters:
1977 - in: "body"
1978 name: "body"
1979 description: "foo object"
1980 required: true
1981 schema:
1982 $ref: "#/definitions/Foo"
1983 responses:
1984 200:
1985 description: "OK"
1986 /bar:
1987 post:
1988 summary: "Bar API"
1989 operationId: "barTest"
1990 parameters:
1991 - in: "body"
1992 name: "body"
1993 description: "bar object"
1994 required: true
1995 schema:
1996 $ref: "#/definitions/Bar"
1997 responses:
1998 200:
1999 description: "OK"
2000 definitions:
2001 Foo:
2002 type: "object"
2003 properties:
2004 id:
2005 type: "integer"
2006 format: "int64"
2007 Bar:
2008 type: "object"
2009 properties:
2010 id:
2011 type: "integer"
2012 format: "int64"
2013 `), &expected)
2014
2015 ast := assert.New(t)
2016 actual, _ := cloneSpec(fooSpec)
2017 orig_barSpec, _ := cloneSpec(barSpec)
2018 if !ast.Error(MergeSpecs(actual, barSpec)) {
2019 return
2020 }
2021 ast.Equal(DebugSpec{orig_barSpec}, DebugSpec{barSpec}, "unexpected mutation of input")
2022
2023 actual, _ = cloneSpec(fooSpec)
2024 if !ast.NoError(MergeSpecsIgnorePathConflictDeprecated(actual, barSpec)) {
2025 return
2026 }
2027 ast.Equal(DebugSpec{expected}, DebugSpec{actual})
2028 ast.Equal(DebugSpec{orig_barSpec}, DebugSpec{barSpec}, "unexpected mutation of input")
2029 }
2030
2031 func TestMergeSpecsIgnorePathConflictsAllConflicting(t *testing.T) {
2032 var fooSpec *spec.Swagger
2033 yaml.Unmarshal([]byte(`
2034 swagger: "2.0"
2035 paths:
2036 /foo:
2037 post:
2038 summary: "Foo API"
2039 operationId: "fooTest"
2040 parameters:
2041 - in: "body"
2042 name: "body"
2043 description: "foo object"
2044 required: true
2045 schema:
2046 $ref: "#/definitions/Foo"
2047 responses:
2048 200:
2049 description: "OK"
2050 definitions:
2051 Foo:
2052 type: "object"
2053 properties:
2054 id:
2055 type: "integer"
2056 format: "int64"
2057 `), &fooSpec)
2058
2059 ast := assert.New(t)
2060 foo2Spec, _ := cloneSpec(fooSpec)
2061 actual, _ := cloneSpec(fooSpec)
2062 if !ast.NoError(MergeSpecsIgnorePathConflictRenamingDefinitionsAndParameters(actual, foo2Spec)) {
2063 return
2064 }
2065 ast.Equal(DebugSpec{fooSpec}, DebugSpec{actual})
2066 ast.Equal(DebugSpec{fooSpec}, DebugSpec{foo2Spec}, "unexpected mutation of input")
2067 }
2068
2069 func TestMergeSpecsIgnorePathConflictsWithKubeSpec(t *testing.T) {
2070 ast := assert.New(t)
2071
2072 specs, expected := loadTestData()
2073 sp, specs := specs[0], specs[1:]
2074
2075 origSpecs := make([]*spec.Swagger, len(specs))
2076 for i := range specs {
2077 cpy, err := cloneSpec(specs[i])
2078 if err != nil {
2079 t.Fatal(err)
2080 }
2081 ast.NoError(err)
2082 origSpecs[i] = cpy
2083 }
2084
2085 for i := range specs {
2086 if err := MergeSpecsIgnorePathConflictRenamingDefinitionsAndParameters(sp, specs[i]); err != nil {
2087 t.Fatalf("merging spec %d failed: %v", i, err)
2088 }
2089 }
2090
2091 ast.Equal(DebugSpec{expected}, DebugSpec{sp})
2092
2093 for i := range specs {
2094 ast.Equal(DebugSpec{origSpecs[i]}, DebugSpec{specs[i]}, "unexpected mutation of specs[%d]", i)
2095 }
2096 }
2097
2098 func BenchmarkMergeSpecsIgnorePathConflictsWithKubeSpec(b *testing.B) {
2099 b.StopTimer()
2100 b.ReportAllocs()
2101 b.ResetTimer()
2102
2103 specs, _ := loadTestData()
2104 start, specs := specs[0], specs[1:]
2105
2106 for n := 0; n < b.N; n++ {
2107 sp, err := cloneSpec(start)
2108 if err != nil {
2109 b.Fatal(err)
2110 }
2111
2112 b.StartTimer()
2113 for i := range specs {
2114 if err := MergeSpecsIgnorePathConflictRenamingDefinitionsAndParameters(sp, specs[i]); err != nil {
2115 panic(err)
2116 }
2117 }
2118
2119 specBytes, _ := sp.MarshalJSON()
2120 handler.ToProtoBinary(specBytes)
2121
2122 b.StopTimer()
2123 }
2124 }
2125
2126 func TestMergeSpecReplacesAllPossibleRefs(t *testing.T) {
2127 var spec1, spec2, expected *spec.Swagger
2128 yaml.Unmarshal([]byte(`
2129 swagger: "2.0"
2130 paths:
2131 /test:
2132 post:
2133 parameters:
2134 - name: "body"
2135 schema:
2136 $ref: "#/definitions/Test"
2137 definitions:
2138 Test:
2139 type: "object"
2140 properties:
2141 foo:
2142 $ref: "#/definitions/TestProperty"
2143 TestProperty:
2144 type: "object"
2145 `), &spec1)
2146
2147 yaml.Unmarshal([]byte(`
2148 swagger: "2.0"
2149 paths:
2150 /test2:
2151 post:
2152 parameters:
2153 - name: "test2"
2154 schema:
2155 $ref: "#/definitions/Test2"
2156 - name: "test3"
2157 schema:
2158 $ref: "#/definitions/Test3"
2159 - name: "test4"
2160 schema:
2161 $ref: "#/definitions/Test4"
2162 - name: "test5"
2163 schema:
2164 $ref: "#/definitions/Test5"
2165 definitions:
2166 Test2:
2167 $ref: "#/definitions/TestProperty"
2168 Test3:
2169 type: "object"
2170 properties:
2171 withRef:
2172 $ref: "#/definitions/TestProperty"
2173 withAllOf:
2174 type: "object"
2175 allOf:
2176 - $ref: "#/definitions/TestProperty"
2177 - type: object
2178 properties:
2179 test:
2180 $ref: "#/definitions/TestProperty"
2181 withAnyOf:
2182 type: "object"
2183 anyOf:
2184 - $ref: "#/definitions/TestProperty"
2185 - type: object
2186 properties:
2187 test:
2188 $ref: "#/definitions/TestProperty"
2189 withOneOf:
2190 type: "object"
2191 oneOf:
2192 - $ref: "#/definitions/TestProperty"
2193 - type: object
2194 properties:
2195 test:
2196 $ref: "#/definitions/TestProperty"
2197 withNot:
2198 type: "object"
2199 not:
2200 $ref: "#/definitions/TestProperty"
2201 patternProperties:
2202 "prefix.*":
2203 $ref: "#/definitions/TestProperty"
2204 additionalProperties:
2205 $ref: "#/definitions/TestProperty"
2206 definitions:
2207 SomeDefinition:
2208 $ref: "#/definitions/TestProperty"
2209 Test4:
2210 type: "array"
2211 items:
2212 $ref: "#/definitions/TestProperty"
2213 additionalItems:
2214 $ref: "#/definitions/TestProperty"
2215 Test5:
2216 type: "array"
2217 items:
2218 - $ref: "#/definitions/TestProperty"
2219 - $ref: "#/definitions/TestProperty"
2220 TestProperty:
2221 description: "This TestProperty is different from the one in spec1"
2222 type: "object"
2223 `), &spec2)
2224
2225 yaml.Unmarshal([]byte(`
2226 swagger: "2.0"
2227 paths:
2228 /test:
2229 post:
2230 parameters:
2231 - name: "body"
2232 schema:
2233 $ref: "#/definitions/Test"
2234 /test2:
2235 post:
2236 parameters:
2237 - name: "test2"
2238 schema:
2239 $ref: "#/definitions/Test2"
2240 - name: "test3"
2241 schema:
2242 $ref: "#/definitions/Test3"
2243 - name: "test4"
2244 schema:
2245 $ref: "#/definitions/Test4"
2246 - name: "test5"
2247 schema:
2248 $ref: "#/definitions/Test5"
2249 definitions:
2250 Test:
2251 type: "object"
2252 properties:
2253 foo:
2254 $ref: "#/definitions/TestProperty"
2255 TestProperty:
2256 type: "object"
2257 Test2:
2258 $ref: "#/definitions/TestProperty_v2"
2259 Test3:
2260 type: "object"
2261 properties:
2262 withRef:
2263 $ref: "#/definitions/TestProperty_v2"
2264 withAllOf:
2265 type: "object"
2266 allOf:
2267 - $ref: "#/definitions/TestProperty_v2"
2268 - type: object
2269 properties:
2270 test:
2271 $ref: "#/definitions/TestProperty_v2"
2272 withAnyOf:
2273 type: "object"
2274 anyOf:
2275 - $ref: "#/definitions/TestProperty_v2"
2276 - type: object
2277 properties:
2278 test:
2279 $ref: "#/definitions/TestProperty_v2"
2280 withOneOf:
2281 type: "object"
2282 oneOf:
2283 - $ref: "#/definitions/TestProperty_v2"
2284 - type: object
2285 properties:
2286 test:
2287 $ref: "#/definitions/TestProperty_v2"
2288 withNot:
2289 type: "object"
2290 not:
2291 $ref: "#/definitions/TestProperty_v2"
2292 patternProperties:
2293 "prefix.*":
2294 $ref: "#/definitions/TestProperty_v2"
2295 additionalProperties:
2296 $ref: "#/definitions/TestProperty_v2"
2297 definitions:
2298 SomeDefinition:
2299 $ref: "#/definitions/TestProperty_v2"
2300 Test4:
2301 type: "array"
2302 items:
2303 $ref: "#/definitions/TestProperty_v2"
2304 additionalItems:
2305 $ref: "#/definitions/TestProperty_v2"
2306 Test5:
2307 type: "array"
2308 items:
2309 - $ref: "#/definitions/TestProperty_v2"
2310 - $ref: "#/definitions/TestProperty_v2"
2311 TestProperty_v2:
2312 description: "This TestProperty is different from the one in spec1"
2313 type: "object"
2314 `), &expected)
2315
2316 ast := assert.New(t)
2317 orig_spec2, _ := cloneSpec(spec2)
2318 if !ast.NoError(MergeSpecs(spec1, spec2)) {
2319 return
2320 }
2321 ast.Equal(DebugSpec{expected}, DebugSpec{spec1})
2322 ast.Equal(DebugSpec{orig_spec2}, DebugSpec{spec2}, "unexpected mutation of input")
2323 }
2324
2325 func loadTestData() ([]*spec.Swagger, *spec.Swagger) {
2326 loadSpec := func(fileName string) *spec.Swagger {
2327 bs, err := os.ReadFile(filepath.Join("../../test/integration/testdata/aggregator", fileName))
2328 if err != nil {
2329 panic(err)
2330 }
2331 sp := spec.Swagger{}
2332
2333 if err := json.Unmarshal(bs, &sp); err != nil {
2334 panic(err)
2335 }
2336 return &sp
2337 }
2338
2339 specs := []*spec.Swagger{
2340 loadSpec("openapi-0.json"),
2341 loadSpec("openapi-1.json"),
2342 loadSpec("openapi-2.json"),
2343 }
2344 expected := loadSpec("openapi.json")
2345
2346 return specs, expected
2347 }
2348
2349 func TestCloneSpec(t *testing.T) {
2350 _, sp := loadTestData()
2351 clone, err := cloneSpec(sp)
2352 if err != nil {
2353 t.Fatalf("unexpected error: %v", err)
2354 }
2355 ast := assert.New(t)
2356 ast.Equal(DebugSpec{sp}, DebugSpec{clone})
2357 }
2358
2359 func cloneSpec(source *spec.Swagger) (*spec.Swagger, error) {
2360 bytes, err := source.MarshalJSON()
2361 if err != nil {
2362 return nil, err
2363 }
2364 var ret spec.Swagger
2365 err = json.Unmarshal(bytes, &ret)
2366 if err != nil {
2367 return nil, err
2368 }
2369 return &ret, nil
2370 }
2371
2372 func TestMergedGVKs(t *testing.T) {
2373 gvk1 := map[string]interface{}{"group": "group1", "version": "v1", "kind": "Foo"}
2374 gvk2 := map[string]interface{}{"group": "group2", "version": "v1", "kind": "Bar"}
2375 gvk3 := map[string]interface{}{"group": "group3", "version": "v1", "kind": "Abc"}
2376 gvk4 := map[string]interface{}{"group": "group4", "version": "v1", "kind": "Abc"}
2377
2378 tests := []struct {
2379 name string
2380 gvks1 interface{}
2381 gvks2 interface{}
2382 want interface{}
2383 wantChanged bool
2384 wantErr bool
2385 }{
2386 {"nil", nil, nil, nil, false, false},
2387 {"first only", []interface{}{gvk1, gvk2}, nil, []interface{}{gvk1, gvk2}, false, false},
2388 {"second only", nil, []interface{}{gvk1, gvk2}, []interface{}{gvk1, gvk2}, true, false},
2389 {"both", []interface{}{gvk1, gvk2}, []interface{}{gvk3}, []interface{}{gvk1, gvk2, gvk3}, true, false},
2390 {"equal, different order", []interface{}{gvk1, gvk2, gvk3}, []interface{}{gvk3, gvk2, gvk1}, []interface{}{gvk1, gvk2, gvk3}, false, false},
2391 {"ordered", []interface{}{gvk3, gvk1, gvk4}, []interface{}{gvk2}, []interface{}{gvk1, gvk2, gvk3, gvk4}, true, false},
2392 {"not ordered when not changed", []interface{}{gvk3, gvk1, gvk4}, []interface{}{}, []interface{}{gvk3, gvk1, gvk4}, false, false},
2393 {"empty", []interface{}{}, []interface{}{}, []interface{}{}, false, false},
2394 {"overlapping", []interface{}{gvk1, gvk2}, []interface{}{gvk2, gvk3}, []interface{}{gvk1, gvk2, gvk3}, true, false},
2395 {"first no slice", 42, []interface{}{gvk1}, nil, false, true},
2396 {"second no slice", []interface{}{gvk1}, 42, nil, false, true},
2397 {"no map in slice", []interface{}{42}, []interface{}{gvk1}, nil, false, true},
2398 }
2399 for _, tt := range tests {
2400 t.Run(tt.name, func(t *testing.T) {
2401 var ext1, ext2 map[string]interface{}
2402 if tt.gvks1 != nil {
2403 ext1 = map[string]interface{}{"x-kubernetes-group-version-kind": tt.gvks1}
2404 }
2405 if tt.gvks2 != nil {
2406 ext2 = map[string]interface{}{"x-kubernetes-group-version-kind": tt.gvks2}
2407 }
2408
2409 got, gotChanged, gotErr := mergedGVKs(
2410 &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: ext1}},
2411 &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: ext2}},
2412 )
2413 if (gotErr != nil) != tt.wantErr {
2414 t.Errorf("mergedGVKs() error = %v, wantErr %v", gotErr, tt.wantErr)
2415 return
2416 }
2417 if gotChanged != tt.wantChanged {
2418 t.Errorf("mergedGVKs() changed = %v, want %v", gotChanged, tt.wantChanged)
2419 }
2420 if !reflect.DeepEqual(got, tt.want) {
2421 t.Errorf("mergedGVKs() got = %v, want %v", got, tt.want)
2422 }
2423 })
2424 }
2425 }
2426
2427 func TestDeepEqualDefinitionsModuloGVKs(t *testing.T) {
2428 tests := []struct {
2429 name string
2430 s1 *spec.Schema
2431 s2 *spec.Schema
2432 equal bool
2433 }{
2434 {name: "nil", equal: true},
2435 {name: "nil, non-nil", s1: nil, s2: &spec.Schema{}},
2436 {name: "equal", s1: &spec.Schema{}, s2: &spec.Schema{}, equal: true},
2437 {name: "different", s1: &spec.Schema{SchemaProps: spec.SchemaProps{ID: "abc"}}, s2: &spec.Schema{}},
2438 {name: "equal modulo: nil, empty",
2439 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: nil}},
2440 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{}}},
2441 equal: true,
2442 },
2443 {name: "equal modulo: nil, gvk",
2444 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: nil}},
2445 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2446 gvkKey: true,
2447 }}},
2448 equal: true,
2449 },
2450 {name: "equal modulo: empty, gvk",
2451 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{}}},
2452 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2453 gvkKey: true,
2454 }}},
2455 equal: true,
2456 },
2457 {name: "equal modulo: non-empty, gvk",
2458 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{"foo": "bar"}}},
2459 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2460 gvkKey: true,
2461 "foo": "bar",
2462 }}},
2463 equal: true,
2464 },
2465 {name: "equal modulo: gvk, gvk",
2466 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2467 gvkKey: false,
2468 "foo": "bar",
2469 }}},
2470 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2471 gvkKey: true,
2472 "foo": "bar",
2473 }}},
2474 equal: true,
2475 },
2476 {name: "different values",
2477 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2478 gvkKey: false,
2479 "foo": "bar",
2480 }}},
2481 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2482 gvkKey: true,
2483 "foo": "abc",
2484 }}},
2485 },
2486 {name: "different sizes",
2487 s1: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2488 gvkKey: false,
2489 "foo": "bar",
2490 "xyz": "123",
2491 }}},
2492 s2: &spec.Schema{VendorExtensible: spec.VendorExtensible{Extensions: spec.Extensions{
2493 gvkKey: true,
2494 "foo": "abc",
2495 }}},
2496 },
2497 }
2498 for _, tt := range tests {
2499 t.Run(tt.name, func(t *testing.T) {
2500 if got := deepEqualDefinitionsModuloGVKs(tt.s1, tt.s2); got != tt.equal {
2501 t.Errorf("deepEqualDefinitionsModuloGVKs(s1, v2) = %v, want %v", got, tt.equal)
2502 }
2503
2504 if got := deepEqualDefinitionsModuloGVKs(tt.s2, tt.s1); got != tt.equal {
2505 t.Errorf("deepEqualDefinitionsModuloGVKs(s2, s1) = %v, want %v", got, tt.equal)
2506 }
2507 })
2508 }
2509 }