diff --git a/src/main/java/com/mservicetech/openapi/common/RequestEntity.java b/src/main/java/com/mservicetech/openapi/common/RequestEntity.java index c8b8302..1253ca6 100644 --- a/src/main/java/com/mservicetech/openapi/common/RequestEntity.java +++ b/src/main/java/com/mservicetech/openapi/common/RequestEntity.java @@ -1,5 +1,6 @@ package com.mservicetech.openapi.common; +import com.networknt.oas.model.MediaType; import java.util.HashMap; import java.util.Map; @@ -64,6 +65,13 @@ public String getContentType() { } public void setContentType(String contentType) { - this.contentType = contentType; + this.contentType = contentType; + } + + public String getContentMediaType() { + if(contentType!=null && contentType.contains(";")) { + return contentType.substring(0, contentType.lastIndexOf(";")); + } + return contentType; } } diff --git a/src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java b/src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java index ced2f3a..b124c74 100644 --- a/src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java +++ b/src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java @@ -44,6 +44,7 @@ public class OpenApiValidator { final String STATUS_INVALID_REQUEST_PATH = "ERR10007"; final String STATUS_METHOD_NOT_ALLOWED = "ERR10008"; final String STATUS_CONTENT_TYPE_MISMATCH = "ERR10015"; + final String STATUS_CONTENT_TYPE_NOT_SUPPORTED = "ERR11108"; final String VALIDATOR_REQUEST_BODY_UNEXPECTED = "ERR11013"; final String VALIDATOR_REQUEST_BODY_MISSING = "ERR11014"; @@ -167,10 +168,15 @@ public Status validateRequestPath (String requestURI , String httpMethod, Reques } if (requestEntity!=null) { + if(requestEntity.getContentMediaType()!=null && + openApiOperation.getOperation().getRequestBody().getContentMediaType(requestEntity.getContentMediaType())==null) { + return new Status(STATUS_CONTENT_TYPE_NOT_SUPPORTED, requestEntity.getContentMediaType()); + } + NormalisedPath requestPath = openApiOperation.getPathString(); Status status = validateRequestParameters(requestEntity, requestPath, openApiOperation); if(status != null) return status; - if ((requestEntity.getContentType()==null || requestEntity.getContentType().startsWith("application/json"))) { + if ((requestEntity.getContentType()==null || requestEntity.getContentMediaType().equals("application/json"))) { try { Object body = attachJsonBody(requestEntity.getRequestBody()); status = validateRequestBody(body, openApiOperation); diff --git a/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java b/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java index 0e937b4..776a901 100644 --- a/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java +++ b/src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java @@ -126,7 +126,20 @@ public void testRequestBodyNull() { } - @Test + @Test + public void testRequestBodyContentTypeWithEncoding() { + InputStream in = this.getClass().getClassLoader().getResourceAsStream("json/req1.json"); + String req1 = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); + + RequestEntity requestEntity = new RequestEntity(); + requestEntity.setRequestBody(req1); + requestEntity.setContentType("application/json; charset=utf-8"); + Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity); + Assert.assertNull(status); + } + + + @Test public void testRequestPath() { RequestEntity requestEntity = new RequestEntity(); @@ -369,6 +382,32 @@ public void testRequestQueryFormObjectWithErrorMissingValue() { //{"statusCode":400,"code":"ERR11004","message":"VALIDATOR_SCHEMA","description":"Schema Validation Error - search.name: must be at least 1 characters long","severity":"ERROR"} } + @Test + public void testRequestMediaType() { + RequestEntity requestEntity = new RequestEntity(); + Map queryMap = new HashMap<>(); + requestEntity.setQueryParameters(queryMap); + requestEntity.setContentType("application/xml"); + Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity); + Assert.assertNotNull(status); + Assert.assertEquals(status.getCode(), "ERR11108"); + } + + + @Test + public void testRequestMediaType2() { + RequestEntity requestEntity = new RequestEntity(); + Map queryMap = new HashMap<>(); + queryMap.put("limit", 12); + queryMap.put("search", "tag,cat,name"); + requestEntity.setQueryParameters(queryMap); + requestEntity.setContentType("application/json"); + Status status = openApiValidator.validateRequestPath("/pets", "get", requestEntity); + Assert.assertNotNull(status); + Assert.assertEquals(status.getCode(), "ERR11108"); + } + + @Test public void testResponseBody() { InputStream in = this.getClass().getClassLoader().getResourceAsStream("json/req1.json");