Skip to content

add validation that Content-Type is supported according to schema #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
package com.mservicetech.openapi.common;

import com.networknt.oas.model.MediaType;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will release a snapshot version this evening. Thanks.


final String VALIDATOR_REQUEST_BODY_UNEXPECTED = "ERR11013";
final String VALIDATOR_REQUEST_BODY_MISSING = "ERR11014";
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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");
Expand Down