Skip to content

Commit 1deeb62

Browse files
authored
[bugfix] Handle invalid spec on a spec with multiple spec setup (#79)
1 parent 57eb0fe commit 1deeb62

File tree

5 files changed

+28
-6
lines changed

5 files changed

+28
-6
lines changed

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/DefaultViolationLogger.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void log(OpenApiViolation violation) {
2525
default -> { /* do nothing */ }
2626
}
2727
} catch (IOException e) {
28-
log.error("Could not add to LoggingContext", e);
28+
log.error("[OpenAPI Validation] Could not add to LoggingContext", e);
2929
}
3030
}
3131

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiInteractionValidatorFactory.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private MultipleSpecOpenApiInteractionValidatorWrapper buildMultipleSpecOpenApiI
5151
var path = entry.specificationFilePath();
5252
var specOptional = loadSpecFromPath(path).or(() -> loadSpecFromResources(path));
5353
if (specOptional.isEmpty()) {
54-
log.error("OpenAPI spec file {} could not be found", path);
54+
log.error("[OpenAPI Validation] Spec file {} could not be found", path);
5555
return null;
5656
}
5757
var validator = buildSingleSpecOpenApiInteractionValidatorWrapper(specOptional.get(),
@@ -69,6 +69,7 @@ private MultipleSpecOpenApiInteractionValidatorWrapper buildMultipleSpecOpenApiI
6969
return new MultipleSpecOpenApiInteractionValidatorWrapper(validators);
7070
}
7171

72+
@Nullable
7273
private SingleSpecOpenApiInteractionValidatorWrapper buildSingleSpecOpenApiInteractionValidatorWrapper(
7374
String spec,
7475
Map<String, LogLevel> levelResolverLevels,
@@ -84,7 +85,7 @@ private SingleSpecOpenApiInteractionValidatorWrapper buildSingleSpecOpenApiInter
8485
.build();
8586
return new SingleSpecOpenApiInteractionValidatorWrapper(validator);
8687
} catch (Throwable e) {
87-
log.error("Could not initialize OpenApiInteractionValidator [validation disabled]", e);
88+
log.error("[OpenAPI Validation] Could not initialize OpenApiInteractionValidator [validation disabled]", e);
8889
return null;
8990
}
9091
}

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/OpenApiRequestValidator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public List<OpenApiViolation> validateRequestObject(
9494
var result = validator.validateRequest(simpleRequest);
9595
return mapper.map(result, request, response, Direction.REQUEST, requestBody);
9696
} catch (Exception e) {
97-
log.error("Could not validate request", e);
97+
log.error("[OpenAPI Validation] Could not validate request", e);
9898
return List.of();
9999
}
100100
}
@@ -138,7 +138,7 @@ public List<OpenApiViolation> validateResponseObject(
138138
);
139139
return mapper.map(result, request, response, Direction.RESPONSE, responseBody);
140140
} catch (Exception e) {
141-
log.error("Could not validate response", e);
141+
log.error("[OpenAPI Validation] Could not validate response", e);
142142
return List.of();
143143
}
144144
}

openapi-validation-core/src/main/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ValidationReport validateResponse(String path, Request.Method method, Sim
4040
private Optional<OpenApiInteractionValidatorWrapper> getValidatorForPath(String path) {
4141
for (var validator : validators) {
4242
if (validator.getLeft().matcher(path).matches()) {
43-
return Optional.of(validator.getRight());
43+
return validator.getRight() != null ? Optional.of(validator.getRight()) : Optional.empty();
4444
}
4545
}
4646

openapi-validation-core/src/test/java/com/getyourguide/openapi/validation/core/validator/MultipleSpecOpenApiInteractionValidatorWrapperTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ public void testReturnsViolationWhenNoMatchingValidatorFound() {
5656
assertEquals("ValidatorConfiguration has no spec file matching path: /123", message.getMessage());
5757
}
5858

59+
@Test
60+
public void testReturnsViolationWhenMatchingValidatorIsNull() {
61+
var catchAllValidator = mockValidator();
62+
63+
validator = new MultipleSpecOpenApiInteractionValidatorWrapper(
64+
List.of(
65+
Pair.of(Pattern.compile("/test/.*"), null),
66+
Pair.of(Pattern.compile(".*"), catchAllValidator.validator())
67+
)
68+
);
69+
70+
var path = "/test/123";
71+
var report = validator.validateRequest(new SimpleRequest.Builder("GET", path).build());
72+
73+
var messages = report.getMessages();
74+
assertEquals(1, messages.size());
75+
var message = messages.get(0);
76+
assertEquals(MESSAGE_KEY_NO_VALIDATOR_FOUND, message.getKey());
77+
assertEquals("ValidatorConfiguration has no spec file matching path: /test/123", message.getMessage());
78+
}
79+
5980
private static MockValidatorResult mockValidator() {
6081
var catchAllValidator = mock(OpenApiInteractionValidatorWrapper.class);
6182
var catchAllValidationReport = mock(ValidationReport.class);

0 commit comments

Comments
 (0)