Skip to content

Commit ff2e60a

Browse files
authored
bugfix: Exclude "matched 2 out of 25" in the oneOf rule (#36)
1 parent 6c22a6a commit ff2e60a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusions.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ public boolean isExcluded(OpenApiViolation violation) {
1313
return falsePositive404(violation)
1414
|| falsePositive400(violation)
1515
|| customViolationExclusions.isExcluded(violation)
16-
// If it matches more than 1, then we don't want to log a validation error
17-
|| violation.getMessage().matches(
18-
".*\\[Path '[^']+'] Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d\\).*");
16+
|| oneOfMatchesMoreThanOneSchema(violation);
17+
}
18+
19+
private static boolean oneOfMatchesMoreThanOneSchema(OpenApiViolation violation) {
20+
return (
21+
"validation.response.body.schema.oneOf".equals(violation.getRule())
22+
|| "validation.request.body.schema.oneOf".equals(violation.getRule())
23+
)
24+
&& violation.getMessage()
25+
.matches(".*Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d+\\).*");
1926
}
2027

2128
private boolean falsePositive404(OpenApiViolation violation) {

openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusionsTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,20 @@ public void testWhenInstanceFailedToMatchExactlyOneThenViolationExcluded() {
5555
when(customViolationExclusions.isExcluded(any())).thenReturn(false);
5656

5757
checkViolationExcluded(OpenApiViolation.builder()
58+
.rule("validation.response.body.schema.oneOf")
5859
.message("[Path '/v1/endpoint'] Instance failed to match exactly one schema (matched 2 out of 4)").build());
5960
}
6061

62+
@Test
63+
public void testWhenInstanceFailedToMatchExactlyOneWithOneOf24ThenViolationExcluded() {
64+
when(customViolationExclusions.isExcluded(any())).thenReturn(false);
65+
66+
checkViolationExcluded(OpenApiViolation.builder()
67+
.rule("validation.request.body.schema.oneOf")
68+
.message("[Path '/v1/endpoint'] Instance failed to match exactly one schema (matched 2 out of 24)")
69+
.build());
70+
}
71+
6172
@Test
6273
public void testWhen404ResponseWithApiPathNotSpecifiedThenViolationExcluded() {
6374
when(customViolationExclusions.isExcluded(any())).thenReturn(false);

0 commit comments

Comments
 (0)