@@ -3,6 +3,7 @@ package openapi3filter
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "errors"
6
7
"fmt"
7
8
"io"
8
9
"net/http"
@@ -47,6 +48,9 @@ type validationTest struct {
47
48
wantErrSchemaOriginReason string
48
49
wantErrSchemaOriginPath string
49
50
wantErrSchemaOriginValue any
51
+ wantMultiErrSchemaReasons []string
52
+ wantMultiErrSchemaPaths []string
53
+ wantMultiErrSchemaValues []any
50
54
wantErrParam string
51
55
wantErrParamIn string
52
56
wantErrParseKind ParseErrorKind
@@ -478,17 +482,38 @@ func getValidationTests(t *testing.T) []*validationTest {
478
482
args : validationArgs {
479
483
r : newPetstoreRequest (t , http .MethodPost , "/pet2" , bytes .NewBufferString (`{"name":"Bahama"}` )),
480
484
},
481
- wantErrReason : "doesn't match schema" ,
482
- wantErrSchemaPath : "/" ,
483
- wantErrSchemaValue : map [string ]string {"name" : "Bahama" },
484
- wantErrSchemaReason : `doesn't match all schemas from "allOf"` ,
485
- wantErrSchemaOriginReason : `property "photoUrls" is missing` ,
486
- wantErrSchemaOriginValue : map [string ]string {"name" : "Bahama" },
487
- wantErrSchemaOriginPath : "/photoUrls" ,
485
+ wantErrReason : "doesn't match schema" ,
486
+ wantErrSchemaPath : "/" ,
487
+ wantErrSchemaValue : map [string ]string {"name" : "Bahama" },
488
+ wantErrSchemaReason : `doesn't match all schemas from "allOf"` ,
489
+ wantMultiErrSchemaPaths : []string {"/photoUrls" },
490
+ wantMultiErrSchemaValues : []any {map [string ]string {"name" : "Bahama" }},
491
+ wantMultiErrSchemaReasons : []string {
492
+ `property "photoUrls" is missing` ,
493
+ },
488
494
wantErrResponse : & ValidationError {
489
495
Status : http .StatusUnprocessableEntity ,
490
- Title : `property "photoUrls" is missing` ,
491
- Source : & ValidationErrorSource {Pointer : "/photoUrls" },
496
+ Title : `doesn't match all schemas from "allOf"` ,
497
+ },
498
+ },
499
+ {
500
+ name : "error - missing required object attribute and bad type from allOf required overlay" ,
501
+ args : validationArgs {
502
+ r : newPetstoreRequest (t , http .MethodPost , "/pet2" , bytes .NewBufferString (`{"name":1}` )),
503
+ },
504
+ wantErrReason : "doesn't match schema" ,
505
+ wantErrSchemaPath : "/" ,
506
+ wantErrSchemaValue : map [string ]float64 {"name" : 1 },
507
+ wantErrSchemaReason : `doesn't match all schemas from "allOf"` ,
508
+ wantMultiErrSchemaPaths : []string {"/name" , "/photoUrls" },
509
+ wantMultiErrSchemaValues : []any {1 , map [string ]float64 {"name" : 1 }},
510
+ wantMultiErrSchemaReasons : []string {
511
+ "value must be a string" ,
512
+ "property \" photoUrls\" is missing" ,
513
+ },
514
+ wantErrResponse : & ValidationError {
515
+ Status : http .StatusUnprocessableEntity ,
516
+ Title : `doesn't match all schemas from "allOf"` ,
492
517
},
493
518
},
494
519
{
@@ -599,6 +624,23 @@ func TestValidationHandler_validateRequest(t *testing.T) {
599
624
pointer := toJSONPointer (originErr .JSONPointer ())
600
625
req .Equal (tt .wantErrSchemaOriginPath , pointer )
601
626
req .Equal (fmt .Sprint (tt .wantErrSchemaOriginValue ), fmt .Sprint (originErr .Value ))
627
+ } else if wrapErr := errors .Unwrap (innerErr .Origin ); wrapErr != nil {
628
+ if multiErr , ok := errors .Unwrap (wrapErr ).(openapi3.MultiError ); ok {
629
+ req .Len (multiErr , len (tt .wantMultiErrSchemaReasons ))
630
+ req .Len (multiErr , len (tt .wantMultiErrSchemaPaths ))
631
+ req .Len (multiErr , len (tt .wantMultiErrSchemaValues ))
632
+ for i , merr := range multiErr {
633
+ schemaErr , ok := merr .(* openapi3.SchemaError )
634
+ if ! ok {
635
+ continue
636
+ }
637
+ req .Equal (tt .wantMultiErrSchemaReasons [i ], schemaErr .Reason )
638
+ pointer := toJSONPointer (schemaErr .JSONPointer ())
639
+ req .Equal (tt .wantMultiErrSchemaPaths [i ], pointer )
640
+ req .Equal (fmt .Sprint (tt .wantMultiErrSchemaValues [i ]), fmt .Sprint (schemaErr .Value ))
641
+
642
+ }
643
+ }
602
644
}
603
645
} else {
604
646
req .False (tt .wantErrSchemaReason != "" || tt .wantErrSchemaPath != "" ,
0 commit comments