diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/validators/ArrayValidator.java b/core/src/main/java/com/predic8/membrane/core/openapi/validators/ArrayValidator.java index 229e7ff40..3e0a41d24 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/validators/ArrayValidator.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/validators/ArrayValidator.java @@ -39,6 +39,7 @@ public ArrayValidator(OpenAPI api, Schema schema) { @Override public ValidationErrors validate(ValidationContext ctx, Object value) { + ctx.schemaType("array"); ValidationErrors errors = new ValidationErrors(); Schema itemsSchema = schema.getItems(); diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/validators/BooleanValidator.java b/core/src/main/java/com/predic8/membrane/core/openapi/validators/BooleanValidator.java index eb0befd3a..ded422486 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/validators/BooleanValidator.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/validators/BooleanValidator.java @@ -17,8 +17,6 @@ package com.predic8.membrane.core.openapi.validators; import com.fasterxml.jackson.databind.node.*; -import io.swagger.v3.oas.models.*; -import io.swagger.v3.oas.models.media.*; public class BooleanValidator implements IJSONSchemaValidator { @@ -39,7 +37,7 @@ public ValidationErrors validate(ValidationContext ctx, Object value) { if (str.equals("true") || str.equals("false")) return errors; - errors.add(ctx,String.format("Value '%s' is not a boolean (true/false).",value)); + errors.add(ctx.schemaType("boolean"),String.format("Value '%s' is not a boolean (true/false).",value)); return errors; } diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/validators/NumberValidator.java b/core/src/main/java/com/predic8/membrane/core/openapi/validators/NumberValidator.java index b711642c1..cc4f36052 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/validators/NumberValidator.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/validators/NumberValidator.java @@ -17,11 +17,10 @@ package com.predic8.membrane.core.openapi.validators; import com.fasterxml.jackson.databind.*; -import io.swagger.v3.oas.models.media.*; import java.math.*; -import static java.lang.Double.parseDouble; +import static java.lang.Double.*; public class NumberValidator implements IJSONSchemaValidator { @@ -30,7 +29,7 @@ public class NumberValidator implements IJSONSchemaValidator { */ @Override public ValidationErrors validate(ValidationContext ctx, Object obj) { - try { + try { if (obj instanceof JsonNode) { // Not using double prevents from losing fractions new BigDecimal(((JsonNode) obj).asText()); diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/validators/ObjectValidator.java b/core/src/main/java/com/predic8/membrane/core/openapi/validators/ObjectValidator.java index 7dba84a9c..3c0a0555a 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/validators/ObjectValidator.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/validators/ObjectValidator.java @@ -18,9 +18,7 @@ import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.node.*; -import com.predic8.membrane.core.openapi.*; import com.predic8.membrane.core.openapi.util.*; -import com.predic8.membrane.core.openapi.validators.*; import io.swagger.v3.oas.models.*; import io.swagger.v3.oas.models.media.*; import org.slf4j.*; @@ -28,7 +26,7 @@ import java.io.*; import java.util.*; -import static com.predic8.membrane.core.openapi.util.Utils.joinByComma; +import static com.predic8.membrane.core.openapi.util.Utils.*; import static java.lang.String.*; /** @@ -40,20 +38,13 @@ public class ObjectValidator implements IJSONSchemaValidator { - private static Logger log = LoggerFactory.getLogger(ObjectValidator.class.getName()); + private static final Logger log = LoggerFactory.getLogger(ObjectValidator.class.getName()); @SuppressWarnings("rawtypes") private Schema schema; final private OpenAPI api; - /** - * ObjectMapper is Thread safe! - * ObjectMapper.html - */ - final private ObjectMapper om = new ObjectMapper(); - - @SuppressWarnings("rawtypes") public ObjectValidator(OpenAPI api, Schema schema) { this.api = api; @@ -65,6 +56,7 @@ public ObjectValidator(OpenAPI api, Schema schema) { @Override public ValidationErrors validate(ValidationContext ctx, Object obj) { + ctx.schemaType("object"); JsonNode node; if (obj instanceof JsonNode) { diff --git a/core/src/main/java/com/predic8/membrane/core/openapi/validators/StringValidator.java b/core/src/main/java/com/predic8/membrane/core/openapi/validators/StringValidator.java index 5b66f83cf..4ed0d752d 100644 --- a/core/src/main/java/com/predic8/membrane/core/openapi/validators/StringValidator.java +++ b/core/src/main/java/com/predic8/membrane/core/openapi/validators/StringValidator.java @@ -39,6 +39,7 @@ public StringValidator(Schema schema) { @Override public ValidationErrors validate(ValidationContext ctx, Object obj) { + ctx.schemaType("string"); ValidationErrors errors = new ValidationErrors(); @@ -48,8 +49,7 @@ public ValidationErrors validate(ValidationContext ctx, Object obj) { } String value; - if (obj instanceof JsonNode) { - JsonNode node = ((JsonNode) obj); + if (obj instanceof JsonNode node) { if (!JsonNodeType.STRING.equals(node.getNodeType())) { errors.add(ctx, format("String expected but got %s of type %s", node, node.getNodeType())); return errors; diff --git a/distribution/src/test/java/com/predic8/membrane/examples/tests/openapi/APIProxyTest.java b/distribution/src/test/java/com/predic8/membrane/examples/tests/openapi/APIProxyTest.java index ede42fe34..20a565662 100644 --- a/distribution/src/test/java/com/predic8/membrane/examples/tests/openapi/APIProxyTest.java +++ b/distribution/src/test/java/com/predic8/membrane/examples/tests/openapi/APIProxyTest.java @@ -82,7 +82,7 @@ void postLikeSwaggerUI() { } """) .when() - .post(LOCALHOST_2000 + "/shop/v2/products/") + .post(LOCALHOST_2000 + "/shop/v2/products") .then() .statusCode(201) .body("name", Matchers.equalTo("Figs")) @@ -102,7 +102,7 @@ void postLikeSwaggerUIInvalidPrice() { } """) .when() - .post(LOCALHOST_2000 + "/shop/v2/products/"); + .post(LOCALHOST_2000 + "/shop/v2/products"); // @formatter:on res.then().assertThat().statusCode(400); @@ -110,8 +110,8 @@ void postLikeSwaggerUIInvalidPrice() { JSONAssert.assertEquals(""" { "method" : "POST", - "uriTemplate" : "/products/", - "path" : "/shop/v2/products/", + "uriTemplate" : "/products", + "path" : "/shop/v2/products", "validationErrors" : { "REQUEST/BODY#/price" : [ { "message" : "-2.7 is smaller than the minimum of 0",