-
Notifications
You must be signed in to change notification settings - Fork 1
Exclude false positive violations #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
73b420a
Refactor: Create class InternalViolationExclusions
pboos f71ad6a
Fix tests
pboos ad7c7df
Refactor: Pass in responseMetaData when validating request if available
pboos a14a444
Refactor: Pass in responseMetaData when validating request if availab…
pboos c84d4e1
Implement 404 false positives
pboos 32602c8
Implement 400 false positives
pboos ae7d5c6
Formatting
pboos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...java/com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.getyourguide.openapi.validation.core.exclusions; | ||
|
||
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 final ViolationExclusions customViolationExclusions; | ||
|
||
public boolean isExcluded(OpenApiViolation violation) { | ||
return falsePositive404(violation) | ||
|| falsePositive400(violation) | ||
|| 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\\).*"); | ||
} | ||
|
||
private boolean falsePositive404(OpenApiViolation violation) { | ||
return "validation.request.path.missing".equals(violation.getRule()) | ||
&& ( | ||
violation.getDirection() == Direction.REQUEST | ||
|| (violation.getDirection() == Direction.RESPONSE && violation.getResponseStatus().orElse(0) == 404) | ||
); | ||
} | ||
|
||
private boolean falsePositive400(OpenApiViolation violation) { | ||
return violation.getDirection() == Direction.REQUEST && violation.getResponseStatus().orElse(0) == 400; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
.../com/getyourguide/openapi/validation/core/exclusions/InternalViolationExclusionsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.getyourguide.openapi.validation.core.exclusions; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
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 java.util.Optional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class InternalViolationExclusionsTest { | ||
private ViolationExclusions customViolationExclusions; | ||
private InternalViolationExclusions violationExclusions; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
customViolationExclusions = mock(); | ||
violationExclusions = new InternalViolationExclusions(customViolationExclusions); | ||
} | ||
|
||
@Test | ||
public void testWhenViolationThenViolationNotExcluded() { | ||
when(customViolationExclusions.isExcluded(any())).thenReturn(false); | ||
|
||
checkViolationNotExcluded(buildSimpleViolation(Direction.RESPONSE, 404)); | ||
checkViolationNotExcluded(buildSimpleViolation(Direction.RESPONSE, 400)); | ||
checkViolationNotExcluded(buildSimpleViolation(Direction.REQUEST, 200)); | ||
checkViolationNotExcluded(buildSimpleViolation(Direction.REQUEST, null)); | ||
checkViolationNotExcluded(buildSimpleViolation(Direction.RESPONSE, 200)); | ||
} | ||
|
||
private static OpenApiViolation buildSimpleViolation(Direction direction, Integer responseStatus) { | ||
return OpenApiViolation.builder() | ||
.direction(direction) | ||
.rule("validation." + (direction == Direction.REQUEST ? "request" : "response") + ".something") | ||
.responseStatus(responseStatus != null ? Optional.of(responseStatus) : Optional.empty()) | ||
.message("Some violation message") | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void testWhenCustomViolationExclusionThenViolationExcluded() { | ||
when(customViolationExclusions.isExcluded(any())).thenReturn(true); | ||
|
||
checkViolationExcluded(OpenApiViolation.builder().build()); | ||
} | ||
|
||
@Test | ||
public void testWhenInstanceFailedToMatchExactlyOneThenViolationExcluded() { | ||
when(customViolationExclusions.isExcluded(any())).thenReturn(false); | ||
|
||
checkViolationExcluded(OpenApiViolation.builder() | ||
.message("[Path '/v1/endpoint'] Instance failed to match exactly one schema (matched 2 out of 4)").build()); | ||
} | ||
|
||
@Test | ||
public void testWhen404ResponseWithApiPathNotSpecifiedThenViolationExcluded() { | ||
when(customViolationExclusions.isExcluded(any())).thenReturn(false); | ||
|
||
checkViolationExcluded(OpenApiViolation.builder() | ||
.direction(Direction.RESPONSE) | ||
.rule("validation.request.path.missing") | ||
.responseStatus(Optional.of(404)) | ||
.message("No API path found that matches request '/nothing'") | ||
.build()); | ||
} | ||
|
||
@Test | ||
public void testWhenRequestWithApiPathNotSpecifiedThenViolationExcluded() { | ||
when(customViolationExclusions.isExcluded(any())).thenReturn(false); | ||
|
||
checkViolationExcluded(OpenApiViolation.builder() | ||
.direction(Direction.REQUEST) | ||
.rule("validation.request.path.missing") | ||
.responseStatus(Optional.empty()) | ||
.message("No API path found that matches request '/nothing'") | ||
.build()); | ||
} | ||
|
||
@Test | ||
public void testWhenRequestViolationsAnd400ThenViolationExcluded() { | ||
when(customViolationExclusions.isExcluded(any())).thenReturn(false); | ||
|
||
checkViolationExcluded(OpenApiViolation.builder() | ||
.direction(Direction.REQUEST) | ||
.responseStatus(Optional.of(400)) | ||
.message("") | ||
.build()); | ||
} | ||
|
||
private void checkViolationNotExcluded(OpenApiViolation violation) { | ||
var isExcluded = violationExclusions.isExcluded(violation); | ||
|
||
assertFalse(isExcluded); | ||
} | ||
|
||
private void checkViolationExcluded(OpenApiViolation violation) { | ||
var isExcluded = violationExclusions.isExcluded(violation); | ||
|
||
assertTrue(isExcluded); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.