diff --git a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/DefaultViolationLogger.java b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/DefaultViolationLogger.java index b7fb62a..e1445c5 100644 --- a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/DefaultViolationLogger.java +++ b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/DefaultViolationLogger.java @@ -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); } } diff --git a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiInteractionValidatorFactory.java b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiInteractionValidatorFactory.java index 3cb0529..5eb16ad 100644 --- a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiInteractionValidatorFactory.java +++ b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiInteractionValidatorFactory.java @@ -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(), @@ -69,6 +69,7 @@ private MultipleSpecOpenApiInteractionValidatorWrapper buildMultipleSpecOpenApiI return new MultipleSpecOpenApiInteractionValidatorWrapper(validators); } + @Nullable private SingleSpecOpenApiInteractionValidatorWrapper buildSingleSpecOpenApiInteractionValidatorWrapper( String spec, Map levelResolverLevels, @@ -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; } } diff --git a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiRequestValidator.java b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiRequestValidator.java index 02e0aa0..1991fda 100644 --- a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiRequestValidator.java +++ b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiRequestValidator.java @@ -94,7 +94,7 @@ public List 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(); } } @@ -138,7 +138,7 @@ public List 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(); } } diff --git a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapper.java b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapper.java index 8ba1a49..ab6369b 100644 --- a/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapper.java +++ b/openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapper.java @@ -40,7 +40,7 @@ public ValidationReport validateResponse(String path, Request.Method method, Sim private Optional 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(); } } diff --git a/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapperTest.java b/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapperTest.java index df4de8b..f54f4f0 100644 --- a/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapperTest.java +++ b/openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapperTest.java @@ -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);