Skip to content
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

[bugfix] Handle invalid spec on a spec with multiple spec setup #79

Merged
merged 4 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void log(OpenApiViolation violation) {
default -> { /* do nothing */ }
}
} catch (IOException e) {
log.error("Could not add to LoggingContext", e);
log.error("[OpenAPI Validation] Could not add to LoggingContext", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private MultipleSpecOpenApiInteractionValidatorWrapper buildMultipleSpecOpenApiI
var path = entry.specificationFilePath();
var specOptional = loadSpecFromPath(path).or(() -> loadSpecFromResources(path));
if (specOptional.isEmpty()) {
log.error("OpenAPI spec file {} could not be found", path);
log.error("[OpenAPI Validation] Spec file {} could not be found", path);
return null;
}
var validator = buildSingleSpecOpenApiInteractionValidatorWrapper(specOptional.get(),
Expand All @@ -69,6 +69,7 @@ private MultipleSpecOpenApiInteractionValidatorWrapper buildMultipleSpecOpenApiI
return new MultipleSpecOpenApiInteractionValidatorWrapper(validators);
}

@Nullable
private SingleSpecOpenApiInteractionValidatorWrapper buildSingleSpecOpenApiInteractionValidatorWrapper(
String spec,
Map<String, LogLevel> levelResolverLevels,
Expand All @@ -84,7 +85,7 @@ private SingleSpecOpenApiInteractionValidatorWrapper buildSingleSpecOpenApiInter
.build();
return new SingleSpecOpenApiInteractionValidatorWrapper(validator);
} catch (Throwable e) {
log.error("Could not initialize OpenApiInteractionValidator [validation disabled]", e);
log.error("[OpenAPI Validation] Could not initialize OpenApiInteractionValidator [validation disabled]", e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public List<OpenApiViolation> validateRequestObject(
var result = validator.validateRequest(simpleRequest);
return mapper.map(result, request, response, Direction.REQUEST, requestBody);
} catch (Exception e) {
log.error("Could not validate request", e);
log.error("[OpenAPI Validation] Could not validate request", e);
return List.of();
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ public List<OpenApiViolation> validateResponseObject(
);
return mapper.map(result, request, response, Direction.RESPONSE, responseBody);
} catch (Exception e) {
log.error("Could not validate response", e);
log.error("[OpenAPI Validation] Could not validate response", e);
return List.of();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public ValidationReport validateResponse(String path, Request.Method method, Sim
private Optional<OpenApiInteractionValidatorWrapper> getValidatorForPath(String path) {
for (var validator : validators) {
if (validator.getLeft().matcher(path).matches()) {
return Optional.of(validator.getRight());
return validator.getRight() != null ? Optional.of(validator.getRight()) : Optional.empty();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ public void testReturnsViolationWhenNoMatchingValidatorFound() {
assertEquals("ValidatorConfiguration has no spec file matching path: /123", message.getMessage());
}

@Test
public void testReturnsViolationWhenMatchingValidatorIsNull() {
var catchAllValidator = mockValidator();

validator = new MultipleSpecOpenApiInteractionValidatorWrapper(
List.of(
Pair.of(Pattern.compile("/test/.*"), null),
Pair.of(Pattern.compile(".*"), catchAllValidator.validator())
)
);

var path = "/test/123";
var report = validator.validateRequest(new SimpleRequest.Builder("GET", path).build());

var messages = report.getMessages();
assertEquals(1, messages.size());
var message = messages.get(0);
assertEquals(MESSAGE_KEY_NO_VALIDATOR_FOUND, message.getKey());
assertEquals("ValidatorConfiguration has no spec file matching path: /test/123", message.getMessage());
}

private static MockValidatorResult mockValidator() {
var catchAllValidator = mock(OpenApiInteractionValidatorWrapper.class);
var catchAllValidationReport = mock(ValidationReport.class);
Expand Down