Skip to content

Commit

Permalink
[bugfix] Handle invalid spec on a spec with multiple spec setup (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
pboos authored Jan 18, 2024
1 parent 57eb0fe commit 1deeb62
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
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

0 comments on commit 1deeb62

Please sign in to comment.