Skip to content

Commit 33e37d9

Browse files
committed
Preserve all validation errors for allOf
Previously, only the first error would be reported
1 parent b771380 commit 33e37d9

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

openapi3/errors.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,13 @@ func (meo multiErrorForOneOf) Error() string {
5757
func (meo multiErrorForOneOf) Unwrap() error {
5858
return MultiError(meo)
5959
}
60+
61+
type multiErrorForAllOf MultiError
62+
63+
func (mea multiErrorForAllOf) Error() string {
64+
return spliceErr(" And ", mea)
65+
}
66+
67+
func (mea multiErrorForAllOf) Unwrap() error {
68+
return MultiError(mea)
69+
}

openapi3/schema.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
14271427
visitedAnyOf = true
14281428
}
14291429

1430+
validationErrors := multiErrorForAllOf{}
14301431
for _, item := range schema.AllOf {
14311432
v := item.Value
14321433
if v == nil {
@@ -1436,17 +1437,20 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
14361437
if settings.failfast {
14371438
return errSchema, false
14381439
}
1439-
return &SchemaError{
1440-
Value: value,
1441-
Schema: schema,
1442-
SchemaField: "allOf",
1443-
Reason: `doesn't match all schemas from "allOf"`,
1444-
Origin: err,
1445-
customizeMessageError: settings.customizeMessageError,
1446-
}, false
1440+
validationErrors = append(validationErrors, err)
14471441
}
14481442
visitedAllOf = true
14491443
}
1444+
if len(validationErrors) > 0 {
1445+
return &SchemaError{
1446+
Value: value,
1447+
Schema: schema,
1448+
SchemaField: "allOf",
1449+
Reason: `doesn't match all schemas from "allOf"`,
1450+
Origin: fmt.Errorf("doesn't match schema due to: %w", validationErrors),
1451+
customizeMessageError: settings.customizeMessageError,
1452+
}, false
1453+
}
14501454

14511455
run = !((visitedOneOf || visitedAnyOf || visitedAllOf) && value == nil)
14521456
return

openapi3filter/issue641_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ paths:
6969
name: "failed allof pattern",
7070
spec: allOfSpec,
7171
req: `/items?test=999999`,
72-
errStr: `parameter "test" in query has an error: string doesn't match the regular expression "^[0-9]{1,4}$"`,
72+
errStr: `parameter "test" in query has an error: doesn't match schema due to: string doesn't match the regular expression "^[0-9]{1,4}$"`,
7373
},
7474
}
7575

openapi3filter/issue789_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ paths:
6969
name: "failed allof object array",
7070
spec: allOfArraySpec,
7171
req: `/items?test=foo`,
72-
errStr: `parameter "test" in query has an error: string doesn't match the regular expression`,
72+
errStr: `parameter "test" in query has an error: doesn't match schema due to: string doesn't match the regular expression`,
7373
},
7474
{
7575
name: "success oneof string pattern match",

openapi3filter/validation_error_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,12 @@ func getValidationTests(t *testing.T) []*validationTest {
482482
wantErrSchemaPath: "/",
483483
wantErrSchemaValue: map[string]string{"name": "Bahama"},
484484
wantErrSchemaReason: `doesn't match all schemas from "allOf"`,
485-
wantErrSchemaOriginReason: `property "photoUrls" is missing`,
485+
wantErrSchemaOriginReason: `doesn't match schema due to: property "photoUrls" is missing`,
486486
wantErrSchemaOriginValue: map[string]string{"name": "Bahama"},
487487
wantErrSchemaOriginPath: "/photoUrls",
488488
wantErrResponse: &ValidationError{
489489
Status: http.StatusUnprocessableEntity,
490-
Title: `property "photoUrls" is missing`,
491-
Source: &ValidationErrorSource{Pointer: "/photoUrls"},
490+
Title: `doesn't match all schemas from "allOf"`,
492491
},
493492
},
494493
{

0 commit comments

Comments
 (0)