@@ -3,6 +3,7 @@ package openapi3filter
33import (
44 "bytes"
55 "context"
6+ "errors"
67 "fmt"
78 "io"
89 "net/http"
@@ -47,6 +48,9 @@ type validationTest struct {
4748 wantErrSchemaOriginReason string
4849 wantErrSchemaOriginPath string
4950 wantErrSchemaOriginValue any
51+ wantMultiErrSchemaReasons []string
52+ wantMultiErrSchemaPaths []string
53+ wantMultiErrSchemaValues []any
5054 wantErrParam string
5155 wantErrParamIn string
5256 wantErrParseKind ParseErrorKind
@@ -478,17 +482,38 @@ func getValidationTests(t *testing.T) []*validationTest {
478482 args : validationArgs {
479483 r : newPetstoreRequest (t , http .MethodPost , "/pet2" , bytes .NewBufferString (`{"name":"Bahama"}` )),
480484 },
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+ },
488494 wantErrResponse : & ValidationError {
489495 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"` ,
492517 },
493518 },
494519 {
@@ -599,6 +624,23 @@ func TestValidationHandler_validateRequest(t *testing.T) {
599624 pointer := toJSONPointer (originErr .JSONPointer ())
600625 req .Equal (tt .wantErrSchemaOriginPath , pointer )
601626 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+ }
602644 }
603645 } else {
604646 req .False (tt .wantErrSchemaReason != "" || tt .wantErrSchemaPath != "" ,
0 commit comments