From 487d0f75fd58d067e166952f4d60221088871ae5 Mon Sep 17 00:00:00 2001 From: Patrick Boos Date: Wed, 18 Oct 2023 09:08:29 +0200 Subject: [PATCH] Extract rule constants to file --- .../getyourguide/openapi/validation/api/Rules.java | 12 ++++++++++++ .../exclusions/InternalViolationExclusions.java | 13 +++++-------- 2 files changed, 17 insertions(+), 8 deletions(-) create mode 100644 openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java 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 new file mode 100644 index 0000000..4636567 --- /dev/null +++ b/openapi-validation-api/src/main/java/com/getyourguide/openapi/validation/api/Rules.java @@ -0,0 +1,12 @@ +package com.getyourguide.openapi.validation.api; + +public class Rules { + public static class Request { + public static final String PATH_MISSING = "validation.request.path.missing"; + public static final String OPERATION_NOT_ALLOWED = "validation.request.operation.notAllowed"; + public static final String BODY_SCHEMA_ONE_OF = "validation.request.body.schema.oneOf"; + } + public static class Response { + public static final String BODY_SCHEMA_ONE_OF = "validation.response.body.schema.oneOf"; + } +} 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 9178927..3bacb7c 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 @@ -1,5 +1,6 @@ package com.getyourguide.openapi.validation.core.exclusions; +import com.getyourguide.openapi.validation.api.Rules; import com.getyourguide.openapi.validation.api.exclusions.ViolationExclusions; import com.getyourguide.openapi.validation.api.model.Direction; import com.getyourguide.openapi.validation.api.model.OpenApiViolation; @@ -7,10 +8,6 @@ @AllArgsConstructor public class InternalViolationExclusions { - private static final String RULE_REQUEST_OPERATION_NOT_ALLOWED = "validation.request.operation.notAllowed"; - private static final String RULE_REQUEST_BODY_SCHEMA_ONE_OF = "validation.request.body.schema.oneOf"; - private static final String RULE_REQUEST_PATH_MISSING = "validation.request.path.missing"; - private static final String RULE_RESPONSE_BODY_SCHEMA_ONE_OF = "validation.response.body.schema.oneOf"; private final ViolationExclusions customViolationExclusions; @@ -24,15 +21,15 @@ public boolean isExcluded(OpenApiViolation violation) { private static boolean oneOfMatchesMoreThanOneSchema(OpenApiViolation violation) { return ( - RULE_RESPONSE_BODY_SCHEMA_ONE_OF.equals(violation.getRule()) - || RULE_REQUEST_BODY_SCHEMA_ONE_OF.equals(violation.getRule()) + Rules.Response.BODY_SCHEMA_ONE_OF.equals(violation.getRule()) + || Rules.Request.BODY_SCHEMA_ONE_OF.equals(violation.getRule()) ) && violation.getMessage() .matches(".*Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d+\\).*"); } private boolean falsePositive404(OpenApiViolation violation) { - return RULE_REQUEST_PATH_MISSING.equals(violation.getRule()) + return Rules.Request.PATH_MISSING.equals(violation.getRule()) && ( violation.getDirection() == Direction.REQUEST || (violation.getDirection() == Direction.RESPONSE && violation.getResponseStatus().orElse(0) == 404) @@ -45,6 +42,6 @@ private boolean falsePositive400(OpenApiViolation violation) { private boolean falsePositive405(OpenApiViolation violation) { return violation.getResponseStatus().orElse(0) == 405 - && RULE_REQUEST_OPERATION_NOT_ALLOWED.equals(violation.getRule()); + && Rules.Request.OPERATION_NOT_ALLOWED.equals(violation.getRule()); } }