Debian Codegraph @

parameters/path_parameters_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package parameters
5
6 import (
7 "net/http"
8 "sync"
9 "sync/atomic"
10 "testing"
11
12 "github.com/pb33f/libopenapi"
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15
16 "github.com/pb33f/libopenapi-validator/config"
17 "github.com/pb33f/libopenapi-validator/helpers"
18 "github.com/pb33f/libopenapi-validator/paths"
19 "github.com/pb33f/libopenapi-validator/radix"
20 )
21
22 func TestNewValidator_SimpleArrayEncodedPath(t *testing.T) {
23 spec := `openapi: 3.1.0
24 paths:
25 /burgers/{burgerIds*}/locate:
26 parameters:
27 - name: burgerIds
28 in: path
29 schema:
30 type: array
31 items:
32 type: integer
33 patch:
34 operationId: locateBurgers`
35
36 doc, _ := libopenapi.NewDocument([]byte(spec))
37
38 m, _ := doc.BuildV3Model()
39
40 v := NewParameterValidator(&m.Model)
41
42 request, _ := http.NewRequest(http.MethodPatch, "https://things.com/burgers/1,2,3,4,5/locate", nil)
43 valid, errors := v.ValidatePathParams(request)
44
45 assert.True(t, valid)
46 assert.Len(t, errors, 0)
47 }
48
49 func TestNewValidator_PathParamURLEncoding_EnumValidation(t *testing.T) {
50 spec := `openapi: 3.1.0
51 paths:
52 /test/{pathParam}:
53 parameters:
54 - name: pathParam
55 in: path
56 required: true
57 schema:
58 type: string
59 enum: ["foo/bar", "hello world", "special@chars"]
60 get:
61 operationId: testOperation`
62
63 doc, _ := libopenapi.NewDocument([]byte(spec))
64 m, _ := doc.BuildV3Model()
65 v := NewParameterValidator(&m.Model)
66
67 // Test 1: URL-encoded forward slash should match enum value "foo/bar"
68 request1, _ := http.NewRequest(http.MethodGet, "https://example.com/test/foo%2Fbar", nil)
69 valid1, errors1 := v.ValidatePathParams(request1)
70 assert.True(t, valid1, "URL-encoded path parameter 'foo%%2Fbar' should match enum value 'foo/bar'")
71 assert.Len(t, errors1, 0)
72
73 // Test 2: URL-encoded space should match enum value "hello world"
74 request2, _ := http.NewRequest(http.MethodGet, "https://example.com/test/hello%20world", nil)
75 valid2, errors2 := v.ValidatePathParams(request2)
76 assert.True(t, valid2, "URL-encoded path parameter 'hello%%20world' should match enum value 'hello world'")
77 assert.Len(t, errors2, 0)
78
79 // Test 3: URL-encoded @ symbol should match enum value "special@chars"
80 request3, _ := http.NewRequest(http.MethodGet, "https://example.com/test/special%40chars", nil)
81 valid3, errors3 := v.ValidatePathParams(request3)
82 assert.True(t, valid3, "URL-encoded path parameter 'special%%40chars' should match enum value 'special@chars'")
83 assert.Len(t, errors3, 0)
84
85 // Test 4: Non-matching encoded value should fail
86 request4, _ := http.NewRequest(http.MethodGet, "https://example.com/test/not%2Dfound", nil)
87 valid4, errors4 := v.ValidatePathParams(request4)
88 assert.False(t, valid4, "URL-encoded path parameter 'not%%2Dfound' should not match any enum values")
89 assert.Len(t, errors4, 1)
90 assert.Contains(t, errors4[0].Reason, "pre-defined values set via an enum")
91 }
92
93 func TestNewValidator_PathParamURLEncoding_BackwardCompatibility(t *testing.T) {
94 spec := `openapi: 3.1.0
95 paths:
96 /test/{pathParam}:
97 parameters:
98 - name: pathParam
99 in: path
100 required: true
101 schema:
102 type: string
103 enum: ["normal-value", "encoded%2Fvalue"]
104 get:
105 operationId: testOperation`
106
107 doc, _ := libopenapi.NewDocument([]byte(spec))
108 m, _ := doc.BuildV3Model()
109 v := NewParameterValidator(&m.Model)
110
111 // Test backward compatibility: if enum contains encoded values,
112 // they should still work for backward compatibility
113 request, _ := http.NewRequest(http.MethodGet, "https://example.com/test/encoded%2Fvalue", nil)
114 valid, errors := v.ValidatePathParams(request)
115 // This should fail because the decoded value "encoded/value" doesn't match "encoded%2Fvalue"
116 assert.False(t, valid, "Encoded enum values should require exact match after decoding")
117 assert.Len(t, errors, 1)
118 assert.Contains(t, errors[0].Reason, "pre-defined values set via an enum")
119 }
120
121 func TestNewValidator_PathParamURLEncoding_IntegerEnum(t *testing.T) {
122 spec := `openapi: 3.1.0
123 paths:
124 /test/{numParam}:
125 parameters:
126 - name: numParam
127 in: path
128 required: true
129 schema:
130 type: integer
131 enum: [123, 456]
132 get:
133 operationId: testOperation`
134
135 doc, _ := libopenapi.NewDocument([]byte(spec))
136 m, _ := doc.BuildV3Model()
137 v := NewParameterValidator(&m.Model)
138
139 // Test URL-encoded integer (though integers typically don't need encoding)
140 request, _ := http.NewRequest(http.MethodGet, "https://example.com/test/123", nil)
141 valid, errors := v.ValidatePathParams(request)
142 assert.True(t, valid, "Integer parameter should validate correctly")
143 assert.Len(t, errors, 0)
144 }
145
146 func TestNewValidator_SimpleArrayEncodedPath_InvalidInteger(t *testing.T) {
147 spec := `openapi: 3.1.0
148 paths:
149 /burgers/{burgerIds*}/locate:
150 parameters:
151 - name: burgerIds
152 in: path
153 schema:
154 type: array
155 items:
156 type: integer
157 get:
158 operationId: locateBurgers`
159
160 doc, _ := libopenapi.NewDocument([]byte(spec))
161
162 m, _ := doc.BuildV3Model()
163
164 v := NewParameterValidator(&m.Model)
165
166 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/1,pizza,3,4,false/locate", nil)
167 valid, errors := v.ValidatePathParams(request)
168
169 assert.False(t, valid)
170 assert.Len(t, errors, 2)
171 assert.Equal(t, "Path array parameter 'burgerIds' is not a valid integer", errors[0].Message)
172 assert.Equal(t, request.Method, errors[0].RequestMethod)
173 assert.Equal(t, request.URL.Path, errors[0].RequestPath)
174 assert.Equal(t, "/burgers/{burgerIds*}/locate", errors[0].SpecPath)
175 }
176
177 func TestNewValidator_SimpleArrayEncodedPath_InvalidNumber(t *testing.T) {
178 spec := `openapi: 3.1.0
179 paths:
180 /burgers/{burgerIds*}/locate:
181 parameters:
182 - name: burgerIds
183 in: path
184 schema:
185 type: array
186 items:
187 type: number
188 get:
189 operationId: locateBurgers`
190
191 doc, _ := libopenapi.NewDocument([]byte(spec))
192
193 m, _ := doc.BuildV3Model()
194
195 v := NewParameterValidator(&m.Model)
196
197 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/1,pizza,3,4,false/locate", nil)
198 valid, errors := v.ValidatePathParams(request)
199
200 assert.False(t, valid)
201 assert.Len(t, errors, 2)
202 assert.Equal(t, "Path array parameter 'burgerIds' is not a valid number", errors[0].Message)
203 assert.Equal(t, request.Method, errors[0].RequestMethod)
204 assert.Equal(t, request.URL.Path, errors[0].RequestPath)
205 assert.Equal(t, "/burgers/{burgerIds*}/locate", errors[0].SpecPath)
206 }
207
208 func TestNewValidator_SimpleArrayEncodedPath_InvalidBool(t *testing.T) {
209 spec := `openapi: 3.1.0
210 paths:
211 /burgers/{burgerIds*}/locate:
212 parameters:
213 - name: burgerIds
214 in: path
215 schema:
216 type: array
217 items:
218 type: boolean
219 get:
220 operationId: locateBurgers`
221
222 doc, _ := libopenapi.NewDocument([]byte(spec))
223
224 m, _ := doc.BuildV3Model()
225
226 v := NewParameterValidator(&m.Model)
227
228 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/1,true,0,frogs,false/locate", nil)
229 valid, errors := v.ValidatePathParams(request)
230
231 assert.False(t, valid)
232 assert.Len(t, errors, 3)
233 assert.Equal(t, "Path array parameter 'burgerIds' is not a valid boolean", errors[0].Message)
234 }
235
236 func TestNewValidator_SimpleObjectEncodedPath(t *testing.T) {
237 spec := `openapi: 3.1.0
238 paths:
239 /burgers/{burger}/locate:
240 parameters:
241 - name: burger
242 in: path
243 schema:
244 type: object
245 properties:
246 id:
247 type: integer
248 vegetarian:
249 type: boolean
250 get:
251 operationId: locateBurger`
252
253 doc, _ := libopenapi.NewDocument([]byte(spec))
254
255 m, _ := doc.BuildV3Model()
256
257 v := NewParameterValidator(&m.Model)
258
259 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/id,1234,vegetarian,true/locate", nil)
260 valid, errors := v.ValidatePathParams(request)
261
262 assert.True(t, valid)
263 assert.Len(t, errors, 0)
264 }
265
266 func TestNewValidator_SimpleObjectEncodedPath_Invalid(t *testing.T) {
267 spec := `openapi: 3.1.0
268 paths:
269 /burgers/{burger}/locate:
270 parameters:
271 - name: burger
272 in: path
273 schema:
274 type: object
275 properties:
276 id:
277 type: integer
278 vegetarian:
279 type: boolean
280 get:
281 operationId: locateBurger`
282
283 doc, _ := libopenapi.NewDocument([]byte(spec))
284
285 m, _ := doc.BuildV3Model()
286
287 v := NewParameterValidator(&m.Model)
288
289 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/id,hello,vegetarian,there/locate", nil)
290 valid, errors := v.ValidatePathParams(request)
291
292 assert.False(t, valid)
293 assert.Len(t, errors, 1)
294 assert.Len(t, errors[0].SchemaValidationErrors, 2)
295 }
296
297 func TestNewValidator_SimpleObjectEncodedPath_Exploded(t *testing.T) {
298 spec := `openapi: 3.1.0
299 paths:
300 /burgers/{burger}/locate:
301 parameters:
302 - name: burger
303 in: path
304 explode: true
305 schema:
306 type: object
307 properties:
308 id:
309 type: integer
310 vegetarian:
311 type: boolean
312 get:
313 operationId: locateBurger`
314
315 doc, _ := libopenapi.NewDocument([]byte(spec))
316
317 m, _ := doc.BuildV3Model()
318
319 v := NewParameterValidator(&m.Model)
320
321 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/id=1234,vegetarian=true/locate", nil)
322 valid, errors := v.ValidatePathParams(request)
323
324 assert.True(t, valid)
325 assert.Len(t, errors, 0)
326 }
327
328 func TestNewValidator_SimpleObjectEncodedPath_ExplodedInvalid(t *testing.T) {
329 spec := `openapi: 3.1.0
330 paths:
331 /burgers/{burger}/locate:
332 parameters:
333 - name: burger
334 in: path
335 explode: true
336 schema:
337 type: object
338 properties:
339 id:
340 type: integer
341 vegetarian:
342 type: boolean
343 get:
344 operationId: locateBurger`
345
346 doc, _ := libopenapi.NewDocument([]byte(spec))
347
348 m, _ := doc.BuildV3Model()
349
350 v := NewParameterValidator(&m.Model)
351
352 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/id=toast,vegetarian=chicken/locate", nil)
353 valid, errors := v.ValidatePathParams(request)
354
355 assert.False(t, valid)
356 assert.Len(t, errors, 1)
357 assert.Len(t, errors[0].SchemaValidationErrors, 2)
358 }
359
360 func TestNewValidator_ObjectEncodedPath(t *testing.T) {
361 spec := `openapi: 3.1.0
362 paths:
363 /burgers/{burger}/locate:
364 parameters:
365 - name: burger
366 in: path
367 schema:
368 type: object
369 properties:
370 id:
371 type: integer
372 vegetarian:
373 type: boolean
374 get:
375 operationId: locateBurger`
376
377 doc, _ := libopenapi.NewDocument([]byte(spec))
378
379 m, _ := doc.BuildV3Model()
380
381 v := NewParameterValidator(&m.Model)
382
383 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/id,1234,vegetarian,true/locate", nil)
384 valid, errors := v.ValidatePathParams(request)
385
386 assert.True(t, valid)
387 assert.Len(t, errors, 0)
388 }
389
390 func TestNewValidator_SimpleEncodedPath_InvalidInteger(t *testing.T) {
391 spec := `openapi: 3.1.0
392 paths:
393 /burgers/{burgerId}/locate:
394 parameters:
395 - name: burgerId
396 in: path
397 schema:
398 type: integer
399 get:
400 operationId: locateBurgers`
401
402 doc, _ := libopenapi.NewDocument([]byte(spec))
403
404 m, _ := doc.BuildV3Model()
405
406 v := NewParameterValidator(&m.Model)
407
408 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/hello/locate", nil)
409 valid, errors := v.ValidatePathParams(request)
410
411 assert.False(t, valid)
412 assert.Len(t, errors, 1)
413 assert.Equal(t, "Path parameter 'burgerId' is not a valid integer", errors[0].Message)
414 assert.Equal(t, "The path parameter 'burgerId' is defined as being an integer, however the value 'hello' is not a valid integer", errors[0].Reason)
415 }
416
417 func TestNewValidator_SimpleEncodedPath_MinimumIntegerViolation(t *testing.T) {
418 spec := `openapi: 3.1.0
419 paths:
420 /burgers/{burgerId}/locate:
421 parameters:
422 - name: burgerId
423 in: path
424 schema:
425 type: integer
426 minimum: 10
427 get:
428 operationId: locateBurgers`
429
430 doc, err := libopenapi.NewDocument([]byte(spec))
431 require.NoError(t, err)
432 m, _ := doc.BuildV3Model()
433
434 v := NewParameterValidator(&m.Model)
435
436 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/1/locate", nil)
437 valid, errors := v.ValidatePathParams(request)
438
439 assert.False(t, valid)
440 assert.Len(t, errors, 1)
441 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
442 assert.Len(t, errors[0].SchemaValidationErrors, 1)
443 assert.Equal(t, "Reason: minimum: got 1, want 10", errors[0].SchemaValidationErrors[0].Error())
444 }
445
446 func TestNewValidator_SimpleEncodedPath_MinimumInteger(t *testing.T) {
447 spec := `openapi: 3.1.0
448 paths:
449 /burgers/{burgerId}/locate:
450 parameters:
451 - name: burgerId
452 in: path
453 schema:
454 type: integer
455 minimum: 10
456 get:
457 operationId: locateBurgers`
458
459 doc, err := libopenapi.NewDocument([]byte(spec))
460 require.NoError(t, err)
461 m, _ := doc.BuildV3Model()
462
463 v := NewParameterValidator(&m.Model)
464
465 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/14/locate", nil)
466 valid, errors := v.ValidatePathParams(request)
467
468 assert.True(t, valid)
469 assert.Nil(t, errors)
470 }
471
472 func TestNewValidator_SimpleEncodedPath_MaximumIntegerViolation(t *testing.T) {
473 spec := `openapi: 3.1.0
474 paths:
475 /burgers/{burgerId}/locate:
476 parameters:
477 - name: burgerId
478 in: path
479 schema:
480 type: integer
481 maximum: 10
482 get:
483 operationId: locateBurgers`
484
485 doc, err := libopenapi.NewDocument([]byte(spec))
486 require.NoError(t, err)
487 m, _ := doc.BuildV3Model()
488
489 v := NewParameterValidator(&m.Model)
490
491 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/11/locate", nil)
492 valid, errors := v.ValidatePathParams(request)
493
494 assert.False(t, valid)
495 assert.Len(t, errors, 1)
496 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
497 assert.Len(t, errors[0].SchemaValidationErrors, 1)
498 assert.Equal(t, "Reason: maximum: got 11, want 10", errors[0].SchemaValidationErrors[0].Error())
499 }
500
501 func TestNewValidator_SimpleEncodedPath_MaximumInteger(t *testing.T) {
502 spec := `openapi: 3.1.0
503 paths:
504 /burgers/{burgerId}/locate:
505 parameters:
506 - name: burgerId
507 in: path
508 schema:
509 type: integer
510 maximum: 10
511 get:
512 operationId: locateBurgers`
513
514 doc, err := libopenapi.NewDocument([]byte(spec))
515 require.NoError(t, err)
516 m, _ := doc.BuildV3Model()
517
518 v := NewParameterValidator(&m.Model)
519
520 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/4/locate", nil)
521 valid, errors := v.ValidatePathParams(request)
522
523 assert.True(t, valid)
524 assert.Nil(t, errors)
525 }
526
527 func TestNewValidator_SimpleEncodedPath_InvalidNumber(t *testing.T) {
528 spec := `openapi: 3.1.0
529 paths:
530 /burgers/{burgerId}/locate:
531 parameters:
532 - name: burgerId
533 in: path
534 schema:
535 type: number
536 get:
537 operationId: locateBurgers`
538
539 doc, _ := libopenapi.NewDocument([]byte(spec))
540
541 m, _ := doc.BuildV3Model()
542
543 v := NewParameterValidator(&m.Model)
544
545 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/hello/locate", nil)
546 valid, errors := v.ValidatePathParams(request)
547
548 assert.False(t, valid)
549 assert.Len(t, errors, 1)
550 assert.Equal(t, "Path parameter 'burgerId' is not a valid number", errors[0].Message)
551 assert.Equal(t, "The path parameter 'burgerId' is defined as being a number, however the value 'hello' is not a valid number", errors[0].Reason)
552 }
553
554 func TestNewValidator_SimpleEncodedPath_MinimumNumberViolation(t *testing.T) {
555 spec := `openapi: 3.1.0
556 paths:
557 /burgers/{burgerId}/locate:
558 parameters:
559 - name: burgerId
560 in: path
561 schema:
562 type: number
563 minimum: 10.2
564 get:
565 operationId: locateBurgers`
566
567 doc, err := libopenapi.NewDocument([]byte(spec))
568 require.NoError(t, err)
569 m, _ := doc.BuildV3Model()
570
571 v := NewParameterValidator(&m.Model)
572
573 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/1.3/locate", nil)
574 valid, errors := v.ValidatePathParams(request)
575
576 assert.False(t, valid)
577 assert.Len(t, errors, 1)
578 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
579 assert.Len(t, errors[0].SchemaValidationErrors, 1)
580 assert.Equal(t, "Reason: minimum: got 1.3, want 10.2", errors[0].SchemaValidationErrors[0].Error())
581 }
582
583 func TestNewValidator_SimpleEncodedPath_MinimumNumber(t *testing.T) {
584 spec := `openapi: 3.1.0
585 paths:
586 /burgers/{burgerId}/locate:
587 parameters:
588 - name: burgerId
589 in: path
590 schema:
591 type: number
592 minimum: 10.3
593 get:
594 operationId: locateBurgers`
595
596 doc, err := libopenapi.NewDocument([]byte(spec))
597 require.NoError(t, err)
598 m, _ := doc.BuildV3Model()
599
600 v := NewParameterValidator(&m.Model)
601
602 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/14.5/locate", nil)
603 valid, errors := v.ValidatePathParams(request)
604
605 assert.True(t, valid)
606 assert.Nil(t, errors)
607 }
608
609 func TestNewValidator_SimpleEncodedPath_MaximumNumberViolation(t *testing.T) {
610 spec := `openapi: 3.1.0
611 paths:
612 /burgers/{burgerId}/locate:
613 parameters:
614 - name: burgerId
615 in: path
616 schema:
617 type: number
618 maximum: 10.2
619 get:
620 operationId: locateBurgers`
621
622 doc, err := libopenapi.NewDocument([]byte(spec))
623 require.NoError(t, err)
624 m, _ := doc.BuildV3Model()
625
626 v := NewParameterValidator(&m.Model)
627
628 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/11.2/locate", nil)
629 valid, errors := v.ValidatePathParams(request)
630
631 assert.False(t, valid)
632 assert.Len(t, errors, 1)
633 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
634 assert.Len(t, errors[0].SchemaValidationErrors, 1)
635 assert.Equal(t, "Reason: maximum: got 11.2, want 10.2", errors[0].SchemaValidationErrors[0].Error())
636 }
637
638 func TestNewValidator_SimpleEncodedPath_MaximumNumber(t *testing.T) {
639 spec := `openapi: 3.1.0
640 paths:
641 /burgers/{burgerId}/locate:
642 parameters:
643 - name: burgerId
644 in: path
645 schema:
646 type: number
647 maximum: 10.2
648 get:
649 operationId: locateBurgers`
650
651 doc, err := libopenapi.NewDocument([]byte(spec))
652 require.NoError(t, err)
653 m, _ := doc.BuildV3Model()
654
655 v := NewParameterValidator(&m.Model)
656
657 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/4.5/locate", nil)
658 valid, errors := v.ValidatePathParams(request)
659
660 assert.True(t, valid)
661 assert.Nil(t, errors)
662 }
663
664 func TestNewValidator_SimpleEncodedPath_InvalidBoolean(t *testing.T) {
665 spec := `openapi: 3.1.0
666 paths:
667 /burgers/{burgerId}/locate:
668 parameters:
669 - name: burgerId
670 in: path
671 schema:
672 type: boolean
673 get:
674 operationId: locateBurgers`
675
676 doc, _ := libopenapi.NewDocument([]byte(spec))
677
678 m, _ := doc.BuildV3Model()
679
680 v := NewParameterValidator(&m.Model)
681
682 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/hello/locate", nil)
683 valid, errors := v.ValidatePathParams(request)
684
685 assert.False(t, valid)
686 assert.Len(t, errors, 1)
687 assert.Equal(t, "Path parameter 'burgerId' is not a valid boolean", errors[0].Message)
688 }
689
690 func TestNewValidator_LabelEncodedPath_InvalidInteger(t *testing.T) {
691 spec := `openapi: 3.1.0
692 paths:
693 /burgers/{.burgerId}/locate:
694 parameters:
695 - name: burgerId
696 in: path
697 style: label
698 schema:
699 type: integer
700 get:
701 operationId: locateBurgers`
702
703 doc, _ := libopenapi.NewDocument([]byte(spec))
704
705 m, _ := doc.BuildV3Model()
706
707 v := NewParameterValidator(&m.Model)
708
709 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.hello/locate", nil)
710 valid, errors := v.ValidatePathParams(request)
711
712 assert.False(t, valid)
713 assert.Len(t, errors, 1)
714 assert.Equal(t, "Path parameter 'burgerId' is not a valid integer", errors[0].Message)
715 }
716
717 func TestNewValidator_LabelEncodedPath_MinimumIntegerViolation(t *testing.T) {
718 spec := `openapi: 3.1.0
719 paths:
720 /burgers/{.burgerId}/locate:
721 parameters:
722 - name: burgerId
723 in: path
724 style: label
725 schema:
726 type: integer
727 minimum: 10
728 get:
729 operationId: locateBurgers`
730
731 doc, _ := libopenapi.NewDocument([]byte(spec))
732
733 m, _ := doc.BuildV3Model()
734
735 v := NewParameterValidator(&m.Model)
736
737 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3/locate", nil)
738 valid, errors := v.ValidatePathParams(request)
739
740 assert.False(t, valid)
741 assert.Len(t, errors, 1)
742 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
743 assert.Len(t, errors[0].SchemaValidationErrors, 1)
744 assert.Equal(t, "Reason: minimum: got 3, want 10", errors[0].SchemaValidationErrors[0].Error())
745 }
746
747 func TestNewValidator_LabelEncodedPath_MaximumIntegerViolation(t *testing.T) {
748 spec := `openapi: 3.1.0
749 paths:
750 /burgers/{.burgerId}/locate:
751 parameters:
752 - name: burgerId
753 in: path
754 style: label
755 schema:
756 type: integer
757 maximum: 10
758 get:
759 operationId: locateBurgers`
760
761 doc, _ := libopenapi.NewDocument([]byte(spec))
762
763 m, _ := doc.BuildV3Model()
764
765 v := NewParameterValidator(&m.Model)
766
767 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.32/locate", nil)
768 valid, errors := v.ValidatePathParams(request)
769
770 assert.False(t, valid)
771 assert.Len(t, errors, 1)
772 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
773 assert.Len(t, errors[0].SchemaValidationErrors, 1)
774 assert.Equal(t, "Reason: maximum: got 32, want 10", errors[0].SchemaValidationErrors[0].Error())
775 }
776
777 func TestNewValidator_LabelEncodedPath_InvalidBoolean(t *testing.T) {
778 spec := `openapi: 3.1.0
779 paths:
780 /burgers/{.burgerId}/locate:
781 parameters:
782 - name: burgerId
783 in: path
784 style: label
785 schema:
786 type: boolean
787 get:
788 operationId: locateBurgers`
789
790 doc, _ := libopenapi.NewDocument([]byte(spec))
791
792 m, _ := doc.BuildV3Model()
793
794 v := NewParameterValidator(&m.Model)
795
796 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.hello/locate", nil)
797 valid, errors := v.ValidatePathParams(request)
798
799 assert.False(t, valid)
800 assert.Len(t, errors, 1)
801 assert.Equal(t, "Path parameter 'burgerId' is not a valid boolean", errors[0].Message)
802 }
803
804 func TestNewValidator_LabelEncodedPath_ValidBoolean(t *testing.T) {
805 spec := `openapi: 3.1.0
806 paths:
807 /burgers/{.burgerId}/locate:
808 parameters:
809 - name: burgerId
810 in: path
811 style: label
812 schema:
813 type: boolean
814 get:
815 operationId: locateBurgers`
816
817 doc, _ := libopenapi.NewDocument([]byte(spec))
818
819 m, _ := doc.BuildV3Model()
820
821 v := NewParameterValidator(&m.Model)
822
823 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.true/locate", nil)
824 valid, errors := v.ValidatePathParams(request)
825
826 assert.True(t, valid)
827 assert.Len(t, errors, 0)
828 }
829
830 func TestNewValidator_LabelEncodedPath_ValidArray_Integer(t *testing.T) {
831 spec := `openapi: 3.1.0
832 paths:
833 /burgers/{.burgerId}/locate:
834 parameters:
835 - name: burgerId
836 in: path
837 style: label
838 schema:
839 type: array
840 items:
841 type: integer
842 get:
843 operationId: locateBurgers`
844
845 doc, _ := libopenapi.NewDocument([]byte(spec))
846
847 m, _ := doc.BuildV3Model()
848
849 v := NewParameterValidator(&m.Model)
850
851 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3,4/locate", nil)
852 valid, errors := v.ValidatePathParams(request)
853
854 assert.True(t, valid)
855 assert.Len(t, errors, 0)
856 }
857
858 func TestNewValidator_LabelEncodedPath_ValidArray_Integer_Exploded(t *testing.T) {
859 spec := `openapi: 3.1.0
860 paths:
861 /burgers/{.burgerId}/locate:
862 parameters:
863 - name: burgerId
864 in: path
865 style: label
866 explode: true
867 schema:
868 type: array
869 items:
870 type: integer
871 get:
872 operationId: locateBurgers`
873
874 doc, _ := libopenapi.NewDocument([]byte(spec))
875
876 m, _ := doc.BuildV3Model()
877
878 v := NewParameterValidator(&m.Model)
879
880 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3.4/locate", nil)
881 valid, errors := v.ValidatePathParams(request)
882
883 assert.True(t, valid)
884 assert.Len(t, errors, 0)
885 }
886
887 func TestNewValidator_LabelEncodedPath_InvalidArray_Integer_Exploded(t *testing.T) {
888 spec := `openapi: 3.1.0
889 paths:
890 /burgers/{.burgerId}/locate:
891 parameters:
892 - name: burgerId
893 in: path
894 style: label
895 explode: true
896 schema:
897 type: array
898 items:
899 type: integer
900 get:
901 operationId: locateBurgers`
902
903 doc, _ := libopenapi.NewDocument([]byte(spec))
904
905 m, _ := doc.BuildV3Model()
906
907 v := NewParameterValidator(&m.Model)
908
909 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3.Not an_integer.5.6/locate", nil)
910 valid, errors := v.ValidatePathParams(request)
911
912 assert.False(t, valid)
913 assert.Len(t, errors, 1)
914 assert.Equal(t, "Path array parameter 'burgerId' is not a valid integer", errors[0].Message)
915 }
916
917 func TestNewValidator_LabelEncodedPath_InvalidArray_Integer(t *testing.T) {
918 spec := `openapi: 3.1.0
919 paths:
920 /burgers/{.burgerId}/locate:
921 parameters:
922 - name: burgerId
923 in: path
924 style: label
925 schema:
926 type: array
927 items:
928 type: integer
929 get:
930 operationId: locateBurgers`
931
932 doc, _ := libopenapi.NewDocument([]byte(spec))
933
934 m, _ := doc.BuildV3Model()
935
936 v := NewParameterValidator(&m.Model)
937
938 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3,4,Not an_integer,6/locate", nil)
939 valid, errors := v.ValidatePathParams(request)
940
941 assert.False(t, valid)
942 assert.Len(t, errors, 1)
943 assert.Equal(t, "Path array parameter 'burgerId' is not a valid integer", errors[0].Message)
944 }
945
946 func TestNewValidator_LabelEncodedPath_ValidArray_Number(t *testing.T) {
947 spec := `openapi: 3.1.0
948 paths:
949 /burgers/{.burgerId}/locate:
950 parameters:
951 - name: burgerId
952 in: path
953 style: label
954 schema:
955 type: array
956 items:
957 type: number
958 get:
959 operationId: locateBurgers`
960
961 doc, _ := libopenapi.NewDocument([]byte(spec))
962
963 m, _ := doc.BuildV3Model()
964
965 v := NewParameterValidator(&m.Model)
966
967 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3.4,5.6/locate", nil)
968 valid, errors := v.ValidatePathParams(request)
969
970 assert.True(t, valid)
971 assert.Len(t, errors, 0)
972 }
973
974 func TestNewValidator_LabelEncodedPath_ValidArray_Number_Exploded(t *testing.T) {
975 spec := `openapi: 3.1.0
976 paths:
977 /burgers/{.burgerId}/locate:
978 parameters:
979 - name: burgerId
980 in: path
981 style: label
982 explode: true
983 schema:
984 type: array
985 items:
986 type: number
987 get:
988 operationId: locateBurgers`
989
990 doc, _ := libopenapi.NewDocument([]byte(spec))
991
992 m, _ := doc.BuildV3Model()
993
994 v := NewParameterValidator(&m.Model)
995
996 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3.4.5.6/locate", nil)
997 valid, errors := v.ValidatePathParams(request)
998
999 assert.True(t, valid)
1000 assert.Len(t, errors, 0)
1001 }
1002
1003 func TestNewValidator_LabelEncodedPath_InvalidArray_Number_Exploded(t *testing.T) {
1004 spec := `openapi: 3.1.0
1005 paths:
1006 /burgers/{.burgerId}/locate:
1007 parameters:
1008 - name: burgerId
1009 in: path
1010 style: label
1011 explode: true
1012 schema:
1013 type: array
1014 items:
1015 type: number
1016 get:
1017 operationId: locateBurgers`
1018
1019 doc, _ := libopenapi.NewDocument([]byte(spec))
1020
1021 m, _ := doc.BuildV3Model()
1022
1023 v := NewParameterValidator(&m.Model)
1024
1025 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3.Not a number.5.6/locate", nil)
1026 valid, errors := v.ValidatePathParams(request)
1027
1028 assert.False(t, valid)
1029 assert.Len(t, errors, 1)
1030 assert.Equal(t, "Path array parameter 'burgerId' is not a valid number", errors[0].Message)
1031 }
1032
1033 func TestNewValidator_LabelEncodedPath_InvalidArray_Number(t *testing.T) {
1034 spec := `openapi: 3.1.0
1035 paths:
1036 /burgers/{.burgerId}/locate:
1037 parameters:
1038 - name: burgerId
1039 in: path
1040 style: label
1041 schema:
1042 type: array
1043 items:
1044 type: number
1045 get:
1046 operationId: locateBurgers`
1047
1048 doc, _ := libopenapi.NewDocument([]byte(spec))
1049
1050 m, _ := doc.BuildV3Model()
1051
1052 v := NewParameterValidator(&m.Model)
1053
1054 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.3,4,Not a number,6/locate", nil)
1055 valid, errors := v.ValidatePathParams(request)
1056
1057 assert.False(t, valid)
1058 assert.Len(t, errors, 1)
1059 assert.Equal(t, "Path array parameter 'burgerId' is not a valid number", errors[0].Message)
1060 }
1061
1062 func TestNewValidator_LabelEncodedPath_InvalidObject(t *testing.T) {
1063 spec := `openapi: 3.1.0
1064 paths:
1065 /burgers/{.burgerId}/locate:
1066 parameters:
1067 - name: burgerId
1068 in: path
1069 style: label
1070 schema:
1071 type: object
1072 properties:
1073 id:
1074 type: integer
1075 vegetarian:
1076 type: boolean
1077 get:
1078 operationId: locateBurgers`
1079
1080 doc, _ := libopenapi.NewDocument([]byte(spec))
1081
1082 m, _ := doc.BuildV3Model()
1083
1084 v := NewParameterValidator(&m.Model)
1085
1086 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.id,hello,vegetarian,why/locate", nil)
1087 valid, errors := v.ValidatePathParams(request)
1088
1089 assert.False(t, valid)
1090 assert.Len(t, errors, 1)
1091 assert.Len(t, errors[0].SchemaValidationErrors, 2)
1092 }
1093
1094 func TestNewValidator_LabelEncodedPath_InvalidObject_Exploded(t *testing.T) {
1095 spec := `openapi: 3.1.0
1096 paths:
1097 /burgers/{.burgerId}/locate:
1098 parameters:
1099 - name: burgerId
1100 in: path
1101 style: label
1102 explode: true
1103 schema:
1104 type: object
1105 properties:
1106 id:
1107 type: integer
1108 vegetarian:
1109 type: boolean
1110 get:
1111 operationId: locateBurgers`
1112
1113 doc, _ := libopenapi.NewDocument([]byte(spec))
1114
1115 m, _ := doc.BuildV3Model()
1116
1117 v := NewParameterValidator(&m.Model)
1118
1119 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.id=hello.vegetarian=why/locate", nil)
1120 valid, errors := v.ValidatePathParams(request)
1121
1122 assert.False(t, valid)
1123 assert.Len(t, errors, 1)
1124 assert.Len(t, errors[0].SchemaValidationErrors, 2)
1125 }
1126
1127 func TestNewValidator_LabelEncodedPath_ValidMultiParam(t *testing.T) {
1128 spec := `openapi: 3.1.0
1129 paths:
1130 /burgers/{.burgerId}/locate/{.query}:
1131 parameters:
1132 - name: query
1133 in: path
1134 style: label
1135 schema:
1136 type: string
1137 - name: burgerId
1138 in: path
1139 style: label
1140 explode: true
1141 schema:
1142 type: object
1143 properties:
1144 id:
1145 type: integer
1146 vegetarian:
1147 type: boolean
1148 get:
1149 operationId: locateBurgers`
1150
1151 doc, _ := libopenapi.NewDocument([]byte(spec))
1152
1153 m, _ := doc.BuildV3Model()
1154
1155 v := NewParameterValidator(&m.Model)
1156
1157 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.id=1234.vegetarian=true/locate/bigMac", nil)
1158 valid, errors := v.ValidatePathParams(request)
1159
1160 assert.True(t, valid)
1161 assert.Len(t, errors, 0)
1162 }
1163
1164 func TestNewValidator_LabelEncodedPath_InvalidMultiParam(t *testing.T) {
1165 spec := `openapi: 3.1.0
1166 paths:
1167 /burgers/{.burgerId}/locate/{.query}:
1168 parameters:
1169 - name: query
1170 in: path
1171 style: label
1172 schema:
1173 type: integer
1174 - name: burgerId
1175 in: path
1176 style: label
1177 explode: true
1178 schema:
1179 type: object
1180 properties:
1181 id:
1182 type: integer
1183 vegetarian:
1184 type: boolean
1185 get:
1186 operationId: locateBurgers`
1187
1188 doc, _ := libopenapi.NewDocument([]byte(spec))
1189
1190 m, _ := doc.BuildV3Model()
1191
1192 v := NewParameterValidator(&m.Model)
1193
1194 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.id=1234.vegetarian=true/locate/bigMac", nil)
1195 valid, errors := v.ValidatePathParams(request)
1196
1197 assert.False(t, valid)
1198 assert.Len(t, errors, 1)
1199 }
1200
1201 func TestNewValidator_MatrixEncodedPath_ValidInteger(t *testing.T) {
1202 spec := `openapi: 3.1.0
1203 paths:
1204 /burgers/{;burgerId}/locate:
1205 parameters:
1206 - name: burgerId
1207 in: path
1208 style: matrix
1209 schema:
1210 type: integer
1211 get:
1212 operationId: locateBurgers`
1213
1214 doc, _ := libopenapi.NewDocument([]byte(spec))
1215
1216 m, _ := doc.BuildV3Model()
1217
1218 v := NewParameterValidator(&m.Model)
1219
1220 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=5/locate", nil)
1221 valid, errors := v.ValidatePathParams(request)
1222
1223 assert.True(t, valid)
1224 assert.Len(t, errors, 0)
1225 }
1226
1227 func TestNewValidator_MatrixEncodedPath_InvalidInteger(t *testing.T) {
1228 spec := `openapi: 3.1.0
1229 paths:
1230 /burgers/{;burgerId}/locate:
1231 parameters:
1232 - name: burgerId
1233 in: path
1234 style: matrix
1235 schema:
1236 type: integer
1237 get:
1238 operationId: locateBurgers`
1239
1240 doc, _ := libopenapi.NewDocument([]byte(spec))
1241
1242 m, _ := doc.BuildV3Model()
1243
1244 v := NewParameterValidator(&m.Model)
1245
1246 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=I am not a number/locate", nil)
1247 valid, errors := v.ValidatePathParams(request)
1248
1249 assert.False(t, valid)
1250 assert.Len(t, errors, 1)
1251 assert.Equal(t, "Path parameter 'burgerId' is not a valid integer", errors[0].Message)
1252 }
1253
1254 func TestNewValidator_MatrixEncodedPath_MinimumIntegerViolation(t *testing.T) {
1255 spec := `openapi: 3.1.0
1256 paths:
1257 /burgers/{;burgerId}/locate:
1258 parameters:
1259 - name: burgerId
1260 in: path
1261 style: matrix
1262 schema:
1263 type: integer
1264 minimum: 5
1265 get:
1266 operationId: locateBurgers`
1267
1268 doc, _ := libopenapi.NewDocument([]byte(spec))
1269
1270 m, _ := doc.BuildV3Model()
1271
1272 v := NewParameterValidator(&m.Model)
1273
1274 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=3/locate", nil)
1275 valid, errors := v.ValidatePathParams(request)
1276
1277 assert.False(t, valid)
1278 assert.Len(t, errors, 1)
1279 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
1280 assert.Len(t, errors[0].SchemaValidationErrors, 1)
1281 assert.Equal(t, "Reason: minimum: got 3, want 5", errors[0].SchemaValidationErrors[0].Error())
1282 }
1283
1284 func TestNewValidator_MatrixEncodedPath_MaximumIntegerViolation(t *testing.T) {
1285 spec := `openapi: 3.1.0
1286 paths:
1287 /burgers/{;burgerId}/locate:
1288 parameters:
1289 - name: burgerId
1290 in: path
1291 style: matrix
1292 schema:
1293 type: integer
1294 maximum: 5
1295 get:
1296 operationId: locateBurgers`
1297
1298 doc, _ := libopenapi.NewDocument([]byte(spec))
1299
1300 m, _ := doc.BuildV3Model()
1301
1302 v := NewParameterValidator(&m.Model)
1303
1304 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=30/locate", nil)
1305 valid, errors := v.ValidatePathParams(request)
1306
1307 assert.False(t, valid)
1308 assert.Len(t, errors, 1)
1309 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
1310 assert.Len(t, errors[0].SchemaValidationErrors, 1)
1311 assert.Equal(t, "Reason: maximum: got 30, want 5", errors[0].SchemaValidationErrors[0].Error())
1312 }
1313
1314 func TestNewValidator_MatrixEncodedPath_InvalidNumber(t *testing.T) {
1315 spec := `openapi: 3.1.0
1316 paths:
1317 /burgers/{;burgerId}/locate:
1318 parameters:
1319 - name: burgerId
1320 in: path
1321 style: matrix
1322 schema:
1323 type: number
1324 get:
1325 operationId: locateBurgers`
1326
1327 doc, _ := libopenapi.NewDocument([]byte(spec))
1328
1329 m, _ := doc.BuildV3Model()
1330
1331 v := NewParameterValidator(&m.Model)
1332
1333 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=I am not a number/locate", nil)
1334 valid, errors := v.ValidatePathParams(request)
1335
1336 assert.False(t, valid)
1337 assert.Len(t, errors, 1)
1338 assert.Equal(t, "Path parameter 'burgerId' is not a valid number", errors[0].Message)
1339 }
1340
1341 func TestNewValidator_MatrixEncodedPath_MinimumNumberViolation(t *testing.T) {
1342 spec := `openapi: 3.1.0
1343 paths:
1344 /burgers/{;burgerId}/locate:
1345 parameters:
1346 - name: burgerId
1347 in: path
1348 style: matrix
1349 schema:
1350 type: number
1351 minimum: 5
1352 get:
1353 operationId: locateBurgers`
1354
1355 doc, _ := libopenapi.NewDocument([]byte(spec))
1356
1357 m, _ := doc.BuildV3Model()
1358
1359 v := NewParameterValidator(&m.Model)
1360
1361 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=3/locate", nil)
1362 valid, errors := v.ValidatePathParams(request)
1363
1364 assert.False(t, valid)
1365 assert.Len(t, errors, 1)
1366 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
1367 assert.Len(t, errors[0].SchemaValidationErrors, 1)
1368 assert.Equal(t, "Reason: minimum: got 3, want 5", errors[0].SchemaValidationErrors[0].Error())
1369 }
1370
1371 func TestNewValidator_MatrixEncodedPath_MaximumNumberViolation(t *testing.T) {
1372 spec := `openapi: 3.1.0
1373 paths:
1374 /burgers/{;burgerId}/locate:
1375 parameters:
1376 - name: burgerId
1377 in: path
1378 style: matrix
1379 schema:
1380 type: number
1381 maximum: 5
1382 get:
1383 operationId: locateBurgers`
1384
1385 doc, _ := libopenapi.NewDocument([]byte(spec))
1386
1387 m, _ := doc.BuildV3Model()
1388
1389 v := NewParameterValidator(&m.Model)
1390
1391 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=30/locate", nil)
1392 valid, errors := v.ValidatePathParams(request)
1393
1394 assert.False(t, valid)
1395 assert.Len(t, errors, 1)
1396 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
1397 assert.Len(t, errors[0].SchemaValidationErrors, 1)
1398 assert.Equal(t, "Reason: maximum: got 30, want 5", errors[0].SchemaValidationErrors[0].Error())
1399 }
1400
1401 func TestNewValidator_MatrixEncodedPath_ValidPrimitiveBoolean(t *testing.T) {
1402 spec := `openapi: 3.1.0
1403 paths:
1404 /burgers/{;burgerId}/locate:
1405 parameters:
1406 - name: burgerId
1407 in: path
1408 style: matrix
1409 schema:
1410 type: boolean
1411 get:
1412 operationId: locateBurgers`
1413
1414 doc, _ := libopenapi.NewDocument([]byte(spec))
1415
1416 m, _ := doc.BuildV3Model()
1417
1418 v := NewParameterValidator(&m.Model)
1419
1420 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=false/locate", nil)
1421 valid, errors := v.ValidatePathParams(request)
1422
1423 assert.True(t, valid)
1424 assert.Len(t, errors, 0)
1425 }
1426
1427 func TestNewValidator_MatrixEncodedPath_InvalidPrimitiveBoolean(t *testing.T) {
1428 spec := `openapi: 3.1.0
1429 paths:
1430 /burgers/{;burgerId}/locate:
1431 parameters:
1432 - name: burgerId
1433 in: path
1434 style: matrix
1435 schema:
1436 type: boolean
1437 get:
1438 operationId: locateBurgers`
1439
1440 doc, _ := libopenapi.NewDocument([]byte(spec))
1441
1442 m, _ := doc.BuildV3Model()
1443
1444 v := NewParameterValidator(&m.Model)
1445
1446 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=I am also not a bool/locate", nil)
1447 valid, errors := v.ValidatePathParams(request)
1448
1449 assert.False(t, valid)
1450 assert.Len(t, errors, 1)
1451 assert.Equal(t, "Path parameter 'burgerId' is not a valid boolean", errors[0].Message)
1452 }
1453
1454 func TestNewValidator_MatrixEncodedPath_ValidObject(t *testing.T) {
1455 spec := `openapi: 3.1.0
1456 paths:
1457 /burgers/{;burger}/locate:
1458 parameters:
1459 - name: burger
1460 in: path
1461 style: matrix
1462 schema:
1463 type: object
1464 properties:
1465 id:
1466 type: integer
1467 vegetarian:
1468 type: boolean
1469 get:
1470 operationId: locateBurgers`
1471
1472 doc, _ := libopenapi.NewDocument([]byte(spec))
1473
1474 m, _ := doc.BuildV3Model()
1475
1476 v := NewParameterValidator(&m.Model)
1477
1478 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burger=id,1234,vegetarian,false/locate", nil)
1479 valid, errors := v.ValidatePathParams(request)
1480
1481 assert.True(t, valid)
1482 assert.Len(t, errors, 0)
1483 }
1484
1485 func TestNewValidator_MatrixEncodedPath_InvalidObject(t *testing.T) {
1486 spec := `openapi: 3.1.0
1487 paths:
1488 /burgers/{;burger}/locate:
1489 parameters:
1490 - name: burger
1491 in: path
1492 style: matrix
1493 schema:
1494 type: object
1495 properties:
1496 id:
1497 type: integer
1498 vegetarian:
1499 type: boolean
1500 get:
1501 operationId: locateBurgers`
1502
1503 doc, _ := libopenapi.NewDocument([]byte(spec))
1504
1505 m, _ := doc.BuildV3Model()
1506
1507 v := NewParameterValidator(&m.Model)
1508
1509 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burger=id,1234,vegetarian,I am not a bool/locate", nil)
1510 valid, errors := v.ValidatePathParams(request)
1511
1512 assert.False(t, valid)
1513 assert.Len(t, errors, 1)
1514 assert.Equal(t, "got string, want boolean", errors[0].SchemaValidationErrors[0].Reason)
1515 }
1516
1517 func TestNewValidator_MatrixEncodedPath_ValidObject_Exploded(t *testing.T) {
1518 spec := `openapi: 3.1.0
1519 paths:
1520 /burgers/{;burger*}/locate:
1521 parameters:
1522 - name: burger
1523 in: path
1524 style: matrix
1525 explode: true
1526 schema:
1527 type: object
1528 properties:
1529 id:
1530 type: integer
1531 vegetarian:
1532 type: boolean
1533 get:
1534 operationId: locateBurgers`
1535
1536 doc, _ := libopenapi.NewDocument([]byte(spec))
1537
1538 m, _ := doc.BuildV3Model()
1539 v := NewParameterValidator(&m.Model)
1540
1541 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;id=1234;vegetarian=false/locate", nil)
1542 valid, errors := v.ValidatePathParams(request)
1543
1544 assert.True(t, valid)
1545 assert.Len(t, errors, 0)
1546 }
1547
1548 func TestNewValidator_MatrixEncodedPath_InvalidObject_Exploded(t *testing.T) {
1549 spec := `openapi: 3.1.0
1550 paths:
1551 /burgers/{;burger*}/locate:
1552 parameters:
1553 - name: burger
1554 in: path
1555 style: matrix
1556 explode: true
1557 schema:
1558 type: object
1559 properties:
1560 id:
1561 type: integer
1562 vegetarian:
1563 type: boolean
1564 get:
1565 operationId: locateBurgers`
1566
1567 doc, _ := libopenapi.NewDocument([]byte(spec))
1568
1569 m, _ := doc.BuildV3Model()
1570 v := NewParameterValidator(&m.Model)
1571
1572 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;id=1234;vegetarian=I am not a boolean/locate", nil)
1573 valid, errors := v.ValidatePathParams(request)
1574
1575 assert.False(t, valid)
1576 assert.Len(t, errors, 1)
1577 assert.Equal(t, "got string, want boolean", errors[0].SchemaValidationErrors[0].Reason)
1578 }
1579
1580 func TestNewValidator_MatrixEncodedPath_ValidArray(t *testing.T) {
1581 spec := `openapi: 3.1.0
1582 paths:
1583 /burgers/{;burger*}/locate:
1584 parameters:
1585 - name: burger
1586 in: path
1587 style: matrix
1588 schema:
1589 type: array
1590 items:
1591 type: integer
1592 get:
1593 operationId: locateBurgers`
1594
1595 doc, _ := libopenapi.NewDocument([]byte(spec))
1596
1597 m, _ := doc.BuildV3Model()
1598 v := NewParameterValidator(&m.Model)
1599
1600 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burger=1,2,3,4,5/locate", nil)
1601 valid, errors := v.ValidatePathParams(request)
1602
1603 assert.True(t, valid)
1604 assert.Len(t, errors, 0)
1605 }
1606
1607 func TestNewValidator_MatrixEncodedPath_InvalidArray(t *testing.T) {
1608 spec := `openapi: 3.1.0
1609 paths:
1610 /burgers/{;burger*}/locate:
1611 parameters:
1612 - name: burger
1613 in: path
1614 style: matrix
1615 schema:
1616 type: array
1617 items:
1618 type: integer
1619 get:
1620 operationId: locateBurgers`
1621
1622 doc, _ := libopenapi.NewDocument([]byte(spec))
1623
1624 m, _ := doc.BuildV3Model()
1625 v := NewParameterValidator(&m.Model)
1626
1627 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burger=1,2,not a number,4,false/locate", nil)
1628 valid, errors := v.ValidatePathParams(request)
1629
1630 assert.False(t, valid)
1631 assert.Len(t, errors, 2)
1632 }
1633
1634 func TestNewValidator_MatrixEncodedPath_ValidArray_Exploded(t *testing.T) {
1635 spec := `openapi: 3.1.0
1636 paths:
1637 /burgers/{;burger*}/locate:
1638 parameters:
1639 - name: burger
1640 in: path
1641 style: matrix
1642 explode: true
1643 schema:
1644 type: array
1645 items:
1646 type: integer
1647 get:
1648 operationId: locateBurgers`
1649
1650 doc, _ := libopenapi.NewDocument([]byte(spec))
1651
1652 m, _ := doc.BuildV3Model()
1653 v := NewParameterValidator(&m.Model)
1654
1655 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burger=1;burger=2;burger=3/locate", nil)
1656 valid, errors := v.ValidatePathParams(request)
1657
1658 assert.True(t, valid)
1659 assert.Len(t, errors, 0)
1660 }
1661
1662 func TestNewValidator_MatrixEncodedPath_InvalidArray_Exploded(t *testing.T) {
1663 spec := `openapi: 3.1.0
1664 paths:
1665 /burgers/{;burger*}/locate:
1666 parameters:
1667 - name: burger
1668 in: path
1669 style: matrix
1670 explode: true
1671 schema:
1672 type: array
1673 items:
1674 type: integer
1675 get:
1676 operationId: locateBurgers`
1677
1678 doc, _ := libopenapi.NewDocument([]byte(spec))
1679
1680 m, _ := doc.BuildV3Model()
1681 v := NewParameterValidator(&m.Model)
1682
1683 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burger=1;burger=I am not an int;burger=3/locate", nil)
1684 valid, errors := v.ValidatePathParams(request)
1685
1686 assert.False(t, valid)
1687 assert.Len(t, errors, 1)
1688 assert.Equal(t, "Path array parameter 'burger' is not a valid integer", errors[0].Message)
1689 }
1690
1691 func TestNewValidator_PathParams_PathNotFound(t *testing.T) {
1692 spec := `openapi: 3.1.0
1693 paths:
1694 /burgers/{;burger*}/locate:
1695 parameters:
1696 - name: burger
1697 in: path
1698 style: matrix
1699 explode: true
1700 schema:
1701 type: array
1702 items:
1703 type: integer
1704 get:
1705 operationId: locateBurgers`
1706
1707 doc, _ := libopenapi.NewDocument([]byte(spec))
1708
1709 m, _ := doc.BuildV3Model()
1710 v := NewParameterValidator(&m.Model)
1711
1712 request, _ := http.NewRequest(http.MethodGet, "https://things.com/I do not exist", nil)
1713 valid, errors := v.ValidatePathParams(request)
1714
1715 assert.False(t, valid)
1716 assert.Len(t, errors, 1)
1717 }
1718
1719 func TestNewValidator_PathParamStringEnumValid(t *testing.T) {
1720 spec := `openapi: 3.1.0
1721 paths:
1722 /burgers/{burgerId}/locate:
1723 parameters:
1724 - name: burgerId
1725 in: path
1726 schema:
1727 type: string
1728 enum: [bigMac, whopper, mcCrispy]
1729 get:
1730 operationId: locateBurgers`
1731
1732 doc, _ := libopenapi.NewDocument([]byte(spec))
1733
1734 m, _ := doc.BuildV3Model()
1735
1736 v := NewParameterValidator(&m.Model)
1737
1738 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/bigMac/locate", nil)
1739 valid, errors := v.ValidatePathParams(request)
1740
1741 assert.True(t, valid)
1742 assert.Len(t, errors, 0)
1743 }
1744
1745 func TestNewValidator_PathParamStringEnumInvalid(t *testing.T) {
1746 spec := `openapi: 3.1.0
1747 paths:
1748 /burgers/{burgerId}/locate:
1749 parameters:
1750 - name: burgerId
1751 in: path
1752 schema:
1753 type: string
1754 enum: [bigMac, whopper, mcCrispy]
1755 get:
1756 operationId: locateBurgers`
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/burgers/hello/locate", nil)
1765 valid, errors := v.ValidatePathParams(request)
1766
1767 assert.False(t, valid)
1768 assert.Len(t, errors, 1)
1769 assert.Equal(t, "Path parameter 'burgerId' does not match allowed values", errors[0].Message)
1770 assert.Equal(t, "Instead of 'hello', use one of the allowed values: 'bigMac, whopper, mcCrispy'", errors[0].HowToFix)
1771 }
1772
1773 func TestNewValidator_PathParamStringMinLengthViolation(t *testing.T) {
1774 spec := `openapi: 3.1.0
1775 paths:
1776 /burgers/{burgerId}/locate:
1777 parameters:
1778 - name: burgerId
1779 in: path
1780 schema:
1781 type: string
1782 minLength: 4
1783 get:
1784 operationId: locateBurgers`
1785
1786 doc, _ := libopenapi.NewDocument([]byte(spec))
1787
1788 m, _ := doc.BuildV3Model()
1789
1790 v := NewParameterValidator(&m.Model)
1791
1792 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/big/locate", nil)
1793 valid, errors := v.ValidatePathParams(request)
1794
1795 assert.False(t, valid)
1796 assert.Len(t, errors, 1)
1797 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
1798 assert.Len(t, errors[0].SchemaValidationErrors, 1)
1799 assert.Equal(t, "Reason: minLength: got 3, want 4", errors[0].SchemaValidationErrors[0].Error())
1800 }
1801
1802 func TestNewValidator_PathParamStringMaxLengthViolation(t *testing.T) {
1803 spec := `openapi: 3.1.0
1804 paths:
1805 /burgers/{burgerId}/locate:
1806 parameters:
1807 - name: burgerId
1808 in: path
1809 schema:
1810 type: string
1811 maxLength: 1
1812 get:
1813 operationId: locateBurgers`
1814
1815 doc, _ := libopenapi.NewDocument([]byte(spec))
1816
1817 m, _ := doc.BuildV3Model()
1818
1819 v := NewParameterValidator(&m.Model)
1820
1821 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/big/locate", nil)
1822 valid, errors := v.ValidatePathParams(request)
1823
1824 assert.False(t, valid)
1825 assert.Len(t, errors, 1)
1826 assert.Equal(t, "Path parameter 'burgerId' failed to validate", errors[0].Message)
1827 assert.Len(t, errors[0].SchemaValidationErrors, 1)
1828 assert.Equal(t, "Reason: maxLength: got 3, want 1", errors[0].SchemaValidationErrors[0].Error())
1829 }
1830
1831 func TestNewValidator_PathParamIntegerEnumValid(t *testing.T) {
1832 spec := `openapi: 3.1.0
1833 paths:
1834 /burgers/{burgerId}/locate:
1835 parameters:
1836 - name: burgerId
1837 in: path
1838 schema:
1839 type: integer
1840 enum: [1,2,99,100]
1841 get:
1842 operationId: locateBurgers`
1843
1844 doc, _ := libopenapi.NewDocument([]byte(spec))
1845
1846 m, _ := doc.BuildV3Model()
1847
1848 v := NewParameterValidator(&m.Model)
1849
1850 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/2/locate", nil)
1851 valid, errors := v.ValidatePathParams(request)
1852
1853 assert.True(t, valid)
1854 assert.Len(t, errors, 0)
1855 }
1856
1857 func TestNewValidator_PathParamIntegerEnumInvalid(t *testing.T) {
1858 spec := `openapi: 3.1.0
1859 paths:
1860 /burgers/{burgerId}/locate:
1861 parameters:
1862 - name: burgerId
1863 in: path
1864 schema:
1865 type: integer
1866 enum: [1,2,99,100]
1867 get:
1868 operationId: locateBurgers`
1869
1870 doc, _ := libopenapi.NewDocument([]byte(spec))
1871
1872 m, _ := doc.BuildV3Model()
1873
1874 v := NewParameterValidator(&m.Model)
1875
1876 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/3284/locate", nil)
1877 valid, errors := v.ValidatePathParams(request)
1878
1879 assert.False(t, valid)
1880 assert.Len(t, errors, 1)
1881 assert.Equal(t, "Path parameter 'burgerId' does not match allowed values", errors[0].Message)
1882 }
1883
1884 func TestNewValidator_PathParamNumberEnumValid(t *testing.T) {
1885 spec := `openapi: 3.1.0
1886 paths:
1887 /burgers/{burgerId}/locate:
1888 parameters:
1889 - name: burgerId
1890 in: path
1891 schema:
1892 type: number
1893 enum: [1,2,99,100]
1894 get:
1895 operationId: locateBurgers`
1896
1897 doc, _ := libopenapi.NewDocument([]byte(spec))
1898
1899 m, _ := doc.BuildV3Model()
1900
1901 v := NewParameterValidator(&m.Model)
1902
1903 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/2/locate", nil)
1904 valid, errors := v.ValidatePathParams(request)
1905
1906 assert.True(t, valid)
1907 assert.Len(t, errors, 0)
1908 }
1909
1910 func TestNewValidator_PathParamNumberEnumInvalid(t *testing.T) {
1911 spec := `openapi: 3.1.0
1912 paths:
1913 /burgers/{burgerId}/locate:
1914 parameters:
1915 - name: burgerId
1916 in: path
1917 schema:
1918 type: number
1919 enum: [1,2,99,100]
1920 get:
1921 operationId: locateBurgers`
1922
1923 doc, _ := libopenapi.NewDocument([]byte(spec))
1924
1925 m, _ := doc.BuildV3Model()
1926
1927 v := NewParameterValidator(&m.Model)
1928
1929 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/3284/locate", nil)
1930 valid, errors := v.ValidatePathParams(request)
1931
1932 assert.False(t, valid)
1933 assert.Len(t, errors, 1)
1934 assert.Equal(t, "Path parameter 'burgerId' does not match allowed values", errors[0].Message)
1935 }
1936
1937 func TestNewValidator_PathLabelEumValid(t *testing.T) {
1938 spec := `openapi: 3.1.0
1939 paths:
1940 /burgers/{.burgerId}/locate:
1941 parameters:
1942 - name: burgerId
1943 in: path
1944 style: label
1945 schema:
1946 type: number
1947 enum: [1,2,99,100]
1948 get:
1949 operationId: locateBurgers`
1950
1951 doc, _ := libopenapi.NewDocument([]byte(spec))
1952
1953 m, _ := doc.BuildV3Model()
1954
1955 v := NewParameterValidator(&m.Model)
1956
1957 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.2/locate", nil)
1958 valid, errors := v.ValidatePathParams(request)
1959
1960 assert.True(t, valid)
1961 assert.Len(t, errors, 0)
1962 }
1963
1964 func TestNewValidator_PathLabelEumInvalid(t *testing.T) {
1965 spec := `openapi: 3.1.0
1966 paths:
1967 /burgers/{.burgerId}/locate:
1968 parameters:
1969 - name: burgerId
1970 in: path
1971 style: label
1972 schema:
1973 type: number
1974 enum: [1,2,99,100]
1975 get:
1976 operationId: locateBurgers`
1977
1978 doc, _ := libopenapi.NewDocument([]byte(spec))
1979
1980 m, _ := doc.BuildV3Model()
1981
1982 v := NewParameterValidator(&m.Model)
1983
1984 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/.22334/locate", nil)
1985 valid, errors := v.ValidatePathParams(request)
1986
1987 assert.False(t, valid)
1988 assert.Len(t, errors, 1)
1989 assert.Equal(t, "Path parameter 'burgerId' does not match allowed values", errors[0].Message)
1990 assert.Equal(t, "Instead of '22334', use one of the allowed values: '1, 2, 99, 100'", errors[0].HowToFix)
1991 }
1992
1993 func TestNewValidator_PathMatrixEumInvalid(t *testing.T) {
1994 spec := `openapi: 3.1.0
1995 paths:
1996 /burgers/{;burgerId}/locate:
1997 parameters:
1998 - name: burgerId
1999 in: path
2000 style: matrix
2001 schema:
2002 type: number
2003 enum: [1,2,99,100]
2004 get:
2005 operationId: locateBurgers`
2006
2007 doc, _ := libopenapi.NewDocument([]byte(spec))
2008
2009 m, _ := doc.BuildV3Model()
2010
2011 v := NewParameterValidator(&m.Model)
2012
2013 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=22334/locate", nil)
2014 valid, errors := v.ValidatePathParams(request)
2015
2016 assert.False(t, valid)
2017 assert.Len(t, errors, 1)
2018 assert.Equal(t, "Path parameter 'burgerId' does not match allowed values", errors[0].Message)
2019 assert.Equal(t, "Instead of '22334', use one of the allowed values: '1, 2, 99, 100'", errors[0].HowToFix)
2020 }
2021
2022 func TestNewValidator_SetPathForPathParam(t *testing.T) {
2023 spec := `openapi: 3.1.0
2024 paths:
2025 /burgers/{;burgerId}/locate:
2026 parameters:
2027 - name: burgerId
2028 in: path
2029 style: matrix
2030 schema:
2031 type: number
2032 enum: [1,2,99,100]
2033 get:
2034 operationId: locateBurgers`
2035
2036 doc, _ := libopenapi.NewDocument([]byte(spec))
2037
2038 m, _ := doc.BuildV3Model()
2039
2040 v := NewParameterValidator(&m.Model)
2041
2042 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/;burgerId=22334/locate", nil)
2043
2044 // preset the path
2045 path, _, pv := paths.FindPath(request, &m.Model, nil)
2046
2047 valid, errors := v.ValidatePathParamsWithPathItem(request, path, pv)
2048
2049 assert.False(t, valid)
2050 assert.Len(t, errors, 1)
2051 assert.Equal(t, "Path parameter 'burgerId' does not match allowed values", errors[0].Message)
2052 assert.Equal(t, "Instead of '22334', use one of the allowed values: '1, 2, 99, 100'", errors[0].HowToFix)
2053 }
2054
2055 func TestNewValidator_SetPathForPathParam_notfound(t *testing.T) {
2056 spec := `openapi: 3.1.0
2057 paths:
2058 /burgers/{;burgerId}/locate:
2059 parameters:
2060 - name: burgerId
2061 in: path
2062 style: matrix
2063 schema:
2064 type: number
2065 enum: [1,2,99,100]
2066 get:
2067 operationId: locateBurgers`
2068
2069 doc, _ := libopenapi.NewDocument([]byte(spec))
2070
2071 m, _ := doc.BuildV3Model()
2072
2073 v := NewParameterValidator(&m.Model)
2074
2075 request, _ := http.NewRequest(http.MethodGet, "https://things.com/pizza/;burgerId=22334/locate", nil)
2076
2077 // preset the path
2078 path, _, pv := paths.FindPath(request, &m.Model, &config.ValidationOptions{RegexCache: &sync.Map{}})
2079
2080 valid, errors := v.ValidatePathParamsWithPathItem(request, path, pv)
2081
2082 assert.False(t, valid)
2083 assert.Len(t, errors, 1)
2084 assert.Equal(t, "GET Path '/pizza/;burgerId=22334/locate' not found", errors[0].Message)
2085 }
2086
2087 func TestNewValidator_ServerPathPrefixInRequestPath(t *testing.T) {
2088 spec := `openapi: 3.1.0
2089 servers:
2090 - url: https://api.pb33f.io/lorem/ipsum
2091 description: Live production endpoint for general use.
2092 paths:
2093 /burgers/{burger}/locate:
2094 parameters:
2095 - name: burger
2096 in: path
2097 schema:
2098 type: string
2099 format: uuid
2100 get:
2101 operationId: locateBurger`
2102
2103 doc, _ := libopenapi.NewDocument([]byte(spec))
2104
2105 m, _ := doc.BuildV3Model()
2106
2107 v := NewParameterValidator(&m.Model)
2108
2109 request, _ := http.NewRequest(http.MethodGet, "https://things.com/lorem/ipsum/burgers/d6d8d513-686c-466f-9f5a-1c051b6b4f3f/locate", nil)
2110 valid, errors := v.ValidatePathParams(request)
2111
2112 assert.True(t, valid)
2113 assert.Len(t, errors, 0)
2114 }
2115
2116 func TestNewValidator_MandatoryPathSegmentEmpty(t *testing.T) {
2117 spec := `openapi: 3.1.0
2118 servers:
2119 - url: https://api.pb33f.io
2120 description: Live production endpoint for general use.
2121 paths:
2122 /burgers/{burger}:
2123 get:
2124 parameters:
2125 - name: burger
2126 in: path
2127 required: true
2128 schema:
2129 type: string`
2130
2131 doc, _ := libopenapi.NewDocument([]byte(spec))
2132
2133 m, _ := doc.BuildV3Model()
2134
2135 v := NewParameterValidator(&m.Model)
2136
2137 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/", nil)
2138 valid, errors := v.ValidatePathParams(request)
2139
2140 assert.False(t, valid)
2141 assert.Len(t, errors, 1)
2142 }
2143
2144 func TestNewValidator_ODataFormattedOpenAPISpecs(t *testing.T) {
2145 spec := `openapi: 3.0.0
2146 paths:
2147 /entities('{Entity}'):
2148 parameters:
2149 - description: 'key: Entity'
2150 in: path
2151 name: Entity
2152 required: true
2153 schema:
2154 type: integer
2155 get:
2156 operationId: one
2157 /orders(RelationshipNumber='{RelationshipNumber}',ValidityEndDate=datetime'{ValidityEndDate}'):
2158 parameters:
2159 - name: RelationshipNumber
2160 in: path
2161 required: true
2162 schema:
2163 type: integer
2164 - name: ValidityEndDate
2165 in: path
2166 required: true
2167 schema:
2168 type: string
2169 get:
2170 operationId: one
2171 `
2172 doc, _ := libopenapi.NewDocument([]byte(spec))
2173
2174 m, _ := doc.BuildV3Model()
2175
2176 v := NewParameterValidator(&m.Model)
2177
2178 request, _ := http.NewRequest(http.MethodGet, "https://things.com/entities('1')", nil)
2179
2180 valid, errors := v.ValidatePathParams(request)
2181
2182 assert.True(t, valid)
2183 assert.Len(t, errors, 0)
2184
2185 request, _ = http.NewRequest(http.MethodGet, "https://things.com/orders(RelationshipNumber='1234',ValidityEndDate=datetime'1492041600000')", nil)
2186
2187 valid, errors = v.ValidatePathParams(request)
2188
2189 assert.True(t, valid)
2190 assert.Len(t, errors, 0)
2191
2192 request, _ = http.NewRequest(http.MethodGet, "https://things.com/orders(RelationshipNumber='dummy',ValidityEndDate=datetime'1492041600000')", nil)
2193
2194 valid, errors = v.ValidatePathParams(request)
2195 assert.False(t, valid)
2196 assert.Len(t, errors, 1)
2197 }
2198
2199 func TestNewValidator_ODataFormattedOpenAPISpecs_Error(t *testing.T) {
2200 spec := `openapi: 3.0.0
2201 paths:
2202 /entities('{Entity'):
2203 parameters:
2204 - in: path
2205 name: Entity
2206 required: true
2207 schema:
2208 type: integer
2209 get:
2210 operationId: one
2211 `
2212 doc, _ := libopenapi.NewDocument([]byte(spec))
2213
2214 m, _ := doc.BuildV3Model()
2215
2216 v := NewParameterValidator(&m.Model)
2217
2218 request, _ := http.NewRequest(http.MethodGet, "https://things.com/entities('1')", nil)
2219
2220 valid, errors := v.ValidatePathParams(request)
2221 assert.False(t, valid)
2222 assert.NotEmpty(t, errors)
2223 }
2224
2225 func TestNewValidator_ODataFormattedOpenAPISpecs_ErrorEmptyParameter(t *testing.T) {
2226 spec := `openapi: 3.0.0
2227 paths:
2228 /entities('{Entity}'):
2229 parameters:
2230 - in: path
2231 name: Entity
2232 required: true
2233 schema:
2234 type: integer
2235 get:
2236 operationId: one
2237 `
2238 doc, _ := libopenapi.NewDocument([]byte(spec))
2239
2240 m, _ := doc.BuildV3Model()
2241
2242 v := NewParameterValidator(&m.Model)
2243
2244 request, _ := http.NewRequest(http.MethodGet, "https://things.com/entities('')", nil)
2245
2246 valid, errors := v.ValidatePathParams(request)
2247 assert.False(t, valid)
2248 assert.NotEmpty(t, errors)
2249 }
2250
2251 type regexCacheWatcher struct {
2252 inner *sync.Map
2253 missCount int64
2254 hitCount int64
2255 storeCount int64
2256 }
2257
2258 func (c *regexCacheWatcher) Load(key any) (value any, ok bool) {
2259 data, found := c.inner.Load(key)
2260 if found {
2261 atomic.AddInt64(&c.hitCount, 1)
2262 } else {
2263 atomic.AddInt64(&c.missCount, 1)
2264 }
2265
2266 return data, found
2267 }
2268
2269 func (c *regexCacheWatcher) Store(key, value any) {
2270 atomic.AddInt64(&c.storeCount, 1)
2271 c.inner.Store(key, value)
2272 }
2273
2274 func TestValidatePathParamsWithPathItem_RegexCache_WithOneCached(t *testing.T) {
2275 spec := `openapi: 3.1.0
2276 paths:
2277 /burgers/{burgerId}/locate:
2278 parameters:
2279 - in: path
2280 name: burgerId
2281 schema:
2282 type: integer
2283 get:
2284 operationId: locateBurgers`
2285 doc, _ := libopenapi.NewDocument([]byte(spec))
2286 m, _ := doc.BuildV3Model()
2287
2288 cache := &regexCacheWatcher{inner: &sync.Map{}}
2289
2290 segment := "{burgerId}"
2291 r, err := helpers.GetRegexForPath(segment)
2292 require.NoError(t, err)
2293 cache.inner.Store(segment, r)
2294
2295 v := NewParameterValidator(&m.Model, config.WithRegexCache(cache))
2296
2297 request, _ := http.NewRequest(http.MethodGet, "https://things.com/burgers/123/locate", nil)
2298
2299 pathItem, _, foundPath := paths.FindPath(request, &m.Model, nil)
2300 v.ValidatePathParamsWithPathItem(request, pathItem, foundPath)
2301
2302 // Only "{burgerId}" regex was cached
2303 assert.EqualValues(t, 2, cache.storeCount) // Stores "burgers" and "locate" regex
2304 assert.EqualValues(t, 2, cache.missCount)
2305 assert.EqualValues(t, 1, cache.hitCount)
2306 }
2307
2308 // TestRadixTree_RegexFallback verifies that:
2309 // 1. Simple paths use the radix tree (no regex cache)
2310 // 2. Complex paths (OData style) fall back to regex and use the cache
2311 func TestRadixTree_RegexFallback(t *testing.T) {
2312 spec := `openapi: 3.1.0
2313 paths:
2314 /simple/{id}:
2315 get:
2316 operationId: getSimple
2317 /entities('{Entity}'):
2318 get:
2319 operationId: getOData`
2320
2321 doc, _ := libopenapi.NewDocument([]byte(spec))
2322 m, _ := doc.BuildV3Model()
2323
2324 cache := &regexCacheWatcher{inner: &sync.Map{}}
2325 opts := &config.ValidationOptions{RegexCache: cache, PathTree: radix.BuildPathTree(&m.Model)}
2326
2327 // Simple path - should NOT use regex cache (handled by radix tree)
2328 simpleRequest, _ := http.NewRequest(http.MethodGet, "https://things.com/simple/123", nil)
2329 pathItem, _, foundPath := paths.FindPath(simpleRequest, &m.Model, opts)
2330
2331 assert.NotNil(t, pathItem)
2332 assert.Equal(t, "/simple/{id}", foundPath)
2333 assert.EqualValues(t, 0, cache.storeCount, "Simple paths should not use regex cache")
2334 assert.EqualValues(t, 0, cache.hitCount+cache.missCount, "Simple paths should not touch regex cache")
2335
2336 // OData path - SHOULD use regex cache (radix tree can't handle embedded params)
2337 odataRequest, _ := http.NewRequest(http.MethodGet, "https://things.com/entities('abc')", nil)
2338 pathItem, _, foundPath = paths.FindPath(odataRequest, &m.Model, opts)
2339
2340 assert.NotNil(t, pathItem)
2341 assert.Equal(t, "/entities('{Entity}')", foundPath)
2342 assert.EqualValues(t, 1, cache.storeCount, "OData paths should use regex cache")
2343 assert.EqualValues(t, 1, cache.missCount, "First OData lookup should miss cache")
2344
2345 // Second OData call should hit cache
2346 pathItem, _, _ = paths.FindPath(odataRequest, &m.Model, opts)
2347 assert.NotNil(t, pathItem)
2348 assert.EqualValues(t, 1, cache.storeCount, "No new stores on cache hit")
2349 assert.EqualValues(t, 1, cache.hitCount, "Second OData lookup should hit cache")
2350 }