Skip to content

Commit

Permalink
Fixed Wildcard mimetype test - Fruitshop Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
predic8 committed Sep 6, 2023
1 parent 947db6c commit 53bbc56
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@

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.*;

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.*;

/**
Expand All @@ -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!
* <a href="https://fasterxml.github.io/jackson-databind/javadoc/2.6/com/fasterxml/jackson/databind/ObjectMapper.html">ObjectMapper.html</a>
*/
final private ObjectMapper om = new ObjectMapper();


@SuppressWarnings("rawtypes")
public ObjectValidator(OpenAPI api, Schema schema) {
this.api = api;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public StringValidator(Schema schema) {

@Override
public ValidationErrors validate(ValidationContext ctx, Object obj) {
ctx.schemaType("string");

ValidationErrors errors = new ValidationErrors();

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand All @@ -102,16 +102,16 @@ void postLikeSwaggerUIInvalidPrice() {
}
""")
.when()
.post(LOCALHOST_2000 + "/shop/v2/products/");
.post(LOCALHOST_2000 + "/shop/v2/products");
// @formatter:on

res.then().assertThat().statusCode(400);

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",
Expand Down

0 comments on commit 53bbc56

Please sign in to comment.