Skip to content

Commit 265c3dc

Browse files
committed
Preserve all validation errors for allOf
Previously, only the first error would be reported
1 parent d72d75b commit 265c3dc

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
@@ -1429,6 +1429,7 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
14291429
visitedAnyOf = true
14301430
}
14311431

1432+
validationErrors := multiErrorForAllOf{}
14321433
for _, item := range schema.AllOf {
14331434
v := item.Value
14341435
if v == nil {
@@ -1438,17 +1439,20 @@ func (schema *Schema) visitXOFOperations(settings *schemaValidationSettings, val
14381439
if settings.failfast {
14391440
return errSchema, false
14401441
}
1441-
return &SchemaError{
1442-
Value: value,
1443-
Schema: schema,
1444-
SchemaField: "allOf",
1445-
Reason: `doesn't match all schemas from "allOf"`,
1446-
Origin: err,
1447-
customizeMessageError: settings.customizeMessageError,
1448-
}, false
1442+
validationErrors = append(validationErrors, err)
14491443
}
14501444
visitedAllOf = true
14511445
}
1446+
if len(validationErrors) > 0 {
1447+
return &SchemaError{
1448+
Value: value,
1449+
Schema: schema,
1450+
SchemaField: "allOf",
1451+
Reason: `doesn't match all schemas from "allOf"`,
1452+
Origin: fmt.Errorf("doesn't match schema due to: %w", validationErrors),
1453+
customizeMessageError: settings.customizeMessageError,
1454+
}, false
1455+
}
14521456

14531457
run = !((visitedOneOf || visitedAnyOf || visitedAllOf) && value == nil)
14541458
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)