Skip to content

Commit 0eff7ba

Browse files
author
David
committed
add validation that Content-Type is supported according to schema
fixes #55
1 parent d55554e commit 0eff7ba

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/main/java/com/mservicetech/openapi/common/RequestEntity.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mservicetech.openapi.common;
22

3+
import com.networknt.oas.model.MediaType;
34
import java.util.HashMap;
45
import java.util.Map;
56

@@ -64,6 +65,13 @@ public String getContentType() {
6465
}
6566

6667
public void setContentType(String contentType) {
67-
this.contentType = contentType;
68+
this.contentType = contentType;
69+
}
70+
71+
public String getContentMediaType() {
72+
if(contentType!=null && contentType.contains(";")) {
73+
return contentType.substring(0, contentType.lastIndexOf(";"));
74+
}
75+
return contentType;
6876
}
6977
}

src/main/java/com/mservicetech/openapi/validation/OpenApiValidator.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class OpenApiValidator {
4444
final String STATUS_INVALID_REQUEST_PATH = "ERR10007";
4545
final String STATUS_METHOD_NOT_ALLOWED = "ERR10008";
4646
final String STATUS_CONTENT_TYPE_MISMATCH = "ERR10015";
47+
final String STATUS_CONTENT_TYPE_NOT_SUPPORTED = "ERR11108";
4748

4849
final String VALIDATOR_REQUEST_BODY_UNEXPECTED = "ERR11013";
4950
final String VALIDATOR_REQUEST_BODY_MISSING = "ERR11014";
@@ -167,10 +168,15 @@ public Status validateRequestPath (String requestURI , String httpMethod, Reques
167168
}
168169

169170
if (requestEntity!=null) {
171+
if(requestEntity.getContentMediaType()!=null &&
172+
openApiOperation.getOperation().getRequestBody().getContentMediaType(requestEntity.getContentMediaType())==null) {
173+
return new Status(STATUS_CONTENT_TYPE_NOT_SUPPORTED, requestEntity.getContentMediaType());
174+
}
175+
170176
NormalisedPath requestPath = openApiOperation.getPathString();
171177
Status status = validateRequestParameters(requestEntity, requestPath, openApiOperation);
172178
if(status != null) return status;
173-
if ((requestEntity.getContentType()==null || requestEntity.getContentType().startsWith("application/json"))) {
179+
if ((requestEntity.getContentType()==null || requestEntity.getContentMediaType().equals("application/json"))) {
174180
try {
175181
Object body = attachJsonBody(requestEntity.getRequestBody());
176182
status = validateRequestBody(body, openApiOperation);

src/test/java/com/mservicetech/openapi/validation/OpenApiValidatorTest.java

+40-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,20 @@ public void testRequestBodyNull() {
126126
}
127127

128128

129-
@Test
129+
@Test
130+
public void testRequestBodyContentTypeWithEncoding() {
131+
InputStream in = this.getClass().getClassLoader().getResourceAsStream("json/req1.json");
132+
String req1 = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
133+
134+
RequestEntity requestEntity = new RequestEntity();
135+
requestEntity.setRequestBody(req1);
136+
requestEntity.setContentType("application/json; charset=utf-8");
137+
Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity);
138+
Assert.assertNull(status);
139+
}
140+
141+
142+
@Test
130143
public void testRequestPath() {
131144

132145
RequestEntity requestEntity = new RequestEntity();
@@ -369,6 +382,32 @@ public void testRequestQueryFormObjectWithErrorMissingValue() {
369382
//{"statusCode":400,"code":"ERR11004","message":"VALIDATOR_SCHEMA","description":"Schema Validation Error - search.name: must be at least 1 characters long","severity":"ERROR"}
370383
}
371384

385+
@Test
386+
public void testRequestMediaType() {
387+
RequestEntity requestEntity = new RequestEntity();
388+
Map<String, Object> queryMap = new HashMap<>();
389+
requestEntity.setQueryParameters(queryMap);
390+
requestEntity.setContentType("application/xml");
391+
Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity);
392+
Assert.assertNotNull(status);
393+
Assert.assertEquals(status.getCode(), "ERR11108");
394+
}
395+
396+
397+
@Test
398+
public void testRequestMediaType2() {
399+
RequestEntity requestEntity = new RequestEntity();
400+
Map<String, Object> queryMap = new HashMap<>();
401+
queryMap.put("limit", 12);
402+
queryMap.put("search", "tag,cat,name");
403+
requestEntity.setQueryParameters(queryMap);
404+
requestEntity.setContentType("application/json");
405+
Status status = openApiValidator.validateRequestPath("/pets", "get", requestEntity);
406+
Assert.assertNotNull(status);
407+
Assert.assertEquals(status.getCode(), "ERR11108");
408+
}
409+
410+
372411
@Test
373412
public void testResponseBody() {
374413
InputStream in = this.getClass().getClassLoader().getResourceAsStream("json/req1.json");

0 commit comments

Comments
 (0)