From 9c129dfc1499492f4f6675559f4da055a6f18a45 Mon Sep 17 00:00:00 2001 From: Patrick Boos Date: Wed, 1 Nov 2023 11:38:44 +0100 Subject: [PATCH] bugfix: exclude 406 status code violations --- .../getyourguide/openapi/validation/api/Rules.java | 1 + .../core/exclusions/InternalViolationExclusions.java | 6 ++++++ .../exclusions/InternalViolationExclusionsTest.java | 12 ++++++++++++ 3 files changed, 19 insertions(+) diff --git a/openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java b/openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java index b9aa85e..e2912cf 100644 --- a/openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java +++ b/openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java @@ -9,5 +9,6 @@ public static class Request { public static class Response { public static final String BODY_SCHEMA_ONE_OF = "validation.response.body.schema.oneOf"; + public static final String STATUS_UNKNOWN = "validation.response.status.unknown"; } } diff --git a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusions.java b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusions.java index 346ddf5..868d654 100644 --- a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusions.java +++ b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusions.java @@ -15,6 +15,7 @@ public boolean isExcluded(OpenApiViolation violation) { return falsePositive404(violation) || falsePositive400(violation) || falsePositive405(violation) + || falsePositive406(violation) || customViolationExclusions.isExcluded(violation) || oneOfMatchesMoreThanOneSchema(violation); } @@ -47,4 +48,9 @@ private boolean falsePositive405(OpenApiViolation violation) { return violation.getResponseStatus().orElse(0) == 405 && Rules.Request.OPERATION_NOT_ALLOWED.equals(violation.getRule()); } + + private boolean falsePositive406(OpenApiViolation violation) { + return violation.getResponseStatus().orElse(0) == 406 + && Rules.Response.STATUS_UNKNOWN.equals(violation.getRule()); + } } diff --git a/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusionsTest.java b/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusionsTest.java index 5ad91cd..48c5253 100644 --- a/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusionsTest.java +++ b/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusionsTest.java @@ -145,6 +145,18 @@ public void when405ResponseCodeWithOperationNotAllowedViolationThenViolationExcl .build()); } + @Test + public void when406ResponseCodeWithStatusUnknownViolationThenViolationExcluded() { + when(customViolationExclusions.isExcluded(any())).thenReturn(false); + + checkViolationExcluded(OpenApiViolation.builder() + .direction(Direction.RESPONSE) + .rule("validation.response.status.unknown") + .responseStatus(406) + .message("") + .build()); + } + private void checkViolationNotExcluded(OpenApiViolation violation) { var isExcluded = violationExclusions.isExcluded(violation);