Skip to content

Commit

Permalink
Refactor: Create class InternalViolationExclusions
Browse files Browse the repository at this point in the history
  • Loading branch information
pboos committed Aug 23, 2023
1 parent 85cd957 commit 73b420a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.getyourguide.openapi.validation.core;

import com.atlassian.oai.validator.report.ValidationReport;
import com.getyourguide.openapi.validation.api.exclusions.ViolationExclusions;
import com.getyourguide.openapi.validation.api.log.LogLevel;
import com.getyourguide.openapi.validation.api.log.ViolationLogger;
import com.getyourguide.openapi.validation.api.metrics.MetricsReporter;
import com.getyourguide.openapi.validation.api.model.Direction;
import com.getyourguide.openapi.validation.api.model.OpenApiViolation;
import com.getyourguide.openapi.validation.api.model.RequestMetaData;
import com.getyourguide.openapi.validation.core.exclusions.InternalViolationExclusions;
import com.getyourguide.openapi.validation.core.throttle.ValidationReportThrottler;
import io.swagger.v3.oas.models.parameters.Parameter;
import java.util.Optional;
Expand All @@ -18,7 +18,7 @@ public class ValidationReportHandler {
private final ValidationReportThrottler throttleHelper;
private final ViolationLogger logger;
private final MetricsReporter metrics;
private final ViolationExclusions violationExclusions;
private final InternalViolationExclusions violationExclusions;

public void handleValidationReport(
RequestMetaData request,
Expand All @@ -31,7 +31,7 @@ public void handleValidationReport(
.getMessages()
.stream()
.map(message -> buildOpenApiViolation(message, request, body, direction))
.filter(violation -> !isViolationExcluded(violation))
.filter(violation -> !violationExclusions.isExcluded(violation))
.forEach(violation -> throttleHelper.throttle(violation, () -> logValidationError(violation)));
}
}
Expand Down Expand Up @@ -81,14 +81,6 @@ private OpenApiViolation buildOpenApiViolation(
.build();
}

private boolean isViolationExcluded(OpenApiViolation openApiViolation) {
return
violationExclusions.isExcluded(openApiViolation)
// If it matches more than 1, then we don't want to log a validation error
|| openApiViolation.getMessage().matches(
".*\\[Path '[^']+'] Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d\\).*");
}

private static Optional<String> getPointersInstance(ValidationReport.Message message) {
return message.getContext()
.flatMap(ValidationReport.MessageContext::getPointers)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.getyourguide.openapi.validation.core.exclusions;

import com.getyourguide.openapi.validation.api.exclusions.ViolationExclusions;
import com.getyourguide.openapi.validation.api.model.OpenApiViolation;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class InternalViolationExclusions {
private final ViolationExclusions customViolationExclusions;

public boolean isExcluded(OpenApiViolation violation) {
return customViolationExclusions.isExcluded(violation)
// If it matches more than 1, then we don't want to log a validation error
|| violation.getMessage().matches(
".*\\[Path '[^']+'] Instance failed to match exactly one schema \\(matched [1-9][0-9]* out of \\d\\).*");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.getyourguide.openapi.validation.core.OpenApiInteractionValidatorFactory;
import com.getyourguide.openapi.validation.core.OpenApiRequestValidator;
import com.getyourguide.openapi.validation.core.ValidationReportHandler;
import com.getyourguide.openapi.validation.core.exclusions.InternalViolationExclusions;
import com.getyourguide.openapi.validation.core.throttle.RequestBasedValidationReportThrottler;
import com.getyourguide.openapi.validation.core.throttle.ValidationReportThrottler;
import com.getyourguide.openapi.validation.core.throttle.ValidationReportThrottlerNone;
Expand Down Expand Up @@ -81,7 +82,7 @@ public ValidationReportHandler validationReportHandler(
validationReportThrottler,
logger,
metricsReporter,
violationExclusions.orElseGet(NoViolationExclusions::new)
new InternalViolationExclusions(violationExclusions.orElseGet(NoViolationExclusions::new))
);
}

Expand Down

0 comments on commit 73b420a

Please sign in to comment.