Debian Codegraph @

parameters/query_parameters_test.go

raw
1 // Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package parameters
5
6 import (
7 "fmt"
8 "net/http"
9 "strings"
10 "sync"
11 "testing"
12
13 "github.com/pb33f/libopenapi"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16
17 "github.com/pb33f/libopenapi-validator/config"
18 "github.com/pb33f/libopenapi-validator/paths"
19 )
20
21 func TestNewValidator_QueryParamMissing(t *testing.T) {
22 spec := `openapi: 3.1.0
23 paths:
24 /a/fishy/on/a/dishy:
25 get:
26 parameters:
27 - name: fishy
28 in: query
29 required: true
30 schema:
31 type: string
32 operationId: locateFishy
33 `
34
35 doc, _ := libopenapi.NewDocument([]byte(spec))
36
37 m, _ := doc.BuildV3Model()
38
39 v := NewParameterValidator(&m.Model)
40
41 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy", nil)
42
43 valid, errors := v.ValidateQueryParams(request)
44 assert.False(t, valid)
45 assert.Equal(t, 1, len(errors))
46 assert.Equal(t, "Query parameter 'fishy' is missing", errors[0].Message)
47 assert.Equal(t, request.Method, errors[0].RequestMethod)
48 assert.Equal(t, request.URL.Path, errors[0].RequestPath)
49 assert.Equal(t, "/a/fishy/on/a/dishy", errors[0].SpecPath)
50 }
51
52 func TestNewValidator_QueryParamNotMissing(t *testing.T) {
53 spec := `openapi: 3.1.0
54 paths:
55 /a/fishy/on/a/dishy:
56 get:
57 parameters:
58 - name: fishy
59 in: query
60 required: true
61 schema:
62 type: string
63 operationId: locateFishy
64 `
65
66 doc, _ := libopenapi.NewDocument([]byte(spec))
67
68 m, _ := doc.BuildV3Model()
69
70 v := NewParameterValidator(&m.Model)
71
72 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
73
74 valid, errors := v.ValidateQueryParams(request)
75 assert.True(t, valid)
76
77 assert.Nil(t, errors)
78 }
79
80 func TestNewValidator_QueryParamMinimumLength_violation(t *testing.T) {
81 spec := `openapi: 3.1.0
82 paths:
83 /a/fishy/on/a/dishy:
84 get:
85 parameters:
86 - name: fishy
87 in: query
88 required: true
89 schema:
90 type: string
91 minLength: 4
92 operationId: locateFishy
93 `
94
95 doc, err := libopenapi.NewDocument([]byte(spec))
96 require.NoError(t, err)
97 m, errs := doc.BuildV3Model()
98 require.NoError(t, errs)
99
100 v := NewParameterValidator(&m.Model)
101
102 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
103
104 valid, errors := v.ValidateQueryParams(request)
105 assert.False(t, valid)
106 assert.Equal(t, 1, len(errors))
107 assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
108 }
109
110 func TestNewValidator_QueryParamMaximumLength_violation(t *testing.T) {
111 spec := `openapi: 3.1.0
112 paths:
113 /a/fishy/on/a/dishy:
114 get:
115 parameters:
116 - name: fishy
117 in: query
118 required: true
119 schema:
120 type: string
121 maxLength: 1
122 operationId: locateFishy
123 `
124
125 doc, err := libopenapi.NewDocument([]byte(spec))
126 require.NoError(t, err)
127 m, errs := doc.BuildV3Model()
128 require.NoError(t, errs)
129
130 v := NewParameterValidator(&m.Model)
131
132 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
133
134 valid, errors := v.ValidateQueryParams(request)
135 assert.False(t, valid)
136 assert.Equal(t, 1, len(errors))
137 assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
138 }
139
140 //func TestNewValidator_QueryParamMinimum_ignoredviolation(t *testing.T) {
141 // spec := `openapi: 3.1.0
142 //paths:
143 // /a/fishy/on/a/dishy:
144 // get:
145 // parameters:
146 // - name: fishy
147 // in: query
148 // required: true
149 // schema:
150 // type: object
151 // operationId: locateFishy
152 //`
153 //
154 // doc, err := libopenapi.NewDocument([]byte(spec))
155 // require.NoError(t, err)
156 // m, errs := doc.BuildV3Model()
157 // require.Len(t, errs, 0)
158 //
159 // v := NewParameterValidator(&m.Model)
160 //
161 // request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
162 //
163 // valid, errors := v.ValidateQueryParams(request)
164 // assert.False(t, valid)
165 // assert.Equal(t, 1, len(errors))
166 // assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
167 //}
168
169 func TestNewValidator_QueryParamMinimum(t *testing.T) {
170 spec := `openapi: 3.1.0
171 paths:
172 /a/fishy/on/a/dishy:
173 get:
174 parameters:
175 - name: fishy
176 in: query
177 required: true
178 schema:
179 type: string
180 minLength: 4
181 operationId: locateFishy
182 `
183
184 doc, err := libopenapi.NewDocument([]byte(spec))
185 require.NoError(t, err)
186 m, errs := doc.BuildV3Model()
187 require.NoError(t, errs)
188
189 v := NewParameterValidator(&m.Model)
190
191 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=salmon", nil)
192
193 valid, errors := v.ValidateQueryParams(request)
194 assert.True(t, valid)
195
196 assert.Nil(t, errors)
197 }
198
199 func TestNewValidator_QueryParamMaximum(t *testing.T) {
200 spec := `openapi: 3.1.0
201 paths:
202 /a/fishy/on/a/dishy:
203 get:
204 parameters:
205 - name: fishy
206 in: query
207 required: true
208 schema:
209 type: string
210 maxLength: 10
211 operationId: locateFishy
212 `
213
214 doc, err := libopenapi.NewDocument([]byte(spec))
215 require.NoError(t, err)
216 m, errs := doc.BuildV3Model()
217 require.NoError(t, errs)
218
219 v := NewParameterValidator(&m.Model)
220
221 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=salmon", nil)
222
223 valid, errors := v.ValidateQueryParams(request)
224 assert.True(t, valid)
225
226 assert.Nil(t, errors)
227 }
228
229 func TestNewValidator_QueryParamPost(t *testing.T) {
230 spec := `openapi: 3.1.0
231 paths:
232 /a/fishy/on/a/dishy:
233 post:
234 parameters:
235 - name: fishy
236 in: query
237 required: true
238 schema:
239 type: string
240 operationId: locateFishy
241 `
242
243 doc, _ := libopenapi.NewDocument([]byte(spec))
244
245 m, _ := doc.BuildV3Model()
246
247 v := NewParameterValidator(&m.Model)
248
249 request, _ := http.NewRequest(http.MethodPost, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
250
251 valid, errors := v.ValidateQueryParams(request)
252 assert.True(t, valid)
253
254 assert.Nil(t, errors)
255 }
256
257 func TestNewValidator_QueryParamPut(t *testing.T) {
258 spec := `openapi: 3.1.0
259 paths:
260 /a/fishy/on/a/dishy:
261 put:
262 parameters:
263 - name: fishy
264 in: query
265 required: true
266 schema:
267 type: string
268 operationId: locateFishy
269 `
270
271 doc, _ := libopenapi.NewDocument([]byte(spec))
272
273 m, _ := doc.BuildV3Model()
274
275 v := NewParameterValidator(&m.Model)
276
277 request, _ := http.NewRequest(http.MethodPut, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
278
279 valid, errors := v.ValidateQueryParams(request)
280 assert.True(t, valid)
281
282 assert.Nil(t, errors)
283 }
284
285 func TestNewValidator_QueryParamDelete(t *testing.T) {
286 spec := `openapi: 3.1.0
287 paths:
288 /a/fishy/on/a/dishy:
289 delete:
290 parameters:
291 - name: fishy
292 in: query
293 required: true
294 schema:
295 type: string
296 operationId: locateFishy
297 `
298
299 doc, _ := libopenapi.NewDocument([]byte(spec))
300
301 m, _ := doc.BuildV3Model()
302
303 v := NewParameterValidator(&m.Model)
304
305 request, _ := http.NewRequest(http.MethodDelete, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
306
307 valid, errors := v.ValidateQueryParams(request)
308 assert.True(t, valid)
309
310 assert.Nil(t, errors)
311 }
312
313 func TestNewValidator_QueryParamOptions(t *testing.T) {
314 spec := `openapi: 3.1.0
315 paths:
316 /a/fishy/on/a/dishy:
317 options:
318 parameters:
319 - name: fishy
320 in: query
321 required: true
322 schema:
323 type: string
324 operationId: locateFishy
325 `
326
327 doc, _ := libopenapi.NewDocument([]byte(spec))
328
329 m, _ := doc.BuildV3Model()
330
331 v := NewParameterValidator(&m.Model)
332
333 request, _ := http.NewRequest(http.MethodOptions, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
334
335 valid, errors := v.ValidateQueryParams(request)
336 assert.True(t, valid)
337
338 assert.Nil(t, errors)
339 }
340
341 func TestNewValidator_QueryParamHead(t *testing.T) {
342 spec := `openapi: 3.1.0
343 paths:
344 /a/fishy/on/a/dishy:
345 head:
346 parameters:
347 - name: fishy
348 in: query
349 required: true
350 schema:
351 type: string
352 operationId: locateFishy
353 `
354
355 doc, _ := libopenapi.NewDocument([]byte(spec))
356
357 m, _ := doc.BuildV3Model()
358
359 v := NewParameterValidator(&m.Model)
360
361 request, _ := http.NewRequest(http.MethodHead, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
362
363 valid, errors := v.ValidateQueryParams(request)
364 assert.True(t, valid)
365
366 assert.Nil(t, errors)
367 }
368
369 func TestNewValidator_QueryParamPatch(t *testing.T) {
370 spec := `openapi: 3.1.0
371 paths:
372 /a/fishy/on/a/dishy:
373 patch:
374 parameters:
375 - name: fishy
376 in: query
377 required: true
378 schema:
379 type: string
380 operationId: locateFishy
381 `
382
383 doc, _ := libopenapi.NewDocument([]byte(spec))
384
385 m, _ := doc.BuildV3Model()
386
387 v := NewParameterValidator(&m.Model)
388
389 request, _ := http.NewRequest(http.MethodPatch, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
390
391 valid, errors := v.ValidateQueryParams(request)
392 assert.True(t, valid)
393
394 assert.Nil(t, errors)
395 }
396
397 func TestNewValidator_QueryParamTrace(t *testing.T) {
398 spec := `openapi: 3.1.0
399 paths:
400 /a/fishy/on/a/dishy:
401 trace:
402 parameters:
403 - name: fishy
404 in: query
405 required: true
406 schema:
407 type: string
408 operationId: locateFishy
409 `
410
411 doc, _ := libopenapi.NewDocument([]byte(spec))
412
413 m, _ := doc.BuildV3Model()
414
415 v := NewParameterValidator(&m.Model)
416
417 request, _ := http.NewRequest(http.MethodTrace, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
418
419 valid, errors := v.ValidateQueryParams(request)
420 assert.True(t, valid)
421
422 assert.Nil(t, errors)
423 }
424
425 func TestNewValidator_QueryParamBadPath(t *testing.T) {
426 spec := `openapi: 3.1.0
427 paths:
428 /a/fishy/on/a/dishy:
429 get:
430 parameters:
431 - name: fishy
432 in: query
433 required: true
434 schema:
435 type: number
436 operationId: locateFishy
437 `
438
439 doc, _ := libopenapi.NewDocument([]byte(spec))
440
441 m, _ := doc.BuildV3Model()
442
443 v := NewParameterValidator(&m.Model)
444
445 request, _ := http.NewRequest(http.MethodGet, "https://things.com/Not/Found/dishy?fishy=cod", nil)
446
447 valid, errors := v.ValidateQueryParams(request)
448 assert.False(t, valid)
449 assert.NotNil(t, errors)
450 }
451
452 func TestMewValidator_QueryParamMultiStringField(t *testing.T) {
453 spec := `openapi: 3.1.0
454 paths:
455 /a/fishy/on/a/dishy:
456 get:
457 parameters:
458 - name: fishy
459 in: query
460 required: true
461 schema:
462 type: string
463 enum: [cod, halibut]
464 - name: dishy
465 in: query
466 required: true
467 schema:
468 type: string
469 operationId: locateFishy
470 `
471
472 doc, err := libopenapi.NewDocument([]byte(spec))
473 assert.Nil(t, err)
474
475 m, _ := doc.BuildV3Model()
476
477 v := NewParameterValidator(&m.Model)
478
479 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=doc&dishy=halibut", nil)
480
481 valid, errors := v.ValidateQueryParams(request)
482 assert.False(t, valid)
483 assert.NotNil(t, errors)
484 }
485
486 func TestMewValidator_QueryParamMultiNumberField(t *testing.T) {
487 spec := `openapi: 3.1.0
488 paths:
489 /a/fishy/on/a/dishy:
490 get:
491 parameters:
492 - name: fishy
493 in: query
494 required: true
495 schema:
496 type: number
497 enum: [1, 99]
498 - name: dishy
499 in: query
500 required: true
501 schema:
502 type: number
503 operationId: locateFishy
504 `
505
506 doc, err := libopenapi.NewDocument([]byte(spec))
507 assert.Nil(t, err)
508
509 m, _ := doc.BuildV3Model()
510
511 v := NewParameterValidator(&m.Model)
512
513 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=10&dishy=10", nil)
514
515 valid, errors := v.ValidateQueryParams(request)
516 assert.False(t, valid)
517 assert.NotNil(t, errors)
518 }
519
520 func TestNewValidator_QueryParamWrongTypeNumber(t *testing.T) {
521 spec := `openapi: 3.1.0
522 paths:
523 /a/fishy/on/a/dishy:
524 get:
525 parameters:
526 - name: fishy
527 in: query
528 required: true
529 schema:
530 type: number
531 operationId: locateFishy
532 `
533
534 doc, _ := libopenapi.NewDocument([]byte(spec))
535
536 m, _ := doc.BuildV3Model()
537
538 v := NewParameterValidator(&m.Model)
539
540 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
541
542 valid, errors := v.ValidateQueryParams(request)
543 assert.False(t, valid)
544
545 assert.NotNil(t, errors)
546 assert.Equal(t, "Query parameter 'fishy' is not a valid number", errors[0].Message)
547 }
548
549 func TestNewValidator_QueryParamValidTypeNumber(t *testing.T) {
550 spec := `openapi: 3.1.0
551 paths:
552 /a/fishy/on/a/dishy:
553 get:
554 parameters:
555 - name: fishy
556 in: query
557 required: true
558 schema:
559 type: number
560 operationId: locateFishy
561 `
562
563 doc, _ := libopenapi.NewDocument([]byte(spec))
564
565 m, _ := doc.BuildV3Model()
566
567 v := NewParameterValidator(&m.Model)
568
569 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123.4", nil)
570
571 valid, errors := v.ValidateQueryParams(request)
572 assert.True(t, valid)
573
574 assert.Nil(t, errors)
575 }
576
577 func TestNewValidator_QueryParamMinimumNumber(t *testing.T) {
578 spec := `openapi: 3.1.0
579 paths:
580 /a/fishy/on/a/dishy:
581 get:
582 parameters:
583 - name: fishy
584 in: query
585 required: true
586 schema:
587 type: number
588 minimum: 200
589 operationId: locateFishy
590 `
591
592 doc, err := libopenapi.NewDocument([]byte(spec))
593 require.NoError(t, err)
594 m, errs := doc.BuildV3Model()
595 require.NoError(t, errs)
596
597 v := NewParameterValidator(&m.Model)
598
599 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=300.4", nil)
600
601 valid, errors := v.ValidateQueryParams(request)
602 assert.True(t, valid)
603
604 assert.Nil(t, errors)
605 }
606
607 func TestNewValidator_QueryParamMinimumNumber_violation(t *testing.T) {
608 spec := `openapi: 3.1.0
609 paths:
610 /a/fishy/on/a/dishy:
611 get:
612 parameters:
613 - name: fishy
614 in: query
615 required: true
616 schema:
617 type: number
618 minimum: 200
619 operationId: locateFishy
620 `
621
622 doc, err := libopenapi.NewDocument([]byte(spec))
623 require.NoError(t, err)
624 m, errs := doc.BuildV3Model()
625 require.NoError(t, errs)
626
627 v := NewParameterValidator(&m.Model)
628
629 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123.4", nil)
630
631 valid, errors := v.ValidateQueryParams(request)
632 assert.False(t, valid)
633 assert.Equal(t, 1, len(errors))
634 assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
635 }
636
637 func TestNewValidator_QueryParamMaximumNumber(t *testing.T) {
638 spec := `openapi: 3.1.0
639 paths:
640 /a/fishy/on/a/dishy:
641 get:
642 parameters:
643 - name: fishy
644 in: query
645 required: true
646 schema:
647 type: number
648 maximum: 200
649 operationId: locateFishy
650 `
651
652 doc, err := libopenapi.NewDocument([]byte(spec))
653 require.NoError(t, err)
654 m, errs := doc.BuildV3Model()
655 require.NoError(t, errs)
656
657 v := NewParameterValidator(&m.Model)
658
659 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123", nil)
660
661 valid, errors := v.ValidateQueryParams(request)
662 assert.True(t, valid)
663
664 assert.Nil(t, errors)
665 }
666
667 func TestNewValidator_QueryParamMaximumNumber_violation(t *testing.T) {
668 spec := `openapi: 3.1.0
669 paths:
670 /a/fishy/on/a/dishy:
671 get:
672 parameters:
673 - name: fishy
674 in: query
675 required: true
676 schema:
677 type: number
678 maximum: 200
679 operationId: locateFishy
680 `
681
682 doc, err := libopenapi.NewDocument([]byte(spec))
683 require.NoError(t, err)
684 m, errs := doc.BuildV3Model()
685 require.NoError(t, errs)
686
687 v := NewParameterValidator(&m.Model)
688
689 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1234.5", nil)
690
691 valid, errors := v.ValidateQueryParams(request)
692 assert.False(t, valid)
693 assert.Equal(t, 1, len(errors))
694 assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
695 }
696
697 func TestNewValidator_QueryParamValidTypeFloat(t *testing.T) {
698 spec := `openapi: 3.1.0
699 paths:
700 /a/fishy/on/a/dishy:
701 get:
702 parameters:
703 - name: fishy
704 in: query
705 required: true
706 schema:
707 type: number
708 operationId: locateFishy
709 `
710
711 doc, _ := libopenapi.NewDocument([]byte(spec))
712
713 m, _ := doc.BuildV3Model()
714
715 v := NewParameterValidator(&m.Model)
716
717 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123.223", nil)
718
719 valid, errors := v.ValidateQueryParams(request)
720 assert.True(t, valid)
721
722 assert.Nil(t, errors)
723 }
724
725 func TestNewValidator_QueryParamWrongTypeInteger_StringValue(t *testing.T) {
726 spec := `openapi: 3.1.0
727 paths:
728 /a/fishy/on/a/dishy:
729 get:
730 parameters:
731 - name: fishy
732 in: query
733 required: true
734 schema:
735 type: integer
736 operationId: locateFishy
737 `
738
739 doc, _ := libopenapi.NewDocument([]byte(spec))
740
741 m, _ := doc.BuildV3Model()
742
743 v := NewParameterValidator(&m.Model)
744
745 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
746
747 valid, errors := v.ValidateQueryParams(request)
748 assert.False(t, valid)
749
750 assert.NotNil(t, errors)
751 assert.Equal(t, "Query parameter 'fishy' is not a valid integer", errors[0].Message)
752 }
753
754 func TestNewValidator_QueryParamWrongTypeInteger_FloatValue(t *testing.T) {
755 spec := `openapi: 3.1.0
756 paths:
757 /a/fishy/on/a/dishy:
758 get:
759 parameters:
760 - name: fishy
761 in: query
762 required: true
763 schema:
764 type: integer
765 operationId: locateFishy
766 `
767
768 doc, _ := libopenapi.NewDocument([]byte(spec))
769
770 m, _ := doc.BuildV3Model()
771
772 v := NewParameterValidator(&m.Model)
773
774 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1.2", nil)
775
776 valid, errors := v.ValidateQueryParams(request)
777 assert.False(t, valid)
778
779 assert.NotNil(t, errors)
780 assert.Equal(t, "Query parameter 'fishy' is not a valid integer", errors[0].Message)
781 }
782
783 func TestNewValidator_QueryParamValidTypeInteger(t *testing.T) {
784 spec := `openapi: 3.1.0
785 paths:
786 /a/fishy/on/a/dishy:
787 get:
788 parameters:
789 - name: fishy
790 in: query
791 required: true
792 schema:
793 type: integer
794 operationId: locateFishy
795 `
796
797 doc, _ := libopenapi.NewDocument([]byte(spec))
798
799 m, _ := doc.BuildV3Model()
800
801 v := NewParameterValidator(&m.Model)
802
803 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123", nil)
804
805 valid, errors := v.ValidateQueryParams(request)
806 assert.True(t, valid)
807
808 assert.Nil(t, errors)
809 }
810
811 func TestNewValidator_QueryParamMinimumInteger(t *testing.T) {
812 spec := `openapi: 3.1.0
813 paths:
814 /a/fishy/on/a/dishy:
815 get:
816 parameters:
817 - name: fishy
818 in: query
819 required: true
820 schema:
821 type: integer
822 minimum: 200
823 operationId: locateFishy
824 `
825
826 doc, err := libopenapi.NewDocument([]byte(spec))
827 require.NoError(t, err)
828 m, errs := doc.BuildV3Model()
829 require.NoError(t, errs)
830
831 v := NewParameterValidator(&m.Model)
832
833 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=300", nil)
834
835 valid, errors := v.ValidateQueryParams(request)
836 assert.True(t, valid)
837
838 assert.Nil(t, errors)
839 }
840
841 func TestNewValidator_QueryParamMinimumInteger_violation(t *testing.T) {
842 spec := `openapi: 3.1.0
843 paths:
844 /a/fishy/on/a/dishy:
845 get:
846 parameters:
847 - name: fishy
848 in: query
849 required: true
850 schema:
851 type: integer
852 minimum: 200
853 operationId: locateFishy
854 `
855
856 doc, err := libopenapi.NewDocument([]byte(spec))
857 require.NoError(t, err)
858 m, errs := doc.BuildV3Model()
859 require.NoError(t, errs)
860
861 v := NewParameterValidator(&m.Model)
862
863 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123", nil)
864
865 valid, errors := v.ValidateQueryParams(request)
866 assert.False(t, valid)
867 assert.Equal(t, 1, len(errors))
868 assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
869 }
870
871 func TestNewValidator_QueryParamMaximumInteger(t *testing.T) {
872 spec := `openapi: 3.1.0
873 paths:
874 /a/fishy/on/a/dishy:
875 get:
876 parameters:
877 - name: fishy
878 in: query
879 required: true
880 schema:
881 type: integer
882 maximum: 200
883 operationId: locateFishy
884 `
885
886 doc, err := libopenapi.NewDocument([]byte(spec))
887 require.NoError(t, err)
888 m, errs := doc.BuildV3Model()
889 require.NoError(t, errs)
890
891 v := NewParameterValidator(&m.Model)
892
893 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=123", nil)
894
895 valid, errors := v.ValidateQueryParams(request)
896 assert.True(t, valid)
897 assert.Nil(t, errors)
898 }
899
900 func TestNewValidator_QueryParamMaximumInteger_violation(t *testing.T) {
901 spec := `openapi: 3.1.0
902 paths:
903 /a/fishy/on/a/dishy:
904 get:
905 parameters:
906 - name: fishy
907 in: query
908 required: true
909 schema:
910 type: integer
911 maximum: 200
912 operationId: locateFishy
913 `
914
915 doc, err := libopenapi.NewDocument([]byte(spec))
916 require.NoError(t, err)
917 m, errs := doc.BuildV3Model()
918 require.NoError(t, errs)
919
920 v := NewParameterValidator(&m.Model)
921
922 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1234", nil)
923
924 valid, errors := v.ValidateQueryParams(request)
925 assert.False(t, valid)
926 assert.Equal(t, 1, len(errors))
927 assert.Equal(t, "Query parameter 'fishy' failed to validate", errors[0].Message)
928 }
929
930 func TestNewValidator_QueryParamWrongTypeBool(t *testing.T) {
931 spec := `openapi: 3.1.0
932 paths:
933 /a/fishy/on/a/dishy:
934 get:
935 parameters:
936 - name: fishy
937 in: query
938 required: true
939 schema:
940 type: boolean
941 operationId: locateFishy
942 `
943
944 doc, _ := libopenapi.NewDocument([]byte(spec))
945
946 m, _ := doc.BuildV3Model()
947
948 v := NewParameterValidator(&m.Model)
949
950 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod", nil)
951
952 valid, errors := v.ValidateQueryParams(request)
953 assert.False(t, valid)
954
955 assert.NotNil(t, errors)
956 assert.Equal(t, "Query parameter 'fishy' is not a valid boolean", errors[0].Message)
957 }
958
959 func TestNewValidator_QueryParamValidTypeBool(t *testing.T) {
960 spec := `openapi: 3.1.0
961 paths:
962 /a/fishy/on/a/dishy:
963 get:
964 parameters:
965 - name: fishy
966 in: query
967 required: true
968 schema:
969 type: boolean
970 operationId: locateFishy
971 `
972
973 doc, _ := libopenapi.NewDocument([]byte(spec))
974
975 m, _ := doc.BuildV3Model()
976
977 v := NewParameterValidator(&m.Model)
978
979 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=true", nil)
980
981 valid, errors := v.ValidateQueryParams(request)
982 assert.True(t, valid)
983
984 assert.Nil(t, errors)
985 }
986
987 func TestNewValidator_QueryParamInvalidEnumString(t *testing.T) {
988 spec := `openapi: 3.1.0
989 paths:
990 /a/fishy/on/a/dishy:
991 get:
992 parameters:
993 - name: fishy
994 in: query
995 required: true
996 schema:
997 type: string
998 enum: [cod, halibut]`
999
1000 doc, _ := libopenapi.NewDocument([]byte(spec))
1001
1002 m, _ := doc.BuildV3Model()
1003
1004 v := NewParameterValidator(&m.Model)
1005
1006 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock", nil)
1007
1008 valid, errors := v.ValidateQueryParams(request)
1009 assert.False(t, valid)
1010 assert.Len(t, errors, 1)
1011 assert.Equal(t, "Query parameter 'fishy' does not match allowed values", errors[0].Message)
1012 assert.Equal(t, "Instead of 'haddock', use one of the allowed values: 'cod, halibut'", errors[0].HowToFix)
1013 }
1014
1015 func TestNewValidator_QueryParamInvalidEnumInteger(t *testing.T) {
1016 spec := `openapi: 3.1.0
1017 paths:
1018 /a/fishy/on/a/dishy:
1019 get:
1020 parameters:
1021 - name: fishy
1022 in: query
1023 required: true
1024 schema:
1025 type: integer
1026 enum: [1, 99]`
1027
1028 doc, _ := libopenapi.NewDocument([]byte(spec))
1029
1030 m, _ := doc.BuildV3Model()
1031
1032 v := NewParameterValidator(&m.Model)
1033
1034 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=22", nil)
1035
1036 valid, errors := v.ValidateQueryParams(request)
1037 assert.False(t, valid)
1038 assert.Len(t, errors, 1)
1039 assert.Equal(t, "Query parameter 'fishy' does not match allowed values", errors[0].Message)
1040 assert.Equal(t, "Instead of '22', use one of the allowed values: '1, 99'", errors[0].HowToFix)
1041 }
1042
1043 func TestNewValidator_QueryParamValidEnumInteger(t *testing.T) {
1044 spec := `openapi: 3.1.0
1045 paths:
1046 /a/fishy/on/a/dishy:
1047 get:
1048 parameters:
1049 - name: fishy
1050 in: query
1051 required: true
1052 schema:
1053 type: integer
1054 enum: [1, 99]`
1055
1056 doc, _ := libopenapi.NewDocument([]byte(spec))
1057
1058 m, _ := doc.BuildV3Model()
1059
1060 v := NewParameterValidator(&m.Model)
1061
1062 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1", nil)
1063
1064 valid, errors := v.ValidateQueryParams(request)
1065 assert.True(t, valid)
1066 assert.Len(t, errors, 0)
1067 }
1068
1069 func TestNewValidator_QueryParamInvalidEnumNumber(t *testing.T) {
1070 spec := `openapi: 3.1.0
1071 paths:
1072 /a/fishy/on/a/dishy:
1073 get:
1074 parameters:
1075 - name: fishy
1076 in: query
1077 required: true
1078 schema:
1079 type: number
1080 enum: [1.2, 99.8]`
1081
1082 doc, _ := libopenapi.NewDocument([]byte(spec))
1083
1084 m, _ := doc.BuildV3Model()
1085
1086 v := NewParameterValidator(&m.Model)
1087
1088 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=22.4", nil)
1089
1090 valid, errors := v.ValidateQueryParams(request)
1091 assert.False(t, valid)
1092 assert.Len(t, errors, 1)
1093 assert.Equal(t, "Query parameter 'fishy' does not match allowed values", errors[0].Message)
1094 assert.Equal(t, "Instead of '22.4', use one of the allowed values: '1.2, 99.8'", errors[0].HowToFix)
1095 }
1096
1097 func TestNewValidator_QueryParamValidEnumNumber(t *testing.T) {
1098 spec := `openapi: 3.1.0
1099 paths:
1100 /a/fishy/on/a/dishy:
1101 get:
1102 parameters:
1103 - name: fishy
1104 in: query
1105 required: true
1106 schema:
1107 type: number
1108 enum: [1.2, 99.8]`
1109
1110 doc, _ := libopenapi.NewDocument([]byte(spec))
1111
1112 m, _ := doc.BuildV3Model()
1113
1114 v := NewParameterValidator(&m.Model)
1115
1116 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1.2", nil)
1117
1118 valid, errors := v.ValidateQueryParams(request)
1119 assert.True(t, valid)
1120 assert.Len(t, errors, 0)
1121 }
1122
1123 func TestNewValidator_QueryParamValidDateFormat(t *testing.T) {
1124 spec := `openapi: 3.1.0
1125 paths:
1126 /a/fishy/on/a/dishy:
1127 get:
1128 parameters:
1129 - name: fishy
1130 in: query
1131 required: true
1132 schema:
1133 type: string
1134 format: date`
1135
1136 doc, _ := libopenapi.NewDocument([]byte(spec))
1137
1138 m, _ := doc.BuildV3Model()
1139
1140 v := NewParameterValidator(&m.Model, config.WithFormatAssertions())
1141
1142 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=2024-12-25", nil)
1143
1144 valid, errors := v.ValidateQueryParams(request)
1145 assert.True(t, valid)
1146 assert.Empty(t, errors)
1147 }
1148
1149 func TestNewValidator_QueryParamInvalidDateFormat(t *testing.T) {
1150 spec := `openapi: 3.1.0
1151 paths:
1152 /a/fishy/on/a/dishy:
1153 get:
1154 parameters:
1155 - name: fishy
1156 in: query
1157 required: true
1158 schema:
1159 type: string
1160 format: date`
1161
1162 doc, _ := libopenapi.NewDocument([]byte(spec))
1163
1164 m, _ := doc.BuildV3Model()
1165
1166 v := NewParameterValidator(&m.Model, config.WithFormatAssertions())
1167
1168 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=12/25/2024", nil)
1169
1170 valid, errors := v.ValidateQueryParams(request)
1171 assert.False(t, valid)
1172 assert.Len(t, errors, 1)
1173 }
1174
1175 func TestNewValidator_QueryParamValidDateTimeFormat(t *testing.T) {
1176 spec := `openapi: 3.1.0
1177 paths:
1178 /a/fishy/on/a/dishy:
1179 get:
1180 parameters:
1181 - name: fishy
1182 in: query
1183 required: true
1184 schema:
1185 type: string
1186 format: date-time`
1187
1188 doc, _ := libopenapi.NewDocument([]byte(spec))
1189
1190 m, _ := doc.BuildV3Model()
1191
1192 v := NewParameterValidator(&m.Model, config.WithFormatAssertions())
1193
1194 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=2024-12-25T13:42:42Z", nil)
1195
1196 valid, errors := v.ValidateQueryParams(request)
1197 assert.True(t, valid)
1198 assert.Empty(t, errors)
1199 }
1200
1201 func TestNewValidator_QueryParamInvalidDateTimeFormat(t *testing.T) {
1202 spec := `openapi: 3.1.0
1203 paths:
1204 /a/fishy/on/a/dishy:
1205 get:
1206 parameters:
1207 - name: fishy
1208 in: query
1209 required: true
1210 schema:
1211 type: string
1212 format: date-time`
1213
1214 doc, _ := libopenapi.NewDocument([]byte(spec))
1215
1216 m, _ := doc.BuildV3Model()
1217
1218 v := NewParameterValidator(&m.Model, config.WithFormatAssertions())
1219
1220 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=2024-12-25", nil)
1221
1222 valid, errors := v.ValidateQueryParams(request)
1223 assert.False(t, valid)
1224 assert.Len(t, errors, 1)
1225 }
1226
1227 func TestNewValidator_QueryParamValidTypeArrayString(t *testing.T) {
1228 spec := `openapi: 3.1.0
1229 paths:
1230 /a/fishy/on/a/dishy:
1231 get:
1232 parameters:
1233 - name: fishy
1234 in: query
1235 required: true
1236 schema:
1237 type: array
1238 items:
1239 type: string
1240 operationId: locateFishy
1241 `
1242
1243 doc, _ := libopenapi.NewDocument([]byte(spec))
1244
1245 m, _ := doc.BuildV3Model()
1246
1247 v := NewParameterValidator(&m.Model)
1248
1249 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock", nil)
1250
1251 valid, errors := v.ValidateQueryParams(request)
1252 assert.True(t, valid)
1253 assert.Nil(t, errors)
1254 }
1255
1256 func TestNewValidator_QueryParamInvalidTypeArrayStringEnum(t *testing.T) {
1257 spec := `openapi: 3.1.0
1258 paths:
1259 /a/fishy/on/a/dishy:
1260 get:
1261 parameters:
1262 - name: fishy
1263 in: query
1264 required: true
1265 schema:
1266 type: array
1267 items:
1268 type: string
1269 enum: [cod, halibut]
1270 operationId: locateFishy
1271 `
1272
1273 doc, _ := libopenapi.NewDocument([]byte(spec))
1274
1275 m, _ := doc.BuildV3Model()
1276
1277 v := NewParameterValidator(&m.Model)
1278
1279 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock", nil)
1280
1281 valid, errors := v.ValidateQueryParams(request)
1282 assert.False(t, valid)
1283 assert.Len(t, errors, 1)
1284 assert.Equal(t, "Instead of 'haddock', use one of the allowed values: 'cod, halibut'", errors[0].HowToFix)
1285 }
1286
1287 func TestNewValidator_QueryParamInvalidTypeArrayIntegerEnum(t *testing.T) {
1288 spec := `openapi: 3.1.0
1289 paths:
1290 /a/fishy/on/a/dishy:
1291 get:
1292 parameters:
1293 - name: fishy
1294 in: query
1295 required: true
1296 schema:
1297 type: array
1298 items:
1299 type: integer
1300 enum: [1, 99]
1301 operationId: locateFishy
1302 `
1303
1304 doc, _ := libopenapi.NewDocument([]byte(spec))
1305
1306 m, _ := doc.BuildV3Model()
1307
1308 v := NewParameterValidator(&m.Model)
1309
1310 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1&fishy=2", nil)
1311
1312 valid, errors := v.ValidateQueryParams(request)
1313 assert.False(t, valid)
1314 assert.Len(t, errors, 1)
1315 assert.Equal(t, "Instead of '2', use one of the allowed values: '1, 99'", errors[0].HowToFix)
1316 }
1317
1318 func TestNewValidator_QueryParamInvalidTypeArrayInteger(t *testing.T) {
1319 spec := `openapi: 3.1.0
1320 paths:
1321 /a/fishy/on/a/dishy:
1322 get:
1323 parameters:
1324 - name: fishy
1325 in: query
1326 required: true
1327 schema:
1328 type: array
1329 items:
1330 type: integer
1331 operationId: locateFishy
1332 `
1333
1334 doc, _ := libopenapi.NewDocument([]byte(spec))
1335
1336 m, _ := doc.BuildV3Model()
1337
1338 v := NewParameterValidator(&m.Model)
1339
1340 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock", nil)
1341
1342 valid, errors := v.ValidateQueryParams(request)
1343 assert.False(t, valid)
1344
1345 assert.Len(t, errors, 2)
1346 assert.Equal(t, "Query array parameter 'fishy' is not a valid integer", errors[0].Message)
1347 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as being an integer, "+
1348 "however the value 'cod' is not a valid integer", errors[0].Reason)
1349 assert.Equal(t, "Query array parameter 'fishy' is not a valid integer", errors[1].Message)
1350 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as being an integer, "+
1351 "however the value 'haddock' is not a valid integer", errors[1].Reason)
1352 }
1353
1354 func TestNewValidator_QueryParamInvalidTypeArrayNumberEnum(t *testing.T) {
1355 spec := `openapi: 3.1.0
1356 paths:
1357 /a/fishy/on/a/dishy:
1358 get:
1359 parameters:
1360 - name: fishy
1361 in: query
1362 required: true
1363 schema:
1364 type: array
1365 items:
1366 type: number
1367 enum: [1.2, 99.8]
1368 operationId: locateFishy
1369 `
1370
1371 doc, _ := libopenapi.NewDocument([]byte(spec))
1372
1373 m, _ := doc.BuildV3Model()
1374
1375 v := NewParameterValidator(&m.Model)
1376
1377 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1.2&fishy=2.3", nil)
1378
1379 valid, errors := v.ValidateQueryParams(request)
1380 assert.False(t, valid)
1381 assert.Len(t, errors, 1)
1382 assert.Equal(t, "Instead of '2.3', use one of the allowed values: '1.2, 99.8'", errors[0].HowToFix)
1383 }
1384
1385 func TestNewValidator_QueryParamInvalidTypeArrayNumber(t *testing.T) {
1386 spec := `openapi: 3.1.0
1387 paths:
1388 /a/fishy/on/a/dishy:
1389 get:
1390 parameters:
1391 - name: fishy
1392 in: query
1393 required: true
1394 schema:
1395 type: array
1396 items:
1397 type: number
1398 operationId: locateFishy
1399 `
1400
1401 doc, _ := libopenapi.NewDocument([]byte(spec))
1402
1403 m, _ := doc.BuildV3Model()
1404
1405 v := NewParameterValidator(&m.Model)
1406
1407 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock", nil)
1408
1409 valid, errors := v.ValidateQueryParams(request)
1410 assert.False(t, valid)
1411
1412 assert.Len(t, errors, 2)
1413 assert.Equal(t, "Query array parameter 'fishy' is not a valid number", errors[0].Message)
1414 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as being a number, "+
1415 "however the value 'cod' is not a valid number", errors[0].Reason)
1416 assert.Equal(t, "Query array parameter 'fishy' is not a valid number", errors[1].Message)
1417 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as being a number, "+
1418 "however the value 'haddock' is not a valid number", errors[1].Reason)
1419 }
1420
1421 func TestNewValidator_QueryParamValidEnumStringType(t *testing.T) {
1422 spec := `openapi: 3.1.0
1423 paths:
1424 /a/fishy/on/a/dishy:
1425 get:
1426 parameters:
1427 - name: fishy
1428 in: query
1429 required: true
1430 schema:
1431 type: array
1432 items:
1433 type: string
1434 enum: [cod, haddock]
1435 operationId: locateFishy
1436 `
1437
1438 doc, _ := libopenapi.NewDocument([]byte(spec))
1439
1440 m, _ := doc.BuildV3Model()
1441
1442 v := NewParameterValidator(&m.Model)
1443
1444 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod,haddock", nil)
1445
1446 valid, errors := v.ValidateQueryParams(request)
1447 assert.True(t, valid)
1448
1449 assert.Len(t, errors, 0)
1450 }
1451
1452 func TestNewValidator_QueryParamValidExplodedType(t *testing.T) {
1453 spec := `openapi: 3.1.0
1454 paths:
1455 /a/fishy/on/a/dishy:
1456 get:
1457 parameters:
1458 - name: fishy
1459 in: query
1460 required: true
1461 explode: true
1462 schema:
1463 type: array
1464 items:
1465 type: string
1466 operationId: locateFishy
1467 `
1468
1469 doc, _ := libopenapi.NewDocument([]byte(spec))
1470
1471 m, _ := doc.BuildV3Model()
1472
1473 v := NewParameterValidator(&m.Model)
1474
1475 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod,haddock", nil)
1476
1477 valid, errors := v.ValidateQueryParams(request)
1478 assert.False(t, valid)
1479
1480 assert.Len(t, errors, 2)
1481 }
1482
1483 func TestNewValidator_QueryParamValidExplodedArray(t *testing.T) {
1484 spec := `openapi: 3.1.0
1485 paths:
1486 /a/fishy/on/a/dishy:
1487 get:
1488 parameters:
1489 - name: fishy
1490 in: query
1491 required: true
1492 explode: true
1493 schema:
1494 type: array
1495 items:
1496 type: number
1497 operationId: locateFishy`
1498
1499 doc, _ := libopenapi.NewDocument([]byte(spec))
1500
1501 m, _ := doc.BuildV3Model()
1502
1503 v := NewParameterValidator(&m.Model)
1504
1505 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=1&fishy=2", nil)
1506 valid, errors := v.ValidateQueryParams(request)
1507 assert.True(t, valid)
1508 assert.Len(t, errors, 0)
1509 }
1510
1511 func TestNewValidator_QueryParamInvalidExplodedArrayAndInvalidType(t *testing.T) {
1512 spec := `openapi: 3.1.0
1513 paths:
1514 /a/fishy/on/a/dishy:
1515 get:
1516 parameters:
1517 - name: fishy
1518 in: query
1519 required: true
1520 explode: true
1521 schema:
1522 type: array
1523 items:
1524 type: number
1525 operationId: locateFishy`
1526
1527 doc, _ := libopenapi.NewDocument([]byte(spec))
1528
1529 m, _ := doc.BuildV3Model()
1530
1531 v := NewParameterValidator(&m.Model)
1532
1533 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=haddock&fishy=cod", nil)
1534
1535 valid, errors := v.ValidateQueryParams(request)
1536 assert.False(t, valid)
1537
1538 assert.Len(t, errors, 2)
1539 }
1540
1541 func TestNewValidator_QueryParamValidExploded(t *testing.T) {
1542 spec := `openapi: 3.1.0
1543 paths:
1544 /a/fishy/on/a/dishy:
1545 get:
1546 parameters:
1547 - name: fishy
1548 in: query
1549 required: true
1550 explode: false
1551 schema:
1552 type: array
1553 items:
1554 type: string
1555 operationId: locateFishy`
1556
1557 doc, _ := libopenapi.NewDocument([]byte(spec))
1558
1559 m, _ := doc.BuildV3Model()
1560
1561 v := NewParameterValidator(&m.Model)
1562
1563 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod,haddock,mackrel", nil)
1564
1565 valid, errors := v.ValidateQueryParams(request)
1566 assert.True(t, valid)
1567
1568 assert.Len(t, errors, 0)
1569 }
1570
1571 func TestNewValidator_QueryParamInvalidTypeArrayBool(t *testing.T) {
1572 spec := `openapi: 3.1.0
1573 paths:
1574 /a/fishy/on/a/dishy:
1575 get:
1576 parameters:
1577 - name: fishy
1578 in: query
1579 required: true
1580 schema:
1581 type: array
1582 items:
1583 type: boolean
1584 operationId: locateFishy`
1585
1586 doc, _ := libopenapi.NewDocument([]byte(spec))
1587
1588 m, _ := doc.BuildV3Model()
1589
1590 v := NewParameterValidator(&m.Model)
1591
1592 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock", nil)
1593
1594 valid, errors := v.ValidateQueryParams(request)
1595 assert.False(t, valid)
1596
1597 assert.Len(t, errors, 2)
1598 assert.Equal(t, "Query array parameter 'fishy' is not a valid boolean", errors[0].Message)
1599 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as being a boolean, "+
1600 "however the value 'cod' is not a valid true/false value", errors[0].Reason)
1601 assert.Equal(t, "Query array parameter 'fishy' is not a valid boolean", errors[1].Message)
1602 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as being a boolean, "+
1603 "however the value 'haddock' is not a valid true/false value", errors[1].Reason)
1604 }
1605
1606 func TestNewValidator_QueryParamInvalidTypeArrayFloat(t *testing.T) {
1607 spec := `openapi: 3.1.0
1608 paths:
1609 /a/fishy/on/a/dishy:
1610 get:
1611 parameters:
1612 - name: fishy
1613 in: query
1614 required: true
1615 schema:
1616 type: array
1617 items:
1618 type: number
1619 operationId: locateFishy`
1620
1621 doc, _ := libopenapi.NewDocument([]byte(spec))
1622
1623 m, _ := doc.BuildV3Model()
1624
1625 v := NewParameterValidator(&m.Model)
1626
1627 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=12&fishy=12.12&fishy=1234567789.1233456657", nil)
1628 valid, errors := v.ValidateQueryParams(request)
1629 assert.True(t, valid)
1630 assert.Len(t, errors, 0)
1631 }
1632
1633 func TestNewValidator_QueryParamInvalidTypeArrayFloatPipeDelimited(t *testing.T) {
1634 spec := `openapi: 3.1.0
1635 paths:
1636 /a/fishy/on/a/dishy:
1637 get:
1638 parameters:
1639 - name: fishy
1640 in: query
1641 style: pipeDelimited
1642 required: true
1643 schema:
1644 type: array
1645 items:
1646 type: number
1647 operationId: locateFishy`
1648
1649 doc, _ := libopenapi.NewDocument([]byte(spec))
1650
1651 m, _ := doc.BuildV3Model()
1652
1653 v := NewParameterValidator(&m.Model)
1654
1655 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=12|12345.2344|22111233444.342452435", nil)
1656 valid, errors := v.ValidateQueryParams(request)
1657 assert.True(t, valid)
1658 assert.Len(t, errors, 0)
1659 }
1660
1661 func TestNewValidator_QueryParamInvalidTypeArrayObjectPipeDelimited(t *testing.T) {
1662 spec := `openapi: 3.1.0
1663 paths:
1664 /a/fishy/on/a/dishy:
1665 get:
1666 parameters:
1667 - name: fishy
1668 in: query
1669 style: pipeDelimited
1670 required: true
1671 schema:
1672 type: object
1673 properties:
1674 ocean:
1675 type: number
1676 silver:
1677 type: number
1678 required: [ocean, silver]
1679 operationId: locateFishy`
1680
1681 doc, _ := libopenapi.NewDocument([]byte(spec))
1682
1683 m, _ := doc.BuildV3Model()
1684
1685 v := NewParameterValidator(&m.Model)
1686
1687 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=ocean|12|silver|12.2345", nil)
1688 valid, errors := v.ValidateQueryParams(request)
1689 assert.True(t, valid)
1690 assert.Len(t, errors, 0)
1691 }
1692
1693 func TestNewValidator_QueryParamValidTypeObject(t *testing.T) {
1694 spec := `openapi: 3.1.0
1695 paths:
1696 /a/fishy/on/a/dishy:
1697 get:
1698 parameters:
1699 - name: fishy
1700 in: query
1701 required: true
1702 schema:
1703 type: array
1704 items:
1705 type: object
1706 properties:
1707 vinegar:
1708 type: boolean
1709 chips:
1710 type: number
1711 required:
1712 - vinegar
1713 - chips
1714 operationId: locateFishy`
1715
1716 doc, _ := libopenapi.NewDocument([]byte(spec))
1717
1718 m, _ := doc.BuildV3Model()
1719
1720 v := NewParameterValidator(&m.Model)
1721
1722 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"cod\":\"cakes\"}&fishy={\"crab\":\"legs\"}", nil)
1723
1724 valid, errors := v.ValidateQueryParams(request)
1725 assert.False(t, valid)
1726
1727 assert.Len(t, errors, 2)
1728 assert.Equal(t, "Query array parameter 'fishy' failed to validate", errors[0].Message)
1729 assert.Equal(t, "The query parameter (which is an array) 'fishy' is defined as an object, "+
1730 "however it failed to pass a schema validation", errors[0].Reason)
1731 assert.Equal(t, "missing properties 'vinegar', 'chips'", errors[0].SchemaValidationErrors[0].Reason)
1732 assert.Equal(t, "/required", errors[0].SchemaValidationErrors[0].KeywordLocation)
1733 }
1734
1735 func TestNewValidator_QueryParamValidTypeObjectPropType_Invalid(t *testing.T) {
1736 spec := `openapi: 3.1.0
1737 paths:
1738 /a/fishy/on/a/dishy:
1739 get:
1740 parameters:
1741 - name: fishy
1742 in: query
1743 required: true
1744 content:
1745 application/json:
1746 schema:
1747 type: object
1748 properties:
1749 vinegar:
1750 type: boolean
1751 chips:
1752 type: number
1753 required:
1754 - vinegar
1755 - chips
1756 operationId: locateFishy`
1757
1758 doc, _ := libopenapi.NewDocument([]byte(spec))
1759
1760 m, _ := doc.BuildV3Model()
1761
1762 v := NewParameterValidator(&m.Model)
1763
1764 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":\"cakes\",\"chips\":\"hello\"}&fishy={\"vinegar\":true,\"chips\":123.223}", nil)
1765 valid, errors := v.ValidateQueryParams(request)
1766 assert.False(t, valid)
1767 assert.Len(t, errors, 1)
1768 assert.Len(t, errors[0].SchemaValidationErrors, 2)
1769 }
1770
1771 func TestNewValidator_QueryParamValidTypeObjectPropTypeFloat(t *testing.T) {
1772 spec := `openapi: 3.1.0
1773 paths:
1774 /a/fishy/on/a/dishy:
1775 get:
1776 parameters:
1777 - name: fishy
1778 in: query
1779 required: true
1780 content:
1781 application/json:
1782 schema:
1783 type: object
1784 properties:
1785 vinegar:
1786 type: boolean
1787 chips:
1788 type: number
1789 required:
1790 - vinegar
1791 - chips
1792 operationId: locateFishy`
1793
1794 doc, _ := libopenapi.NewDocument([]byte(spec))
1795
1796 m, _ := doc.BuildV3Model()
1797
1798 v := NewParameterValidator(&m.Model)
1799
1800 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":true,\"chips\":12}&fishy={\"vinegar\":true,\"chips\":123.333}", nil)
1801 valid, errors := v.ValidateQueryParams(request)
1802 assert.True(t, valid)
1803 assert.Len(t, errors, 0)
1804 }
1805
1806 func TestNewValidator_QueryParamInvalidTypeObjectArrayPropType_Ref(t *testing.T) {
1807 spec := `openapi: 3.1.0
1808 components:
1809 parameters:
1810 something:
1811 name: somethingElse
1812 in: query
1813 content:
1814 application/json:
1815 schema:
1816 type: array
1817 items:
1818 type: object
1819 properties:
1820 vinegar:
1821 type: boolean
1822 chips:
1823 type: number
1824 required:
1825 - vinegar
1826 - chips
1827 paths:
1828 /a/fishy/on/a/dishy:
1829 get:
1830 parameters:
1831 - name: fishy
1832 in: query
1833 required: true
1834 content:
1835 $ref: "#/components/parameters/something/content"
1836 operationId: locateFishy
1837 `
1838
1839 doc, _ := libopenapi.NewDocument([]byte(spec))
1840
1841 m, _ := doc.BuildV3Model()
1842
1843 v := NewParameterValidator(&m.Model)
1844
1845 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":\"cakes\",\"chips\":\"hello\"}&fishy={\"vinegar\":true,\"chips\":123}", nil)
1846 valid, errors := v.ValidateQueryParams(request)
1847 assert.False(t, valid)
1848 assert.Len(t, errors, 1)
1849 assert.Len(t, errors[0].SchemaValidationErrors, 2)
1850 }
1851
1852 func TestNewValidator_QueryParamValidTypeObjectArrayPropType_Ref(t *testing.T) {
1853 spec := `openapi: 3.1.0
1854 components:
1855 parameters:
1856 something:
1857 name: somethingElse
1858 in: query
1859 content:
1860 application/json:
1861 schema:
1862 type: array
1863 items:
1864 type: object
1865 properties:
1866 vinegar:
1867 type: boolean
1868 chips:
1869 type: number
1870 required:
1871 - vinegar
1872 - chips
1873 paths:
1874 /a/fishy/on/a/dishy:
1875 get:
1876 parameters:
1877 - name: fishy
1878 in: query
1879 required: true
1880 content:
1881 $ref: "#/components/parameters/something/content"
1882 operationId: locateFishy
1883 `
1884
1885 doc, _ := libopenapi.NewDocument([]byte(spec))
1886
1887 m, _ := doc.BuildV3Model()
1888
1889 v := NewParameterValidator(&m.Model)
1890
1891 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":false,\"chips\":999}&fishy={\"vinegar\":true,\"chips\":123}", nil)
1892 valid, errors := v.ValidateQueryParams(request)
1893 assert.True(t, valid)
1894 assert.Len(t, errors, 0)
1895 }
1896
1897 func TestNewValidator_QueryParamValidTypeObjectPropType_Ref(t *testing.T) {
1898 spec := `openapi: 3.1.0
1899 components:
1900 parameters:
1901 fishy:
1902 name: fishy
1903 in: query
1904 schema:
1905 type: object
1906 properties:
1907 vinegar:
1908 type: boolean
1909 chips:
1910 type: number
1911 required:
1912 - vinegar
1913 - chips
1914 paths:
1915 /a/fishy/on/a/dishy:
1916 get:
1917 parameters:
1918 - $ref: "#/components/parameters/fishy"
1919 operationId: locateFishy
1920 `
1921
1922 doc, _ := libopenapi.NewDocument([]byte(spec))
1923
1924 m, _ := doc.BuildV3Model()
1925
1926 v := NewParameterValidator(&m.Model)
1927
1928 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?vinegar=true&chips=12", nil)
1929 valid, errors := v.ValidateQueryParams(request)
1930 assert.True(t, valid)
1931 assert.Len(t, errors, 0)
1932 }
1933
1934 func TestNewValidator_QueryParamValidTypeObjectPropType_RefInvalid(t *testing.T) {
1935 spec := `openapi: 3.1.0
1936 components:
1937 parameters:
1938 fishy:
1939 name: fishy
1940 in: query
1941 schema:
1942 type: object
1943 properties:
1944 vinegar:
1945 type: boolean
1946 chips:
1947 type: number
1948 required:
1949 - vinegar
1950 - chips
1951 paths:
1952 /a/fishy/on/a/dishy:
1953 get:
1954 parameters:
1955 - $ref: "#/components/parameters/fishy"
1956 operationId: locateFishy
1957 `
1958
1959 doc, _ := libopenapi.NewDocument([]byte(spec))
1960
1961 m, _ := doc.BuildV3Model()
1962
1963 v := NewParameterValidator(&m.Model)
1964
1965 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?vinegar=true&chips=false", nil)
1966 valid, errors := v.ValidateQueryParams(request)
1967 assert.False(t, valid)
1968 assert.Len(t, errors, 1)
1969 assert.Equal(t, "got boolean, want number", errors[0].SchemaValidationErrors[0].Reason)
1970 }
1971
1972 func TestNewValidator_QueryParamValidTypeObjectPropType_RefViaContentWrapped(t *testing.T) {
1973 spec := `openapi: 3.1.0
1974 components:
1975 parameters:
1976 fishy:
1977 name: fishy
1978 in: query
1979 content:
1980 application/json:
1981 schema:
1982 type: object
1983 properties:
1984 vinegar:
1985 type: boolean
1986 chips:
1987 type: number
1988 required:
1989 - vinegar
1990 - chips
1991 paths:
1992 /a/fishy/on/a/dishy:
1993 get:
1994 parameters:
1995 - $ref: "#/components/parameters/fishy"
1996 operationId: locateFishy
1997 `
1998
1999 doc, _ := libopenapi.NewDocument([]byte(spec))
2000
2001 m, _ := doc.BuildV3Model()
2002
2003 v := NewParameterValidator(&m.Model)
2004
2005 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":false,\"chips\":999}", nil)
2006
2007 valid, errors := v.ValidateQueryParams(request)
2008 assert.True(t, valid)
2009
2010 assert.Len(t, errors, 0)
2011 }
2012
2013 func TestNewValidator_QueryParamValidTypeObjectPropType_RefViaContentWrappedInvalid(t *testing.T) {
2014 spec := `openapi: 3.1.0
2015 components:
2016 parameters:
2017 fishy:
2018 name: fishy
2019 in: query
2020 content:
2021 application/json:
2022 schema:
2023 type: object
2024 properties:
2025 vinegar:
2026 type: boolean
2027 chips:
2028 type: number
2029 required:
2030 - vinegar
2031 - chips
2032 paths:
2033 /a/fishy/on/a/dishy:
2034 get:
2035 parameters:
2036 - $ref: "#/components/parameters/fishy"
2037 operationId: locateFishy
2038 `
2039
2040 doc, _ := libopenapi.NewDocument([]byte(spec))
2041
2042 m, _ := doc.BuildV3Model()
2043
2044 v := NewParameterValidator(&m.Model)
2045
2046 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":false,\"chips\":\"I am invalid\"}", nil)
2047 valid, errors := v.ValidateQueryParams(request)
2048 assert.False(t, valid)
2049 assert.Len(t, errors, 1)
2050 assert.Equal(t, "got string, want number", errors[0].SchemaValidationErrors[0].Reason)
2051 }
2052
2053 func TestNewValidator_QueryParamValidTypeObjectPropType_JSONInvalid(t *testing.T) {
2054 spec := `openapi: 3.1.0
2055 components:
2056 parameters:
2057 fishy:
2058 name: fishy
2059 in: query
2060 content:
2061 application/json:
2062 schema:
2063 type: object
2064 properties:
2065 vinegar:
2066 type: boolean
2067 chips:
2068 type: number
2069 required:
2070 - vinegar
2071 - chips
2072 paths:
2073 /a/fishy/on/a/dishy:
2074 get:
2075 parameters:
2076 - $ref: "#/components/parameters/fishy"
2077 operationId: locateFishy
2078 `
2079
2080 doc, _ := libopenapi.NewDocument([]byte(spec))
2081
2082 m, _ := doc.BuildV3Model()
2083
2084 v := NewParameterValidator(&m.Model)
2085
2086 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy=I am not json", nil)
2087 valid, errors := v.ValidateQueryParams(request)
2088 assert.False(t, valid)
2089 assert.Len(t, errors, 1)
2090 assert.Equal(t, "Query parameter 'fishy' is not valid JSON", errors[0].Message)
2091 }
2092
2093 func TestNewValidator_QueryParamInvalidTypeObjectPropType_Ref(t *testing.T) {
2094 spec := `openapi: 3.1.0
2095 components:
2096 schema_validation:
2097 chippy:
2098 type: object
2099 properties:
2100 vinegar:
2101 type: boolean
2102 chips:
2103 type: number
2104 required:
2105 - vinegar
2106 - chips
2107 parameters:
2108 fishy:
2109 name: fishy
2110 in: query
2111 content:
2112 application/json:
2113 schema:
2114 $ref: "#/components/schema_validation/chippy"
2115 paths:
2116 /a/fishy/on/a/dishy:
2117 get:
2118 parameters:
2119 - $ref: "#/components/parameters/fishy"
2120 operationId: locateFishy`
2121
2122 doc, _ := libopenapi.NewDocument([]byte(spec))
2123
2124 m, _ := doc.BuildV3Model()
2125 v := NewParameterValidator(&m.Model)
2126
2127 request, _ := http.NewRequest(http.MethodGet, "https://things.com/a/fishy/on/a/dishy?fishy={\"vinegar\":1234,\"chips\":false}", nil)
2128 valid, errors := v.ValidateQueryParams(request)
2129 assert.False(t, valid)
2130 assert.Len(t, errors, 1)
2131 assert.Len(t, errors[0].SchemaValidationErrors, 2)
2132 }
2133
2134 func TestNewValidator_QueryParamValidateStyle_AllowReserved(t *testing.T) {
2135 spec := `openapi: 3.1.0
2136 paths:
2137 /a/fishy/on/a/dishy:
2138 get:
2139 parameters:
2140 - name: fishy
2141 in: query
2142 required: true
2143 explode: true
2144 schema:
2145 type: array
2146 items:
2147 type: string
2148 operationId: locateFishy`
2149
2150 doc, _ := libopenapi.NewDocument([]byte(spec))
2151
2152 m, _ := doc.BuildV3Model()
2153
2154 v := NewParameterValidator(&m.Model)
2155
2156 request, _ := http.NewRequest(http.MethodGet,
2157 "https://things.com/a/fishy/on/a/dishy?fishy=$$oh", nil)
2158
2159 valid, errors := v.ValidateQueryParams(request)
2160 assert.False(t, valid)
2161
2162 assert.Len(t, errors, 1)
2163 assert.Equal(t, "parameter values need to URL Encoded to ensure "+
2164 "reserved values are correctly encoded, for example: '%24%24oh'", errors[0].HowToFix)
2165 }
2166
2167 func TestNewValidator_QueryParamValidateStyle_ValidObjectArrayNoExplode(t *testing.T) {
2168 spec := `openapi: 3.1.0
2169 paths:
2170 /a/fishy/on/a/dishy:
2171 get:
2172 parameters:
2173 - name: fishy
2174 in: query
2175 required: true
2176 schema:
2177 type: array
2178 items:
2179 type: string
2180 operationId: locateFishy`
2181
2182 doc, _ := libopenapi.NewDocument([]byte(spec))
2183
2184 m, _ := doc.BuildV3Model()
2185
2186 v := NewParameterValidator(&m.Model)
2187
2188 request, _ := http.NewRequest(http.MethodGet,
2189 "https://things.com/a/fishy/on/a/dishy?fishy=cod,haddock,mackrel", nil)
2190
2191 valid, errors := v.ValidateQueryParams(request)
2192 assert.True(t, valid)
2193
2194 assert.Len(t, errors, 0)
2195 }
2196
2197 func TestNewValidator_QueryParamValidateStyle_InValidObjectArrayNoExplode(t *testing.T) {
2198 spec := `openapi: 3.1.0
2199 paths:
2200 /a/fishy/on/a/dishy:
2201 get:
2202 parameters:
2203 - name: fishy
2204 in: query
2205 required: true
2206 explode: true
2207 allowReserved: true
2208 schema:
2209 type: array
2210 items:
2211 type: string
2212 operationId: locateFishy`
2213
2214 doc, _ := libopenapi.NewDocument([]byte(spec))
2215
2216 m, _ := doc.BuildV3Model()
2217
2218 v := NewParameterValidator(&m.Model)
2219
2220 request, _ := http.NewRequest(http.MethodGet,
2221 "https://things.com/a/fishy/on/a/dishy?fishy=cod,haddock,mackrel", nil)
2222
2223 valid, errors := v.ValidateQueryParams(request)
2224 assert.False(t, valid)
2225
2226 assert.Len(t, errors, 1)
2227 assert.Equal(t, "Query parameter 'fishy' is not exploded correctly", errors[0].Message)
2228 }
2229
2230 func TestNewValidator_QueryParamValidateStyle_SpaceDelimitedIncorrectlyExploded(t *testing.T) {
2231 spec := `openapi: 3.1.0
2232 paths:
2233 /a/fishy/on/a/dishy:
2234 get:
2235 parameters:
2236 - name: fishy
2237 in: query
2238 required: true
2239 style: spaceDelimited
2240 schema:
2241 type: array
2242 items:
2243 type: string
2244 operationId: locateFishy`
2245
2246 doc, _ := libopenapi.NewDocument([]byte(spec))
2247
2248 m, _ := doc.BuildV3Model()
2249
2250 v := NewParameterValidator(&m.Model)
2251
2252 request, _ := http.NewRequest(http.MethodGet,
2253 "https://things.com/a/fishy/on/a/dishy?fishy=cod&fishy=haddock&fishy=mackrel", nil)
2254
2255 valid, errors := v.ValidateQueryParams(request)
2256 assert.False(t, valid)
2257
2258 assert.Len(t, errors, 1)
2259 assert.Equal(t, "Query parameter 'fishy' delimited incorrectly", errors[0].Message)
2260 }
2261
2262 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedObjectValidExplode(t *testing.T) {
2263 spec := `openapi: 3.1.0
2264 paths:
2265 /a/fishy/on/a/dishy:
2266 get:
2267 parameters:
2268 - name: fishy
2269 in: query
2270 required: true
2271 style: pipeDelimited
2272 schema:
2273 type: array
2274 items:
2275 type: string
2276 operationId: locateFishy`
2277
2278 doc, _ := libopenapi.NewDocument([]byte(spec))
2279
2280 m, _ := doc.BuildV3Model()
2281
2282 v := NewParameterValidator(&m.Model)
2283
2284 request, _ := http.NewRequest(http.MethodGet,
2285 "https://things.com/a/fishy/on/a/dishy?fishy=cod|haddock|mackrel", nil)
2286
2287 valid, errors := v.ValidateQueryParams(request)
2288 assert.True(t, valid)
2289 assert.Len(t, errors, 0)
2290 }
2291
2292 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedObjectInvalidExplode(t *testing.T) {
2293 spec := `openapi: 3.1.0
2294 paths:
2295 /a/fishy/on/a/dishy:
2296 get:
2297 parameters:
2298 - name: fishy
2299 in: query
2300 required: true
2301 style: pipeDelimited
2302 schema:
2303 type: array
2304 items:
2305 type: string
2306 operationId: locateFishy`
2307
2308 doc, _ := libopenapi.NewDocument([]byte(spec))
2309
2310 m, _ := doc.BuildV3Model()
2311
2312 v := NewParameterValidator(&m.Model)
2313
2314 request, _ := http.NewRequest(http.MethodGet,
2315 "https://things.com/a/fishy/on/a/dishy?fishy=cod|haddock|mackrel&fishy=breaded|cooked|fried", nil)
2316
2317 valid, errors := v.ValidateQueryParams(request)
2318 assert.False(t, valid)
2319 assert.Len(t, errors, 1)
2320 }
2321
2322 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedObjectValid(t *testing.T) {
2323 spec := `
2324 openapi: 3.1.0
2325 paths:
2326 /a/fishy/on/a/dishy:
2327 get:
2328 parameters:
2329 - name: plate
2330 in: query
2331 required: true
2332 style: pipeDelimited
2333 schema:
2334 type: array
2335 items:
2336 type: string
2337 - name: fishy
2338 in: query
2339 required: true
2340 style: pipeDelimited
2341 schema:
2342 type: array
2343 items:
2344 type: string
2345 operationId: locateFishy`
2346
2347 doc, _ := libopenapi.NewDocument([]byte(spec))
2348
2349 m, _ := doc.BuildV3Model()
2350
2351 v := NewParameterValidator(&m.Model)
2352
2353 request, _ := http.NewRequest(http.MethodGet,
2354 "https://things.com/a/fishy/on/a/dishy?fishy=cod|haddock|mackrel&plate=flat|round", nil)
2355
2356 valid, errors := v.ValidateQueryParams(request)
2357 assert.True(t, valid)
2358
2359 assert.Len(t, errors, 0)
2360 }
2361
2362 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedObjectDecode(t *testing.T) {
2363 spec := `openapi: 3.1.0
2364 paths:
2365 /a/fishy/on/a/dishy:
2366 get:
2367 parameters:
2368 - name: fishy
2369 in: query
2370 required: true
2371 style: pipeDelimited
2372 schema:
2373 type: object
2374 properties:
2375 fish:
2376 type: string
2377 enum:
2378 - salmon
2379 - tuna
2380 - cod
2381 dish:
2382 type: string
2383 enum:
2384 - salad
2385 - soup
2386 - stew
2387 required:
2388 - fish
2389 - dish
2390 operationId: locateFishy`
2391
2392 doc, _ := libopenapi.NewDocument([]byte(spec))
2393
2394 m, _ := doc.BuildV3Model()
2395
2396 v := NewParameterValidator(&m.Model)
2397
2398 request, _ := http.NewRequest(http.MethodGet,
2399 "https://things.com/a/fishy/on/a/dishy?fishy=fish|salmon|dish|stew", nil)
2400
2401 valid, errors := v.ValidateQueryParams(request)
2402 assert.True(t, valid)
2403
2404 assert.Len(t, errors, 0)
2405 }
2406
2407 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedObjectDecodeInvalid(t *testing.T) {
2408 spec := `openapi: 3.1.0
2409 paths:
2410 /a/fishy/on/a/dishy:
2411 get:
2412 parameters:
2413 - name: fishy
2414 in: query
2415 required: true
2416 style: pipeDelimited
2417 schema:
2418 type: object
2419 properties:
2420 fish:
2421 type: string
2422 enum:
2423 - salmon
2424 - tuna
2425 - cod
2426 dish:
2427 type: string
2428 enum:
2429 - salad
2430 - soup
2431 - stew
2432 required:
2433 - fish
2434 - dish
2435 operationId: locateFishy`
2436
2437 doc, _ := libopenapi.NewDocument([]byte(spec))
2438
2439 m, _ := doc.BuildV3Model()
2440
2441 v := NewParameterValidator(&m.Model)
2442
2443 request, _ := http.NewRequest(http.MethodGet,
2444 "https://things.com/a/fishy/on/a/dishy?fishy=fish|salmon|dish|cakes", nil)
2445
2446 valid, errors := v.ValidateQueryParams(request)
2447 assert.False(t, valid)
2448
2449 assert.Len(t, errors, 1)
2450 assert.Equal(t, "value must be one of 'salad', 'soup', 'stew'", errors[0].SchemaValidationErrors[0].Reason)
2451 }
2452
2453 func TestNewValidator_QueryParamValidateStyle_SpaceDelimitedObjectDecode(t *testing.T) {
2454 spec := `openapi: 3.1.0
2455 paths:
2456 /a/fishy/on/a/dishy:
2457 get:
2458 parameters:
2459 - name: fishy
2460 in: query
2461 required: true
2462 style: spaceDelimited
2463 schema:
2464 type: object
2465 properties:
2466 fish:
2467 type: string
2468 enum:
2469 - salmon
2470 - tuna
2471 - cod
2472 dish:
2473 type: string
2474 enum:
2475 - salad
2476 - soup
2477 - stew
2478 required:
2479 - fish
2480 - dish
2481 operationId: locateFishy`
2482
2483 doc, _ := libopenapi.NewDocument([]byte(spec))
2484
2485 m, _ := doc.BuildV3Model()
2486
2487 v := NewParameterValidator(&m.Model)
2488
2489 request, _ := http.NewRequest(http.MethodGet,
2490 "https://things.com/a/fishy/on/a/dishy?fishy=fish%20salmon%20dish%20stew", nil) // dumb, don't do this.
2491
2492 valid, errors := v.ValidateQueryParams(request)
2493 assert.True(t, valid)
2494
2495 assert.Len(t, errors, 0)
2496 }
2497
2498 func TestNewValidator_QueryParamValidateStyle_SpaceDelimitedObjectDecodeInvalid(t *testing.T) {
2499 spec := `openapi: 3.1.0
2500 paths:
2501 /a/fishy/on/a/dishy:
2502 get:
2503 parameters:
2504 - name: fishy
2505 in: query
2506 required: true
2507 style: spaceDelimited
2508 schema:
2509 type: object
2510 properties:
2511 fish:
2512 type: string
2513 enum:
2514 - salmon
2515 - tuna
2516 - cod
2517 dish:
2518 type: string
2519 enum:
2520 - salad
2521 - soup
2522 - stew
2523 required:
2524 - fish
2525 - dish
2526 operationId: locateFishy`
2527
2528 doc, _ := libopenapi.NewDocument([]byte(spec))
2529
2530 m, _ := doc.BuildV3Model()
2531
2532 v := NewParameterValidator(&m.Model)
2533
2534 request, _ := http.NewRequest(http.MethodGet,
2535 "https://things.com/a/fishy/on/a/dishy?fishy=fish%20salmon%20dish%20coffee", nil) // dumb, don't do this.
2536 valid, errors := v.ValidateQueryParams(request)
2537 assert.False(t, valid)
2538 assert.Len(t, errors, 1)
2539 assert.Equal(t, "value must be one of 'salad', 'soup', 'stew'", errors[0].SchemaValidationErrors[0].Reason)
2540 }
2541
2542 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedObjectInvalidMultiple(t *testing.T) {
2543 spec := `
2544 openapi: 3.1.0
2545 paths:
2546 /a/fishy/on/a/dishy:
2547 get:
2548 parameters:
2549 - name: plate
2550 in: query
2551 required: true
2552 style: pipeDelimited
2553 schema:
2554 type: array
2555 items:
2556 type: string
2557 - name: fishy
2558 in: query
2559 required: true
2560 style: pipeDelimited
2561 schema:
2562 type: array
2563 items:
2564 type: string
2565 operationId: locateFishy`
2566
2567 doc, _ := libopenapi.NewDocument([]byte(spec))
2568
2569 m, _ := doc.BuildV3Model()
2570
2571 v := NewParameterValidator(&m.Model)
2572
2573 request, _ := http.NewRequest(http.MethodGet,
2574 "https://things.com/a/fishy/on/a/dishy?fishy=cod|haddock|mackrel&plate=flat,round", nil)
2575 valid, errors := v.ValidateQueryParams(request)
2576 assert.True(t, valid)
2577 assert.Len(t, errors, 0)
2578 }
2579
2580 func TestNewValidator_QueryParamValidateStyle_DeepObjectMultiValuesNoSchema(t *testing.T) {
2581 spec := `---
2582 openapi: 3.1.0
2583 paths:
2584 /a/fishy/on/a/dishy:
2585 get:
2586 parameters:
2587 - name: fishy
2588 in: query
2589 required: true
2590 style: deepObject
2591 schema:
2592 type: object
2593 operationId: locateFishy`
2594
2595 doc, _ := libopenapi.NewDocument([]byte(spec))
2596
2597 m, _ := doc.BuildV3Model()
2598
2599 v := NewParameterValidator(&m.Model)
2600
2601 request, _ := http.NewRequest(http.MethodGet,
2602 "https://things.com/a/fishy/on/a/dishy?fishy[ocean]=atlantic&fishy[salt]=12", nil)
2603
2604 valid, errors := v.ValidateQueryParams(request)
2605 assert.True(t, valid)
2606
2607 assert.Len(t, errors, 0)
2608 }
2609
2610 func TestNewValidator_QueryParamValidateStyle_DeepObjectMultiValuesInvalid(t *testing.T) {
2611 spec := `---
2612 openapi: 3.1.0
2613 paths:
2614 /a/fishy/on/a/dishy:
2615 get:
2616 parameters:
2617 - name: fishy
2618 in: query
2619 required: true
2620 style: deepObject
2621 schema:
2622 type: object
2623 operationId: locateFishy`
2624
2625 doc, _ := libopenapi.NewDocument([]byte(spec))
2626
2627 m, _ := doc.BuildV3Model()
2628
2629 v := NewParameterValidator(&m.Model)
2630
2631 request, _ := http.NewRequest(http.MethodGet,
2632 "https://things.com/a/fishy/on/a/dishy?fishy=atlantic&fishy=12", nil)
2633
2634 valid, errors := v.ValidateQueryParams(request)
2635 assert.False(t, valid)
2636
2637 assert.Len(t, errors, 2)
2638 assert.Equal(t, "The query parameter 'fishy' has the 'deepObject' style defined, "+
2639 "There are multiple values (2) supplied, instead of a single value", errors[0].Reason)
2640 }
2641
2642 func TestNewValidator_QueryParamValidateStyle_FormEncoding(t *testing.T) {
2643 spec := `openapi: 3.1.0
2644 paths:
2645 /a/fishy/on/a/dishy:
2646 get:
2647 parameters:
2648 - name: fishy
2649 in: query
2650 required: true
2651 schema:
2652 type: object
2653 properties:
2654 ocean:
2655 type: string
2656 fins:
2657 type: number
2658 required: [ocean, fins]
2659 - name: dishy
2660 in: query
2661 required: [hot, salty]
2662 schema:
2663 type: object
2664 properties:
2665 hot:
2666 type: boolean
2667 salty:
2668 type: boolean
2669 operationId: locateFishy`
2670
2671 doc, _ := libopenapi.NewDocument([]byte(spec))
2672
2673 m, _ := doc.BuildV3Model()
2674
2675 v := NewParameterValidator(&m.Model)
2676
2677 request, _ := http.NewRequest(http.MethodGet,
2678 "https://things.com/a/fishy/on/a/dishy?fishy=ocean,atlantic,fins,4&dishy=hot,true,salty,true", nil)
2679
2680 valid, errors := v.ValidateQueryParams(request)
2681 assert.True(t, valid)
2682
2683 assert.Len(t, errors, 0)
2684 }
2685
2686 func TestNewValidator_QueryParamValidateStyle_FormEncodingInvalid(t *testing.T) {
2687 spec := `openapi: 3.1.0
2688 paths:
2689 /a/fishy/on/a/dishy:
2690 get:
2691 parameters:
2692 - name: fishy
2693 in: query
2694 required: true
2695 schema:
2696 type: object
2697 properties:
2698 ocean:
2699 type: string
2700 fins:
2701 type: number
2702 required: [ocean, fins]
2703 - name: dishy
2704 in: query
2705 schema:
2706 required: [hot, salty]
2707 type: object
2708 properties:
2709 hot:
2710 type: boolean
2711 salty:
2712 type: boolean
2713 operationId: locateFishy`
2714
2715 doc, _ := libopenapi.NewDocument([]byte(spec))
2716
2717 m, _ := doc.BuildV3Model()
2718
2719 v := NewParameterValidator(&m.Model)
2720
2721 request, _ := http.NewRequest(http.MethodGet,
2722 "https://things.com/a/fishy/on/a/dishy?fishy=ocean,atlantic,fins,4&dishy=hot,no,salty,why", nil)
2723
2724 valid, errors := v.ValidateQueryParams(request)
2725 assert.False(t, valid)
2726
2727 assert.Len(t, errors, 1)
2728 assert.Equal(t, "got string, want boolean", errors[0].SchemaValidationErrors[0].Reason)
2729 assert.Equal(t, "got string, want boolean", errors[0].SchemaValidationErrors[1].Reason)
2730 }
2731
2732 func TestNewValidator_QueryParamValidateStyle_FormEncodingArray(t *testing.T) {
2733 spec := `openapi: 3.1.0
2734 paths:
2735 /a/fishy/on/a/dishy:
2736 get:
2737 parameters:
2738 - name: fishy
2739 in: query
2740 required: true
2741 schema:
2742 type: array
2743 items:
2744 type: number
2745 - name: dishy
2746 in: query
2747 required: true
2748 schema:
2749 type: array
2750 items:
2751 type: string
2752 operationId: locateFishy
2753 `
2754
2755 doc, _ := libopenapi.NewDocument([]byte(spec))
2756
2757 m, _ := doc.BuildV3Model()
2758
2759 v := NewParameterValidator(&m.Model)
2760
2761 request, _ := http.NewRequest(http.MethodGet,
2762 "https://things.com/a/fishy/on/a/dishy?fishy=1,2,3&dishy=a,little,plate", nil)
2763
2764 valid, errors := v.ValidateQueryParams(request)
2765 assert.True(t, valid)
2766
2767 assert.Len(t, errors, 0)
2768 }
2769
2770 func TestNewValidator_QueryParamValidateStyle_FormEncodingArrayExplode(t *testing.T) {
2771 spec := `openapi: 3.1.0
2772 paths:
2773 /a/fishy/on/a/dishy:
2774 get:
2775 parameters:
2776 - name: fishy
2777 in: query
2778 explode: true
2779 required: true
2780 schema:
2781 type: array
2782 items:
2783 type: number
2784 - name: dishy
2785 in: query
2786 explode: true
2787 required: true
2788 schema:
2789 type: array
2790 items:
2791 type: string
2792 operationId: locateFishy
2793 `
2794
2795 doc, _ := libopenapi.NewDocument([]byte(spec))
2796
2797 m, _ := doc.BuildV3Model()
2798
2799 v := NewParameterValidator(&m.Model)
2800
2801 request, _ := http.NewRequest(http.MethodGet,
2802 "https://things.com/a/fishy/on/a/dishy?fishy=1&fishy=2&fishy=3&dishy=a&dishy=little&dishy=dish", nil)
2803
2804 valid, errors := v.ValidateQueryParams(request)
2805 assert.True(t, valid)
2806
2807 assert.Len(t, errors, 0)
2808 }
2809
2810 func TestNewValidator_QueryParamValidateStyle_FormEncodingArrayExplodeInvalid(t *testing.T) {
2811 spec := `openapi: 3.1.0
2812 paths:
2813 /a/fishy/on/a/dishy:
2814 get:
2815 parameters:
2816 - name: fishy
2817 in: query
2818 explode: true
2819 required: true
2820 schema:
2821 type: array
2822 items:
2823 type: number
2824 - name: dishy
2825 in: query
2826 explode: true
2827 required: true
2828 schema:
2829 type: array
2830 items:
2831 type: string
2832 operationId: locateFishy
2833 `
2834
2835 doc, _ := libopenapi.NewDocument([]byte(spec))
2836
2837 m, _ := doc.BuildV3Model()
2838
2839 v := NewParameterValidator(&m.Model)
2840
2841 request, _ := http.NewRequest(http.MethodGet,
2842 "https://things.com/a/fishy/on/a/dishy?fishy=1,2,3&dishy=little,dishy", nil)
2843 valid, errors := v.ValidateQueryParams(request)
2844 assert.False(t, valid)
2845 assert.Len(t, errors, 4)
2846 assert.Equal(t, "The query parameter 'fishy' has a default or 'form' encoding defined, however the "+
2847 "value '1,2,3' is encoded as an object or an array using commas. "+
2848 "The contract defines the explode value to set to 'true'", errors[0].Reason)
2849 }
2850
2851 func TestNewValidator_QueryParamValidateStyle_PipeDelimitedValid(t *testing.T) {
2852 spec := `openapi: 3.1.0
2853 paths:
2854 /a/fishy/on/a/dishy:
2855 get:
2856 parameters:
2857 - name: fishy
2858 in: query
2859 required: true
2860 style: pipeDelimited
2861 schema:
2862 type: array
2863 items:
2864 type: number
2865 - name: dishy
2866 in: query
2867 style: pipeDelimited
2868 required: true
2869 schema:
2870 type: array
2871 items:
2872 type: string
2873 operationId: locateFishy
2874 `
2875
2876 doc, _ := libopenapi.NewDocument([]byte(spec))
2877
2878 m, _ := doc.BuildV3Model()
2879
2880 v := NewParameterValidator(&m.Model)
2881
2882 request, _ := http.NewRequest(http.MethodGet,
2883 "https://things.com/a/fishy/on/a/dishy?fishy=1|2|3&dishy=little|dishy", nil)
2884 valid, errors := v.ValidateQueryParams(request)
2885 assert.True(t, valid)
2886 assert.Len(t, errors, 0)
2887 }
2888
2889 func TestNewValidator_QueryParamValidateStyle_SpaceDelimitedValid(t *testing.T) {
2890 spec := `openapi: 3.1.0
2891 paths:
2892 /a/fishy/on/a/dishy:
2893 get:
2894 parameters:
2895 - name: fishy
2896 in: query
2897 required: true
2898 style: spaceDelimited
2899 schema:
2900 type: array
2901 items:
2902 type: number
2903 - name: dishy
2904 in: query
2905 style: spaceDelimited
2906 required: true
2907 schema:
2908 type: array
2909 items:
2910 type: string
2911 operationId: locateFishy
2912 `
2913
2914 doc, _ := libopenapi.NewDocument([]byte(spec))
2915
2916 m, _ := doc.BuildV3Model()
2917
2918 v := NewParameterValidator(&m.Model)
2919
2920 request, _ := http.NewRequest(http.MethodGet,
2921 "https://things.com/a/fishy/on/a/dishy?fishy=1%202%203&dishy=little%20dishy", nil)
2922 valid, errors := v.ValidateQueryParams(request)
2923 assert.True(t, valid)
2924 assert.Len(t, errors, 0)
2925 }
2926
2927 func TestNewValidator_QueryParamValidateStyle_SpaceDelimitedInvalidSchema(t *testing.T) {
2928 spec := `openapi: 3.1.0
2929 paths:
2930 /a/fishy/on/a/dishy:
2931 get:
2932 parameters:
2933 - name: fishy
2934 in: query
2935 required: true
2936 style: spaceDelimited
2937 schema:
2938 type: array
2939 items:
2940 type: number
2941 - name: dishy
2942 in: query
2943 style: spaceDelimited
2944 required: true
2945 schema:
2946 type: array
2947 items:
2948 type: string
2949 operationId: locateFishy
2950 `
2951
2952 doc, _ := libopenapi.NewDocument([]byte(spec))
2953
2954 m, _ := doc.BuildV3Model()
2955
2956 v := NewParameterValidator(&m.Model)
2957
2958 request, _ := http.NewRequest(http.MethodGet,
2959 "https://things.com/a/fishy/on/a/dishy?fishy=1|%202%203&dishy=little%20dishy", nil)
2960 valid, errors := v.ValidateQueryParams(request)
2961 assert.False(t, valid)
2962 assert.Len(t, errors, 1)
2963 assert.Equal(t, "Convert the value '1|' into a number", errors[0].HowToFix)
2964 }
2965
2966 func TestNewValidator_QueryParamValidateStyle_DeepObjectMultiValuesFailedSchema(t *testing.T) {
2967 spec := `openapi: 3.1.0
2968 paths:
2969 /a/fishy/on/a/dishy:
2970 get:
2971 parameters:
2972 - name: fishy
2973 in: query
2974 required: true
2975 style: deepObject
2976 schema:
2977 type: object
2978 properties:
2979 ocean:
2980 type: string
2981 salt:
2982 type: boolean
2983 required: [ocean, salt]
2984 operationId: locateFishy`
2985
2986 doc, _ := libopenapi.NewDocument([]byte(spec))
2987
2988 m, _ := doc.BuildV3Model()
2989
2990 v := NewParameterValidator(&m.Model)
2991
2992 request, _ := http.NewRequest(http.MethodGet,
2993 "https://things.com/a/fishy/on/a/dishy?fishy[ocean]=atlantic&fishy[salt]=12", nil)
2994
2995 valid, errors := v.ValidateQueryParams(request)
2996 assert.False(t, valid)
2997
2998 assert.Len(t, errors, 1)
2999 assert.Equal(t, "got number, want boolean", errors[0].SchemaValidationErrors[0].Reason)
3000 }
3001
3002 func TestNewValidator_QueryParamSetPath(t *testing.T) {
3003 spec := `openapi: 3.1.0
3004 paths:
3005 /a/fishy/on/a/dishy:
3006 get:
3007 parameters:
3008 - name: fishy
3009 in: query
3010 required: true
3011 style: deepObject
3012 schema:
3013 type: object
3014 properties:
3015 ocean:
3016 type: string
3017 salt:
3018 type: boolean
3019 required: [ocean, salt]
3020 operationId: locateFishy`
3021
3022 doc, _ := libopenapi.NewDocument([]byte(spec))
3023
3024 m, _ := doc.BuildV3Model()
3025
3026 v := NewParameterValidator(&m.Model)
3027
3028 request, _ := http.NewRequest(http.MethodGet,
3029 "https://things.com/a/fishy/on/a/dishy?fishy[ocean]=atlantic&fishy[salt]=12", nil)
3030
3031 // preset the path
3032 path, _, pv := paths.FindPath(request, &m.Model, &config.ValidationOptions{RegexCache: &sync.Map{}})
3033
3034 valid, errors := v.ValidateQueryParamsWithPathItem(request, path, pv)
3035 assert.False(t, valid)
3036
3037 assert.Len(t, errors, 1)
3038 assert.Equal(t, "got number, want boolean", errors[0].SchemaValidationErrors[0].Reason)
3039 }
3040
3041 func TestNewValidator_QueryParamSetPath_notfound(t *testing.T) {
3042 spec := `openapi: 3.1.0
3043 paths:
3044 /a/fishy/on/a/dishy:
3045 get:
3046 parameters:
3047 - name: fishy
3048 in: query
3049 required: true
3050 style: deepObject
3051 schema:
3052 type: object
3053 properties:
3054 ocean:
3055 type: string
3056 salt:
3057 type: boolean
3058 required: [ocean, salt]
3059 operationId: locateFishy`
3060
3061 doc, _ := libopenapi.NewDocument([]byte(spec))
3062
3063 m, _ := doc.BuildV3Model()
3064
3065 v := NewParameterValidator(&m.Model)
3066
3067 request, _ := http.NewRequest(http.MethodGet,
3068 "https://things.com/a/beef/on/a/dishy?fishy[ocean]=atlantic&fishy[salt]=12", nil)
3069
3070 // preset the path
3071 path, _, pv := paths.FindPath(request, &m.Model, nil)
3072
3073 valid, errors := v.ValidateQueryParamsWithPathItem(request, path, pv)
3074 assert.False(t, valid)
3075
3076 assert.Len(t, errors, 1)
3077 assert.Equal(t, "GET Path '/a/beef/on/a/dishy' not found", errors[0].Message)
3078 }
3079
3080 func TestNewValidator_QueryParamValidateStyle_DeepObjectMultiValuesFailedMultipleSchemas(t *testing.T) {
3081 spec := `---
3082 openapi: 3.1.0
3083 paths:
3084 /a/fishy/on/a/dishy:
3085 get:
3086 parameters:
3087 - name: fishy
3088 in: query
3089 required: true
3090 style: deepObject
3091 schema:
3092 type: object
3093 properties:
3094 ocean:
3095 type: string
3096 salt:
3097 type: boolean
3098 required:
3099 - ocean
3100 - salt
3101 - name: dishy
3102 in: query
3103 required: true
3104 style: deepObject
3105 schema:
3106 type: object
3107 properties:
3108 size:
3109 type: string
3110 numCracks:
3111 type: number
3112 required:
3113 - size
3114 - numCracks
3115 - name: cake
3116 in: query
3117 required: true
3118 style: deepObject
3119 schema:
3120 type: object
3121 properties:
3122 message:
3123 type: string
3124 numCandles:
3125 type: number
3126 required:
3127 - message
3128 - numCandles
3129 operationId: locateFishy`
3130
3131 doc, _ := libopenapi.NewDocument([]byte(spec))
3132
3133 m, _ := doc.BuildV3Model()
3134
3135 v := NewParameterValidator(&m.Model)
3136
3137 request, _ := http.NewRequest(http.MethodGet,
3138 "https://things.com/a/fishy/on/a/dishy?fishy[ocean]=atlantic&fishy[salt]=12"+
3139 "&dishy[size]=big&dishy[numCracks]=false"+
3140 "&cake[message]=happy%20birthday&cake[numCandles]=false", nil)
3141
3142 valid, errors := v.ValidateQueryParams(request)
3143 assert.False(t, valid)
3144
3145 assert.Len(t, errors, 3)
3146 assert.Equal(t, "got number, want boolean", errors[0].SchemaValidationErrors[0].Reason)
3147 assert.Equal(t, "got boolean, want number", errors[1].SchemaValidationErrors[0].Reason)
3148 assert.Equal(t, "got boolean, want number", errors[2].SchemaValidationErrors[0].Reason)
3149 }
3150
3151 func TestNewValidator_ValidateEncodedObjectIsCorrect(t *testing.T) {
3152 spec := `openapi: 3.1.0
3153 paths:
3154 /a/fishy/on/a/dishy:
3155 get:
3156 parameters:
3157 - name: fishy
3158 in: query
3159 required: true
3160 style: deepObject
3161 schema:
3162 type: object
3163 properties:
3164 ocean:
3165 type: string
3166 salt:
3167 type: boolean
3168 required: [ocean, salt]
3169 operationId: locateFishy`
3170
3171 doc, _ := libopenapi.NewDocument([]byte(spec))
3172 m, _ := doc.BuildV3Model()
3173
3174 sch := m.Model.Paths.PathItems.GetOrZero("/a/fishy/on/a/dishy").Get.Parameters[0].Schema
3175
3176 s := sch.Schema()
3177
3178 // this is not compatible.
3179 rawObject := map[int]int{
3180 1: 2,
3181 }
3182
3183 errs := ValidateParameterSchema(s, rawObject, "cake", "burger", "lemons",
3184 "pizza", "rice", "herbs", nil)
3185
3186 assert.Len(t, errs, 1)
3187 assert.Equal(t, "lemons 'pizza' is defined as an object, "+
3188 "however it failed to be decoded as an object", errs[0].Reason)
3189 }
3190
3191 // https://github.com/pb33f/wiretap/issues/82
3192 func TestNewValidator_QueryParamValidateStyle_DeepObjectAdditionalPropertiesArray(t *testing.T) {
3193 spec := `openapi: 3.1.0
3194 paths:
3195 /anything/queryParams/deepObject/map:
3196 get:
3197 operationId: deepObjectQueryParamsMap
3198 parameters:
3199 - name: mapArrParam
3200 in: query
3201 style: deepObject
3202 schema:
3203 type: object
3204 additionalProperties:
3205 type: array
3206 items:
3207 type: string
3208 example: { "test": ["test", "test2"], "test2": ["test3", "test4"] }
3209 responses:
3210 "200":
3211 description: OK`
3212
3213 doc, _ := libopenapi.NewDocument([]byte(spec))
3214
3215 m, _ := doc.BuildV3Model()
3216
3217 v := NewParameterValidator(&m.Model)
3218
3219 request, _ := http.NewRequest(http.MethodGet,
3220 "https://things.com/anything/queryParams/deepObject/map?mapArrParam[test2]=test3&mapArrParam[test2]=test4&mapArrParam[test]=test&mapArrParam[test]=test2", nil)
3221
3222 valid, errors := v.ValidateQueryParams(request)
3223 assert.True(t, valid)
3224
3225 assert.Len(t, errors, 0)
3226 }
3227
3228 // https://github.com/pb33f/wiretap/issues/82
3229 func TestNewValidator_QueryParamValidateStyle_DeepObjectAdditionalPropertiesArrayTop(t *testing.T) {
3230 spec := `openapi: 3.1.0
3231 paths:
3232 /anything/queryParams/deepObject/map:
3233 get:
3234 operationId: deepObjectQueryParamsMap
3235 parameters:
3236 - name: mapArrParam
3237 in: query
3238 style: deepObject
3239 schema:
3240 type: array
3241 items:
3242 type: string
3243 example: { "test": ["test", "test2"], "test2": ["test3", "test4"] }
3244 responses:
3245 "200":
3246 description: OK`
3247
3248 doc, _ := libopenapi.NewDocument([]byte(spec))
3249
3250 m, _ := doc.BuildV3Model()
3251
3252 v := NewParameterValidator(&m.Model)
3253
3254 request, _ := http.NewRequest(http.MethodGet,
3255 "https://things.com/anything/queryParams/deepObject/map?mapArrParam[test2]=test3&mapArrParam[test2]=test4&mapArrParam[test]=test&mapArrParam[test]=test2", nil)
3256
3257 valid, errors := v.ValidateQueryParams(request)
3258 assert.True(t, valid)
3259
3260 assert.Len(t, errors, 0)
3261 }
3262
3263 // https://github.com/pb33f/wiretap/issues/82
3264 func TestNewValidator_QueryParamValidateStyle_DeepObjectAdditionalPropertiesArrayNumericStrings(t *testing.T) {
3265 spec := `openapi: 3.1.0
3266 paths:
3267 /anything/queryParams/deepObject/map:
3268 get:
3269 operationId: deepObjectQueryParamsMap
3270 parameters:
3271 - name: mapArrParam
3272 in: query
3273 style: deepObject
3274 schema:
3275 type: object
3276 additionalProperties:
3277 type: array
3278 items:
3279 type: string
3280 example: { "test": ["test", "test2"], "test2": ["test3", "test4"] }
3281 responses:
3282 "200":
3283 description: OK`
3284
3285 doc, _ := libopenapi.NewDocument([]byte(spec))
3286
3287 m, _ := doc.BuildV3Model()
3288
3289 v := NewParameterValidator(&m.Model)
3290
3291 request, _ := http.NewRequest(http.MethodGet,
3292 "https://things.com/anything/queryParams/deepObject/map?mapArrParam[test2]=23&mapArrParam[test2]=test4&mapArrParam[test]=test&mapArrParam[test]=test2", nil)
3293
3294 valid, errors := v.ValidateQueryParams(request)
3295 assert.True(t, valid)
3296 assert.Len(t, errors, 0)
3297 }
3298
3299 // https://github.com/pb33f/wiretap/issues/83
3300 func TestNewValidator_QueryParamValidateStyle_BadSchemaDeepObject(t *testing.T) {
3301 spec := `openapi: 3.1.0
3302 info:
3303 title: Test
3304 version: 0.1.0
3305 security:
3306 - apiKeyAuth: []
3307 paths:
3308 /anything/queryParams/deepObject/obj:
3309 get:
3310 operationId: deepObjectQueryParamsObject
3311 parameters:
3312 - name: objParam
3313 in: query
3314 style: deepObject
3315 schema:
3316 $ref: "components.yaml#/components/schemas/simpleObject"
3317 required: true
3318 responses:
3319 "200":
3320 description: OK
3321 components:
3322 securitySchemes:
3323 apiKeyAuth:
3324 type: apiKey
3325 in: header
3326 name: Authorization
3327 description: Authenticate using an API Key generated via our platform.`
3328
3329 doc, _ := libopenapi.NewDocument([]byte(spec))
3330
3331 m, err := doc.BuildV3Model()
3332 assert.Error(t, err) // path build will fail because of missing schema.
3333
3334 v := NewParameterValidator(&m.Model)
3335
3336 request, _ := http.NewRequest(http.MethodGet,
3337 "http://localhost:9090/anything/queryParams/deepObject/obj?objParam=blahdedahdedah", nil)
3338
3339 valid, errors := v.ValidateQueryParams(request)
3340 assert.True(t, valid)
3341 assert.Len(t, errors, 0)
3342 }
3343
3344 // https://github.com/pb33f/wiretap/issues/83
3345 func TestNewValidator_QueryParamValidateStyle_BadSchemaDeepObject_Inline(t *testing.T) {
3346 spec := `openapi: 3.1.0
3347 info:
3348 title: Test
3349 version: 0.1.0
3350 security:
3351 - apiKeyAuth: []
3352 paths:
3353 /anything/queryParams/deepObject/obj:
3354 get:
3355 operationId: deepObjectQueryParamsObject
3356 parameters:
3357 - name: objParam
3358 in: query
3359 style: deepObject
3360 schema:
3361 type: object
3362 properties:
3363 cake:
3364 type: string
3365 required: true
3366 responses:
3367 "200":
3368 description: OK
3369 components:
3370 securitySchemes:
3371 apiKeyAuth:
3372 type: apiKey
3373 in: header
3374 name: Authorization
3375 description: Authenticate using an API Key generated via our platform.`
3376
3377 doc, _ := libopenapi.NewDocument([]byte(spec))
3378
3379 m, err := doc.BuildV3Model()
3380 assert.NoError(t, err)
3381
3382 v := NewParameterValidator(&m.Model)
3383
3384 request, _ := http.NewRequest(http.MethodGet,
3385 "http://localhost:9090/anything/queryParams/deepObject/obj?objParam=blahdedahdedah", nil)
3386
3387 valid, errors := v.ValidateQueryParams(request)
3388 assert.False(t, valid)
3389 assert.Len(t, errors, 1)
3390 assert.Equal(t, "The query parameter 'objParam' is defined as an object,"+
3391 " however it failed to pass a schema validation", errors[0].Reason)
3392 }
3393
3394 // https://github.com/pb33f/libopenapi-validator/issues/83
3395 func TestNewValidator_QueryParamValidateStyle_NestedDeepObject(t *testing.T) {
3396 spec := `openapi: 3.1.0
3397 info:
3398 title: Test
3399 version: 1.0.0
3400 paths:
3401 /path:
3402 get:
3403 parameters:
3404 - name: obj
3405 in: query
3406 style: deepObject
3407 schema:
3408 type: object
3409 properties:
3410 root:
3411 type: string
3412 nested:
3413 type: object
3414 properties:
3415 child:
3416 type: string
3417 required: true
3418 responses:
3419 "200":
3420 description: OK`
3421
3422 doc, _ := libopenapi.NewDocument([]byte(spec))
3423 m, err := doc.BuildV3Model()
3424 require.NoError(t, err)
3425
3426 v := NewParameterValidator(&m.Model)
3427 request, _ := http.NewRequest(http.MethodGet,
3428 "https://things.com/path?obj[root]=test1&obj[nested][child]=test2", nil)
3429
3430 valid, errors := v.ValidateQueryParams(request)
3431 assert.True(t, valid)
3432 assert.Len(t, errors, 0)
3433 }
3434
3435 func TestNewValidator_QueryParamValidateStyle_NestedDeepObjectInvalidLeafType(t *testing.T) {
3436 spec := `openapi: 3.1.0
3437 info:
3438 title: Test
3439 version: 1.0.0
3440 paths:
3441 /path:
3442 get:
3443 parameters:
3444 - name: obj
3445 in: query
3446 style: deepObject
3447 schema:
3448 type: object
3449 properties:
3450 nested:
3451 type: object
3452 properties:
3453 child:
3454 type: boolean
3455 required: true
3456 responses:
3457 "200":
3458 description: OK`
3459
3460 doc, _ := libopenapi.NewDocument([]byte(spec))
3461 m, err := doc.BuildV3Model()
3462 require.NoError(t, err)
3463
3464 v := NewParameterValidator(&m.Model)
3465 request, _ := http.NewRequest(http.MethodGet,
3466 "https://things.com/path?obj[nested][child]=not-a-bool", nil)
3467
3468 valid, errors := v.ValidateQueryParams(request)
3469 assert.False(t, valid)
3470 require.Len(t, errors, 1)
3471 require.NotEmpty(t, errors[0].SchemaValidationErrors)
3472 assert.Equal(t, "got string, want boolean", errors[0].SchemaValidationErrors[0].Reason)
3473 assert.Equal(t, "$.nested.child", errors[0].SchemaValidationErrors[0].FieldPath)
3474 }
3475
3476 func TestNewValidator_QueryParamValidateStyle_NestedDeepObjectRequiredLeaf(t *testing.T) {
3477 spec := `openapi: 3.1.0
3478 info:
3479 title: Test
3480 version: 1.0.0
3481 paths:
3482 /path:
3483 get:
3484 parameters:
3485 - name: obj
3486 in: query
3487 style: deepObject
3488 schema:
3489 type: object
3490 required: [nested]
3491 properties:
3492 nested:
3493 type: object
3494 required: [child]
3495 properties:
3496 child:
3497 type: string
3498 other:
3499 type: string
3500 required: true
3501 responses:
3502 "200":
3503 description: OK`
3504
3505 doc, _ := libopenapi.NewDocument([]byte(spec))
3506 m, err := doc.BuildV3Model()
3507 require.NoError(t, err)
3508
3509 v := NewParameterValidator(&m.Model)
3510 request, _ := http.NewRequest(http.MethodGet,
3511 "https://things.com/path?obj[nested][other]=value", nil)
3512
3513 valid, errors := v.ValidateQueryParams(request)
3514 assert.False(t, valid)
3515 require.Len(t, errors, 1)
3516 require.NotEmpty(t, errors[0].SchemaValidationErrors)
3517 assert.Contains(t, errors[0].SchemaValidationErrors[0].Reason, "missing")
3518 }
3519
3520 func TestNewValidator_QueryParamValidateStyle_NestedDeepObjectArray(t *testing.T) {
3521 spec := `openapi: 3.1.0
3522 info:
3523 title: Test
3524 version: 1.0.0
3525 paths:
3526 /path:
3527 get:
3528 parameters:
3529 - name: obj
3530 in: query
3531 style: deepObject
3532 schema:
3533 type: object
3534 properties:
3535 nested:
3536 type: object
3537 properties:
3538 tags:
3539 type: array
3540 items:
3541 type: string
3542 required: true
3543 responses:
3544 "200":
3545 description: OK`
3546
3547 doc, _ := libopenapi.NewDocument([]byte(spec))
3548 m, err := doc.BuildV3Model()
3549 require.NoError(t, err)
3550
3551 v := NewParameterValidator(&m.Model)
3552 request, _ := http.NewRequest(http.MethodGet,
3553 "https://things.com/path?obj[nested][tags]=123&obj[nested][tags]=456", nil)
3554
3555 valid, errors := v.ValidateQueryParams(request)
3556 assert.True(t, valid)
3557 assert.Len(t, errors, 0)
3558 }
3559
3560 func TestNewValidator_QueryParamValidateStyle_NestedDeepObjectAdditionalPropertiesArray(t *testing.T) {
3561 spec := `openapi: 3.1.0
3562 info:
3563 title: Test
3564 version: 1.0.0
3565 paths:
3566 /path:
3567 get:
3568 parameters:
3569 - name: obj
3570 in: query
3571 style: deepObject
3572 schema:
3573 type: object
3574 properties:
3575 filters:
3576 type: object
3577 additionalProperties:
3578 type: array
3579 items:
3580 type: string
3581 required: true
3582 responses:
3583 "200":
3584 description: OK`
3585
3586 doc, _ := libopenapi.NewDocument([]byte(spec))
3587 m, err := doc.BuildV3Model()
3588 require.NoError(t, err)
3589
3590 v := NewParameterValidator(&m.Model)
3591 request, _ := http.NewRequest(http.MethodGet,
3592 "https://things.com/path?obj[filters][tag]=123&obj[filters][tag]=456", nil)
3593
3594 valid, errors := v.ValidateQueryParams(request)
3595 assert.True(t, valid)
3596 assert.Len(t, errors, 0)
3597 }
3598
3599 func TestNewValidator_QueryParamValidateStyle_NestedDeepObjectPathConflict(t *testing.T) {
3600 spec := `openapi: 3.1.0
3601 info:
3602 title: Test
3603 version: 1.0.0
3604 paths:
3605 /path:
3606 get:
3607 parameters:
3608 - name: obj
3609 in: query
3610 style: deepObject
3611 schema:
3612 type: object
3613 properties:
3614 nested:
3615 type: object
3616 properties:
3617 child:
3618 type: string
3619 required: true
3620 responses:
3621 "200":
3622 description: OK`
3623
3624 doc, _ := libopenapi.NewDocument([]byte(spec))
3625 m, err := doc.BuildV3Model()
3626 require.NoError(t, err)
3627
3628 v := NewParameterValidator(&m.Model)
3629 request, _ := http.NewRequest(http.MethodGet,
3630 "https://things.com/path?obj[nested]=bad&obj[nested][child]=ok", nil)
3631
3632 valid, validationErrors := v.ValidateQueryParams(request)
3633 assert.False(t, valid)
3634 require.NotEmpty(t, validationErrors)
3635 assert.Contains(t, validationErrors[0].Reason, "property path 'nested'")
3636 assert.Contains(t, validationErrors[0].Reason, "'nested.child'")
3637 }
3638
3639 func TestNewValidator_ValidateRawMap(t *testing.T) {
3640 spec := `openapi: 3.1.0
3641 paths:
3642 /a/fishy/on/a/dishy:
3643 get:
3644 parameters:
3645 - name: fishy
3646 in: query
3647 required: true
3648 style: deepObject
3649 schema:
3650 type: object
3651 properties:
3652 ocean:
3653 type: string
3654 salt:
3655 type: boolean
3656 required: [ocean, salt]
3657 operationId: locateFishy`
3658
3659 doc, _ := libopenapi.NewDocument([]byte(spec))
3660 m, _ := doc.BuildV3Model()
3661
3662 sch := m.Model.Paths.PathItems.GetOrZero("/a/fishy/on/a/dishy").Get.Parameters[0].Schema
3663
3664 s := sch.Schema()
3665
3666 // this is not compatible.
3667 rawObject := map[int]int{
3668 1: 2,
3669 }
3670
3671 errs := ValidateParameterSchema(s, rawObject, "cake", "burger", "lemons",
3672 "pizza", "rice", "herbs", nil)
3673
3674 assert.Len(t, errs, 1)
3675 assert.Equal(t, "lemons 'pizza' is defined as an object, "+
3676 "however it failed to be decoded as an object", errs[0].Reason)
3677 }
3678
3679 // https://github.com/pb33f/wiretap/issues/83
3680 func TestNewValidator_QueryParamValidateStyle_EmptyDeepObject(t *testing.T) {
3681 spec := `openapi: 3.1.0
3682 info:
3683 title: Test
3684 version: 0.1.0
3685 security:
3686 - apiKeyAuth: []
3687 paths:
3688 /anything/queryParams/deepObject/obj:
3689 get:
3690 operationId: deepObjectQueryParamsObject
3691 parameters:
3692 - name: objParam
3693 in: query
3694 style: deepObject
3695 schema:
3696 type: object
3697 properties:
3698 cake:
3699 type: string
3700 required: true
3701 responses:
3702 "200":
3703 description: OK
3704 components:
3705 securitySchemes:
3706 apiKeyAuth:
3707 type: apiKey
3708 in: header
3709 name: Authorization
3710 description: Authenticate using an API Key generated via our platform.`
3711
3712 doc, _ := libopenapi.NewDocument([]byte(spec))
3713
3714 m, err := doc.BuildV3Model()
3715 assert.NoError(t, err)
3716
3717 v := NewParameterValidator(&m.Model)
3718
3719 request, _ := http.NewRequest(http.MethodGet,
3720 "http://localhost:9090/anything/queryParams/deepObject/obj?objParam", nil)
3721
3722 valid, errors := v.ValidateQueryParams(request)
3723 assert.True(t, valid)
3724 assert.Len(t, errors, 0)
3725 }
3726
3727 // https://github.com/pb33f/libopenapi-validator/issues/140
3728 func TestNewValidator_CheckQueryParamsMaxItems(t *testing.T) {
3729 spec := `openapi: 3.1.0
3730 info:
3731 title: ID List API
3732 version: "1.0.0"
3733 paths:
3734 /items:
3735 get:
3736 parameters:
3737 - name: id
3738 in: query
3739 required: true
3740 style: form
3741 explode: false
3742 schema:
3743 type: array
3744 items:
3745 type: integer
3746 maxItems: 10
3747 responses:
3748 '200':
3749 description: OK
3750 '400':
3751 description: Invalid input`
3752
3753 doc, _ := libopenapi.NewDocument([]byte(spec))
3754
3755 m, err := doc.BuildV3Model()
3756 assert.NoError(t, err)
3757
3758 v := NewParameterValidator(&m.Model)
3759
3760 // Helper to build a request
3761 makeRequest := func(ids []int) *http.Request {
3762 values := make([]string, len(ids))
3763 for i, id := range ids {
3764 values[i] = fmt.Sprintf("%d", id)
3765 }
3766 req, _ := http.NewRequest(http.MethodGet, "/items?id="+strings.Join(values, ","), nil)
3767 return req
3768 }
3769
3770 // Test invalid case (12 items)
3771 invalidReq := makeRequest([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})
3772
3773 valid, errors := v.ValidateQueryParams(invalidReq)
3774 assert.False(t, valid)
3775 assert.Len(t, errors, 1)
3776 assert.Equal(t, errors[0].Reason, "The query parameter (which is an array) 'id' has a maximum item length of 10, however the request provided 12 items")
3777 }
3778
3779 // https://github.com/pb33f/libopenapi-validator/issues/140
3780 func TestNewValidator_CheckQueryParamsMinItems(t *testing.T) {
3781 spec := `openapi: 3.1.0
3782 info:
3783 title: ID List API
3784 version: "1.0.0"
3785 paths:
3786 /items:
3787 get:
3788 parameters:
3789 - name: id
3790 in: query
3791 required: true
3792 style: form
3793 explode: false
3794 schema:
3795 type: array
3796 items:
3797 type: integer
3798 minItems: 10
3799 responses:
3800 '200':
3801 description: OK
3802 '400':
3803 description: Invalid input`
3804
3805 doc, _ := libopenapi.NewDocument([]byte(spec))
3806
3807 m, err := doc.BuildV3Model()
3808 assert.NoError(t, err)
3809
3810 v := NewParameterValidator(&m.Model)
3811
3812 // Helper to build a request
3813 makeRequest := func(ids []int) *http.Request {
3814 values := make([]string, len(ids))
3815 for i, id := range ids {
3816 values[i] = fmt.Sprintf("%d", id)
3817 }
3818 req, _ := http.NewRequest(http.MethodGet, "/items?id="+strings.Join(values, ","), nil)
3819 return req
3820 }
3821
3822 // Test invalid case (12 items)
3823 invalidReq := makeRequest([]int{1, 2, 3})
3824
3825 valid, errors := v.ValidateQueryParams(invalidReq)
3826 assert.False(t, valid)
3827 assert.Len(t, errors, 1)
3828 assert.Equal(t, errors[0].Reason, "The query parameter (which is an array) 'id' has a minimum items length of 10, however the request provided 3 items")
3829 }
3830
3831 func TestNewValidator_CheckQueryParamsUniqueItems(t *testing.T) {
3832 spec := `openapi: 3.1.0
3833 info:
3834 title: ID List API
3835 version: "1.0.0"
3836 paths:
3837 /items:
3838 get:
3839 parameters:
3840 - name: id
3841 in: query
3842 required: true
3843 style: form
3844 explode: false
3845 schema:
3846 type: array
3847 items:
3848 type: string
3849 uniqueItems: true
3850 responses:
3851 '200':
3852 description: OK
3853 '400':
3854 description: Invalid input`
3855
3856 doc, _ := libopenapi.NewDocument([]byte(spec))
3857
3858 m, err := doc.BuildV3Model()
3859 assert.NoError(t, err)
3860
3861 v := NewParameterValidator(&m.Model)
3862
3863 // Helper to build a request
3864 makeRequest := func(ids []string) *http.Request {
3865 values := make([]string, len(ids))
3866 copy(values, ids)
3867 req, _ := http.NewRequest(http.MethodGet, "/items?id="+strings.Join(values, ","), nil)
3868 return req
3869 }
3870
3871 // Test invalid case (12 items)
3872 invalidReq := makeRequest([]string{"cake", "cake", "meat", "potatoes", "eggs", "meat"})
3873
3874 valid, errors := v.ValidateQueryParams(invalidReq)
3875 assert.False(t, valid)
3876 assert.Len(t, errors, 1)
3877 assert.Equal(t, errors[0].Reason, "The query parameter (which is an array) 'id' contains the following duplicates: 'cake, meat'")
3878 }
3879
3880 func TestNewValidator_QueryParamWithBracketsInName(t *testing.T) {
3881 // Test for issue #210: parameter names with brackets (e.g., match[])
3882 // should be recognized when URL-encoded as match%5B%5D
3883 // https://github.com/pb33f/libopenapi-validator/issues/210
3884 spec := `openapi: 3.1.0
3885 paths:
3886 /api/query:
3887 get:
3888 parameters:
3889 - name: "match[]"
3890 in: query
3891 required: true
3892 explode: false
3893 schema:
3894 type: array
3895 items:
3896 type: string
3897 - name: start
3898 in: query
3899 schema:
3900 type: integer
3901 - name: end
3902 in: query
3903 schema:
3904 type: integer
3905 operationId: queryData
3906 `
3907
3908 doc, _ := libopenapi.NewDocument([]byte(spec))
3909 m, _ := doc.BuildV3Model()
3910 v := NewParameterValidator(&m.Model)
3911
3912 // URL with encoded brackets: match%5B%5D=up (decodes to match[]=up)
3913 request, _ := http.NewRequest(http.MethodGet,
3914 "https://example.com/api/query?match%5B%5D=up&start=0&end=100", nil)
3915
3916 valid, errors := v.ValidateQueryParams(request)
3917 assert.True(t, valid, "Expected validation to pass, got errors: %v", errors)
3918 assert.Empty(t, errors)
3919 }
3920
3921 func TestNewValidator_QueryParamWithBracketsInName_Missing(t *testing.T) {
3922 // Test that missing bracket parameters are still reported correctly
3923 spec := `openapi: 3.1.0
3924 paths:
3925 /api/query:
3926 get:
3927 parameters:
3928 - name: "match[]"
3929 in: query
3930 required: true
3931 schema:
3932 type: array
3933 items:
3934 type: string
3935 operationId: queryData
3936 `
3937
3938 doc, _ := libopenapi.NewDocument([]byte(spec))
3939 m, _ := doc.BuildV3Model()
3940 v := NewParameterValidator(&m.Model)
3941
3942 // Request without the required match[] parameter
3943 request, _ := http.NewRequest(http.MethodGet,
3944 "https://example.com/api/query?other=value", nil)
3945
3946 valid, errors := v.ValidateQueryParams(request)
3947 assert.False(t, valid)
3948 assert.Len(t, errors, 1)
3949 assert.Equal(t, "Query parameter 'match[]' is missing", errors[0].Message)
3950 }
3951
3952 func TestNewValidator_QueryParams_StrictMode_UndeclaredParam(t *testing.T) {
3953 spec := `openapi: 3.1.0
3954 paths:
3955 /api/search:
3956 get:
3957 parameters:
3958 - name: query
3959 in: query
3960 required: true
3961 schema:
3962 type: string
3963 - name: limit
3964 in: query
3965 schema:
3966 type: integer
3967 operationId: search
3968 `
3969
3970 doc, _ := libopenapi.NewDocument([]byte(spec))
3971 m, _ := doc.BuildV3Model()
3972 v := NewParameterValidator(&m.Model, config.WithStrictMode())
3973
3974 // Request with undeclared 'extra' parameter
3975 request, _ := http.NewRequest(http.MethodGet,
3976 "https://example.com/api/search?query=test&limit=10&extra=undeclared", nil)
3977
3978 valid, errors := v.ValidateQueryParams(request)
3979 assert.False(t, valid)
3980 assert.Len(t, errors, 1)
3981 assert.Contains(t, errors[0].Message, "extra")
3982 assert.Contains(t, errors[0].Message, "not declared")
3983 }
3984
3985 func TestNewValidator_QueryParams_StrictMode_ValidRequest(t *testing.T) {
3986 spec := `openapi: 3.1.0
3987 paths:
3988 /api/search:
3989 get:
3990 parameters:
3991 - name: query
3992 in: query
3993 required: true
3994 schema:
3995 type: string
3996 - name: limit
3997 in: query
3998 schema:
3999 type: integer
4000 operationId: search
4001 `
4002
4003 doc, _ := libopenapi.NewDocument([]byte(spec))
4004 m, _ := doc.BuildV3Model()
4005 v := NewParameterValidator(&m.Model, config.WithStrictMode())
4006
4007 // Request with only declared parameters
4008 request, _ := http.NewRequest(http.MethodGet,
4009 "https://example.com/api/search?query=test&limit=10", nil)
4010
4011 valid, errors := v.ValidateQueryParams(request)
4012 assert.True(t, valid)
4013 assert.Len(t, errors, 0)
4014 }
4015
4016 // TestNewValidator_QueryParamExplodedObjectStringType_Issue91 reproduces GitHub issue #91:
4017 // exploded object query params with type: string properties that look numeric should not
4018 // be eagerly cast to numbers before schema validation.
4019 func TestNewValidator_QueryParamExplodedObjectStringType_Issue91(t *testing.T) {
4020 spec := `openapi: 3.1.0
4021 paths:
4022 /products:
4023 get:
4024 parameters:
4025 - name: filter
4026 in: query
4027 required: true
4028 explode: true
4029 schema:
4030 type: object
4031 properties:
4032 item_count:
4033 type: string
4034 search_term:
4035 type: string
4036 required:
4037 - item_count
4038 - search_term
4039 operationId: listProducts`
4040
4041 doc, _ := libopenapi.NewDocument([]byte(spec))
4042 m, _ := doc.BuildV3Model()
4043 v := NewParameterValidator(&m.Model)
4044
4045 request, _ := http.NewRequest(http.MethodGet,
4046 "https://example.com/products?item_count=10&search_term=foo", nil)
4047
4048 valid, errors := v.ValidateQueryParams(request)
4049 assert.True(t, valid, "validation should pass: item_count=10 is valid for type: string")
4050 assert.Empty(t, errors)
4051 }
4052
4053 // TestNewValidator_QueryParamExplodedObjectMixedTypes_Issue91 tests that mixed types
4054 // in an exploded object are correctly handled: string properties stay strings,
4055 // integer properties get cast.
4056 func TestNewValidator_QueryParamExplodedObjectMixedTypes_Issue91(t *testing.T) {
4057 spec := `openapi: 3.1.0
4058 paths:
4059 /products:
4060 get:
4061 parameters:
4062 - name: filter
4063 in: query
4064 required: true
4065 explode: true
4066 schema:
4067 type: object
4068 properties:
4069 item_count:
4070 type: string
4071 page:
4072 type: integer
4073 minimum: 1
4074 required:
4075 - item_count
4076 - page
4077 operationId: listProducts`
4078
4079 doc, _ := libopenapi.NewDocument([]byte(spec))
4080 m, _ := doc.BuildV3Model()
4081 v := NewParameterValidator(&m.Model)
4082
4083 request, _ := http.NewRequest(http.MethodGet,
4084 "https://example.com/products?item_count=10&page=2", nil)
4085
4086 valid, errors := v.ValidateQueryParams(request)
4087 assert.True(t, valid, "validation should pass: item_count=10 is string, page=2 is integer >= 1")
4088 assert.Empty(t, errors)
4089 }
4090
4091 // TestNewValidator_QueryParamExactIssue91Reproduction uses the exact spec and URL from
4092 // https://github.com/pb33f/wiretap/issues/91 to verify the fix.
4093 func TestNewValidator_QueryParamExactIssue91Reproduction(t *testing.T) {
4094 spec := `openapi: 3.1.0
4095 info:
4096 title: Test
4097 version: 0.1.0
4098 paths:
4099 /anything/queryParams/form/camelObj:
4100 get:
4101 operationId: formQueryParamsCamelObject
4102 parameters:
4103 - name: obj_param_exploded
4104 in: query
4105 explode: true
4106 schema:
4107 type: object
4108 properties:
4109 search_term:
4110 type: string
4111 example: foo
4112 item_count:
4113 type: string
4114 example: "10"
4115 required: true
4116 - name: obj_param
4117 in: query
4118 explode: false
4119 schema:
4120 type: object
4121 properties:
4122 encoded_term:
4123 type: string
4124 example: bar
4125 encoded_count:
4126 type: string
4127 example: "11"
4128 responses:
4129 "200":
4130 description: OK`
4131
4132 doc, _ := libopenapi.NewDocument([]byte(spec))
4133 m, _ := doc.BuildV3Model()
4134 v := NewParameterValidator(&m.Model)
4135
4136 request, _ := http.NewRequest(http.MethodGet,
4137 "https://localhost:35123/anything/queryParams/form/camelObj?item_count=10&obj_param=encoded_count%2C11%2Cencoded_term%2Cbar&search_term=foo", nil)
4138
4139 valid, errors := v.ValidateQueryParams(request)
4140 assert.True(t, valid, "issue #91: item_count=10 with type: string should not fail with 'expected string, but got number'")
4141 assert.Empty(t, errors)
4142 }