Skip to content

Commit

Permalink
fixes #380 validator error doesnot have the name for the header, path…
Browse files Browse the repository at this point in the history
… and query parameters (#381)
  • Loading branch information
stevehu authored Jul 16, 2024
1 parent cd553da commit 08c1e5e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.node.TextNode;
import com.networknt.config.Config;
import com.networknt.httpstring.AttachmentConstants;
import com.networknt.schema.PathType;
import io.undertow.util.Headers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -118,9 +119,11 @@ private Status validateRequestBody(Object requestBody, final OpenApiOperation op
}
return null;
}
SchemaValidatorsConfig config = new SchemaValidatorsConfig();
config.setTypeLoose(false);
config.setHandleNullableField(ValidatorHandler.config.isHandleNullableField());
SchemaValidatorsConfig config = SchemaValidatorsConfig.builder()
.typeLoose(false)
.pathType(PathType.JSON_POINTER)
.nullableKeywordEnabled(ValidatorHandler.config.isHandleNullableField())
.build();

// the body can be converted to JsonNode here. If not, an error is returned.
JsonNode requestNode;
Expand Down Expand Up @@ -191,7 +194,7 @@ private Status validatePathParameters(final HttpServerExchange exchange, final N
logger.info("Path parameter cannot be decoded, it will be used directly");
}

return schemaValidator.validate(new TextNode(paramValue), Overlay.toJson((SchemaImpl)(parameter.get().getSchema())));
return schemaValidator.validate(new TextNode(paramValue), Overlay.toJson((SchemaImpl)(parameter.get().getSchema())), paramName);
}
}
return status;
Expand Down Expand Up @@ -233,7 +236,7 @@ private Status validateQueryParameter(final HttpServerExchange exchange,

Optional<Status> optional = queryParameterValues
.stream()
.map((v) -> schemaValidator.validate(new TextNode(v), Overlay.toJson((SchemaImpl)queryParameter.getSchema())))
.map((v) -> schemaValidator.validate(new TextNode(v), Overlay.toJson((SchemaImpl)queryParameter.getSchema()), queryParameter.getName()))
.filter(s -> s != null)
.findFirst();

Expand All @@ -243,7 +246,7 @@ private Status validateQueryParameter(final HttpServerExchange exchange,
// thus array validation should be applied, for example, validate the length of the array.
} else {
final JsonNode content = Config.getInstance().getMapper().valueToTree(queryParameterValues);
return schemaValidator.validate(content, Overlay.toJson((SchemaImpl)queryParameter.getSchema()));
return schemaValidator.validate(content, Overlay.toJson((SchemaImpl)queryParameter.getSchema()), queryParameter.getName());
}
return null;
}
Expand Down Expand Up @@ -343,7 +346,7 @@ private Status validateHeader(final HttpServerExchange exchange,
} else {
Optional<Status> optional = headerValues
.stream()
.map((v) -> schemaValidator.validate(new TextNode(v), Overlay.toJson((SchemaImpl)headerParameter.getSchema())))
.map((v) -> schemaValidator.validate(new TextNode(v), Overlay.toJson((SchemaImpl)headerParameter.getSchema()), headerParameter.getName()))
.filter(s -> s != null)
.findFirst();
return optional.orElse(null);
Expand All @@ -363,7 +366,7 @@ private ValidationResult validateDeserializedValues(final HttpServerExchange exc
validationResult.addSkipped(p);
}else {
JsonNode jsonNode = Config.getInstance().getMapper().valueToTree(deserializedValue);
Status s = schemaValidator.validate(jsonNode, Overlay.toJson((SchemaImpl)(p.getSchema())));
Status s = schemaValidator.validate(jsonNode, Overlay.toJson((SchemaImpl)(p.getSchema())), p.getName());
validationResult.addStatus(s);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ public SchemaValidator() {
public SchemaValidator(final OpenApi3 api) {
this.api = api;
this.jsonNode = Overlay.toJson((OpenApi3Impl)api).get("components");
this.defaultConfig = new SchemaValidatorsConfig();
this.defaultConfig.setTypeLoose(true);
this.defaultConfig = SchemaValidatorsConfig.builder()
.typeLoose(true)
.pathType(PathType.JSON_POINTER)
.build();
}

/**
Expand All @@ -86,15 +88,19 @@ public Status validate(final JsonNode value, final JsonNode schema, SchemaValida
* @param value The value to validate
* @param schema The property schema to validate the value against
* @param config The config model for some validator
* @param instanceLocation The JsonNodePath being validated
* @param instanceLocation The location being validated
* @return Status object
*/
public Status validate(final JsonNode value, final JsonNode schema, SchemaValidatorsConfig config, JsonNodePath instanceLocation) {
return doValidate(value, schema, config, instanceLocation);
}

public Status validate(final JsonNode value, final JsonNode schema) {
return doValidate(value, schema, defaultConfig, null);
public Status validate(final JsonNode value, final JsonNode schema, String at) {
JsonNodePath instanceLocation = new JsonNodePath(defaultConfig.getPathType());
if (at != null) {
instanceLocation = instanceLocation.append(at);
}
return validate(value, schema, defaultConfig, instanceLocation);
}

public Status validate(final JsonNode value, final JsonNode schema, JsonNodePath instanceLocation) {
Expand Down

0 comments on commit 08c1e5e

Please sign in to comment.