Skip to content

Commit

Permalink
Extract rule constants to file
Browse files Browse the repository at this point in the history
  • Loading branch information
pboos committed Oct 18, 2023
1 parent 9bcd6ed commit 487d0f7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
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;
import lombok.AllArgsConstructor;

@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;

Expand All @@ -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)
Expand All @@ -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());
}
}

0 comments on commit 487d0f7

Please sign in to comment.