Skip to content
Closed
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 @@ -29,6 +29,8 @@

import static com.predic8.membrane.core.openapi.util.UriUtil.*;
import static com.predic8.membrane.core.openapi.validators.ValidationContext.ValidatedEntityType.*;
import static com.predic8.membrane.core.openapi.validators.ValidationContext.fromRequest;
import static com.predic8.membrane.core.openapi.validators.ValidationErrors.error;
import static java.lang.String.*;

public class OpenAPIValidator {
Expand Down Expand Up @@ -86,18 +88,23 @@ private ValidationErrors validateMessage(Request<? extends Body> req, Response<?
}
}

return ValidationErrors.error( ValidationContext.fromRequest(req)
.entity(req.getPath())
.entityType(PATH)
.statusCode(404), format("Path %s is invalid.", req.getPath()));
if (res == null) {
return error(fromRequest(req)
.entity(req.getPath())
.entityType(PATH)
.statusCode(404), format("Path %s is invalid.", req.getPath()));
}

// Do not validate the path for response
return new ValidationErrors();
}

private ValidationErrors validateMethodsAndParametersIfPathMatches(Request<? extends Body> req, Response<? extends Body> response, String uriTemplate, PathItem pathItem) throws PathDoesNotMatchException {

// Throws exception if path or parameters do not match
req.parsePathParameters(normalizeUri(basePath + uriTemplate));

ValidationContext ctx = ValidationContext.fromRequest(req);
ValidationContext ctx = fromRequest(req);

ValidationErrors errors = validateMethods(ctx.uriTemplate(uriTemplate), req, response, pathItem);

Expand All @@ -122,4 +129,4 @@ private ValidationErrors validateMethods(ValidationContext ctx, Request<? extend
public OpenAPI getApi() {
return rec.getApi();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.*;

import static com.predic8.membrane.core.http.MimeType.*;
import static com.predic8.membrane.core.openapi.model.Response.statusCode;
import static com.predic8.membrane.core.openapi.validators.ValidationContext.ValidatedEntityType.*;
import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -37,14 +38,14 @@ protected String getOpenAPIFileName() {
@Test
public void validCustomerResponse() throws ParseException {

ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")));
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), statusCode(200).mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")));
assertEquals(0,errors.size());
}

@Test
public void invalidCustomerResponse() throws ParseException {

ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/invalid-customer.json")));
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), statusCode(200).mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/invalid-customer.json")));


assertEquals(3,errors.size());
Expand All @@ -62,7 +63,7 @@ public void statusCode404() {

InputStream is = getResourceAsStream("/openapi/messages/customer.json");

ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(404).json().body(is));
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), statusCode(404).json().body(is));
assertEquals(1,errors.size());
ValidationError e = errors.get(0);
assertEquals(BODY,e.getContext().getValidatedEntityType());
Expand All @@ -75,7 +76,7 @@ public void statusCode404() {

@Test
public void wrongMediaTypeResponse() throws ParseException {
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), Response.statusCode(200).mediaType(APPLICATION_XML).body(getResourceAsStream("/openapi/messages/customer.json")));
ValidationErrors errors = validator.validateResponse(Request.put().path("/customers"), statusCode(200).mediaType(APPLICATION_XML).body(getResourceAsStream("/openapi/messages/customer.json")));
assertEquals(1,errors.size());
ValidationError e = errors.get(0);
assertEquals(MEDIA_TYPE,e.getContext().getValidatedEntityType());
Expand All @@ -89,7 +90,7 @@ public void wrongMediaTypeResponse() throws ParseException {
*/
@Test
public void noContentInResponseSendPayload() throws ParseException {
ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")), Response.statusCode(200).mediaType(APPLICATION_JSON).body("{}"));
ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")), statusCode(200).mediaType(APPLICATION_JSON).body("{}"));
assertEquals(1,errors.size());
ValidationError e = errors.get(0);
assertEquals(BODY,e.getContext().getValidatedEntityType());
Expand All @@ -100,9 +101,8 @@ public void noContentInResponseSendPayload() throws ParseException {

@Test
public void statusCodeNotInResponse() throws ParseException {

ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")), Response.statusCode(202).mediaType(APPLICATION_JSON).body("{ }"));
System.out.println("errors = " + errors);
ValidationErrors errors = validator.validateResponse(Request.post().path("/customers").mediaType(APPLICATION_JSON).body(getResourceAsStream("/openapi/messages/customer.json")), statusCode(202).mediaType(APPLICATION_JSON).body("{ }"));
//System.out.println("errors = " + errors);
assertEquals(1,errors.size());
ValidationError e = errors.get(0);
assertEquals("POST",e.getContext().getMethod());
Expand All @@ -112,4 +112,9 @@ public void statusCodeNotInResponse() throws ParseException {
assertTrue(e.getMessage().toLowerCase().contains("status"));
assertTrue(e.getMessage().toLowerCase().contains("but allowed are only"));
}
}

@Test
public void responsePathMismatchDoesNotFailValidation() throws ParseException {
assertEquals(0, validator.validateResponse(Request.get().path("/does-not-exist"), statusCode(200).mediaType(APPLICATION_JSON).body("{}")).size());
}
}
Loading