Skip to content

Commit 0f86d62

Browse files
authored
Merge pull request #17 from delanym/main
Allow setting request body with JsonNode
2 parents 6a3b54a + 395684e commit 0f86d62

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020
<groupId>com.mservicetech</groupId>
2121
<artifactId>openapi-schema-validation</artifactId>
22-
<version>2.0.2</version>
22+
<version>2.0.3</version>
2323
<packaging>jar</packaging>
2424
<description>openapi schema for openpai 3.*</description>
2525
<url>https://github.com/mservicetech/openapi-schema-validation</url>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
import com.fasterxml.jackson.databind.JsonNode;
7+
68
public class RequestEntity {
79

810
Map<String, ?> pathParameters;
@@ -53,6 +55,10 @@ public void setRequestBody(String requestBody) {
5355
this.requestBody = requestBody;
5456
}
5557

58+
public void setRequestBody(JsonNode requestBody) {
59+
this.requestBody = requestBody.toPrettyString();
60+
}
61+
5662
public String getContentType() {
5763
return contentType;
5864
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.mservicetech.openapi.validation;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonNode;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
36
import com.mservicetech.openapi.common.RequestEntity;
47

58
import com.mservicetech.openapi.common.Status;
@@ -81,6 +84,21 @@ public void testRequestBody3() {
8184
//{"statusCode":400,"code":"ERR11004","message":"VALIDATOR_SCHEMA","description":"Schema Validation Error - requestBody.id: string found, integer expected","severity":"ERROR"}
8285
}
8386

87+
@Test
88+
public void testRequestBody4() throws JsonProcessingException {
89+
InputStream in = this.getClass().getClassLoader().getResourceAsStream("json/req3.json");
90+
String req = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
91+
RequestEntity requestEntity = new RequestEntity();
92+
ObjectMapper mapper = new ObjectMapper();
93+
JsonNode jsonNode = mapper.readTree(req);
94+
requestEntity.setRequestBody(jsonNode);
95+
requestEntity.setContentType("application/json");
96+
Status status = openApiValidator.validateRequestPath("/pets", "post", requestEntity);
97+
Assert.assertNotNull(status);
98+
Assert.assertEquals( status.getCode(), "ERR11004");
99+
//{"statusCode":400,"code":"ERR11004","message":"VALIDATOR_SCHEMA","description":"Schema Validation Error - requestBody.id: string found, integer expected","severity":"ERROR"}
100+
}
101+
84102
@Test
85103
public void testRequestPath() {
86104

0 commit comments

Comments
 (0)