Skip to content

Commit 48aac54

Browse files
authored
TRX-1066: fix(core): exclude accept.notAllowed violations on 204 responses (#414)
1 parent 6d8c3c7 commit 48aac54

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static class Request {
55
public static final String PATH_MISSING = "validation.request.path.missing";
66
public static final String OPERATION_NOT_ALLOWED = "validation.request.operation.notAllowed";
77
public static final String BODY_SCHEMA_ONE_OF = "validation.request.body.schema.oneOf";
8+
public static final String ACCEPT_NOT_ALLOWED = "validation.request.accept.notAllowed";
89
}
910

1011
public static class Response {

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class InternalViolationExclusions {
1212
private final ViolationExclusions customViolationExclusions;
1313

1414
public boolean isExcluded(OpenApiViolation violation) {
15-
return falsePositive404(violation)
15+
return falsePositive204(violation)
16+
|| falsePositive404(violation)
1617
|| falsePositive405(violation)
1718
|| falsePositive406(violation)
1819
|| falsePositiveRequestWith4xxResponse(violation)
@@ -31,6 +32,11 @@ private static boolean oneOfMatchesMoreThanOneSchema(OpenApiViolation violation)
3132
.matches(".*Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d+\\).*");
3233
}
3334

35+
private boolean falsePositive204(OpenApiViolation violation) {
36+
return violation.getResponseStatus().orElse(0) == 204
37+
&& Rules.Request.ACCEPT_NOT_ALLOWED.equals(violation.getRule());
38+
}
39+
3440
private boolean falsePositive404(OpenApiViolation violation) {
3541
return
3642
(

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void whenViolationThenViolationNotExcluded() {
2929
checkViolationNotExcluded(buildSimpleViolation(Direction.RESPONSE, 404));
3030
checkViolationNotExcluded(buildSimpleViolation(Direction.RESPONSE, 400));
3131
checkViolationNotExcluded(buildSimpleViolation(Direction.REQUEST, 200));
32+
checkViolationNotExcluded(buildSimpleViolation(Direction.REQUEST, 204));
3233
checkViolationNotExcluded(buildSimpleViolation(Direction.REQUEST, null));
3334
checkViolationNotExcluded(buildSimpleViolation(Direction.RESPONSE, 200));
3435
}
@@ -159,6 +160,30 @@ public void when406ResponseCodeWithStatusUnknownViolationThenViolationExcluded()
159160
.build());
160161
}
161162

163+
@Test
164+
public void when204ResponseCodeWithAcceptNotAllowedViolationThenViolationExcluded() {
165+
when(customViolationExclusions.isExcluded(any())).thenReturn(false);
166+
167+
checkViolationExcluded(buildAcceptNotAllowedViolation(204));
168+
}
169+
170+
@Test
171+
public void whenNon204ResponseCodeWithAcceptNotAllowedViolationThenViolationNotExcluded() {
172+
when(customViolationExclusions.isExcluded(any())).thenReturn(false);
173+
174+
checkViolationNotExcluded(buildAcceptNotAllowedViolation(200));
175+
}
176+
177+
private static OpenApiViolation buildAcceptNotAllowedViolation(int responseStatus) {
178+
return OpenApiViolation.builder()
179+
.direction(Direction.REQUEST)
180+
.rule("validation.request.accept.notAllowed")
181+
.responseStatus(responseStatus)
182+
.message("Request Accept header '[application/json]' does not match any defined response types. "
183+
+ "Must be one of: [application/problem+json].")
184+
.build();
185+
}
186+
162187
@Test
163188
public void whenUnknownErrorWithConcurrentModificationExceptionThenViolationExcluded() {
164189
when(customViolationExclusions.isExcluded(any())).thenReturn(false);

0 commit comments

Comments
 (0)