cannot validate formData parameter
Issue #106
resolved
I use swagger-request-validator 1.3.10 in my programes and I hava a api like this:
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Test API",
"description": "A simple API to learn how to write OpenAPI Specification"
},
"schemes": [
"http",
"https"
],
"host": "127.0.0.1",
"basePath": "/mytest",
"paths": {
"/formDataTest": {
"post": {
"summary": "Gets some persons",
"consumes": [
"application/x-www-form-urlencoded",
"multipart/form-data"
],
"produces": [
"application/json"
],
"description": "Update the specific person.",
"parameters": [
{
"in": "formData",
"name": "A",
"required": true,
"type": "string"
},
{
"name": "B",
"in": "formData",
"required": true,
"type": "boolean"
},
{
"name": "H",
"in": "formData",
"required": true,
"type": "string"
}
]
}
}
}
}
and this is my java code,it seem can’t normal work to validate formData parameters:
@Test
public void formDataTest(){
SwaggerRequestResponseValidator validator =
SwaggerRequestResponseValidator.createFor("swaggerTest/swagger.json").build();
SimpleRequest.Builder requestBuilder = SimpleRequest.Builder.post("/mytest/formDataTest");
//requestBuilder.withQueryParam("A", "jack");
requestBuilder.withQueryParam("C", "true");
//requestBuilder.withQueryParam("H", "lily");
SimpleRequest request = requestBuilder.build();
ValidationReport validationReport = validator.validateRequest(request);
if(validationReport.hasErrors()){
String message = "";
for (ValidationReport.Message m : validationReport.getMessages()) {
message += m.getMessage() + " ";
}
System.out.println("-_- found error:" + message);
}else{
System.out.println("No mistake was found");
}
}
I defined the formData parameter 'A' in swagger specification, and the 'required' attribute was set to true, but I did not set the parameter 'A' in the created SimpleRequest object. In fact,validationReport.hasErrors () returned the result that should be true, but actually it was false. Please help me. I need to verify the formData parameter in the HTTP request.Thanks!!
Comments (3)
-
reporter -
reporter - changed status to resolved
-
All good - glad you got it working :)
- Log in to comment
I found the reason. I used a wrong function, I should use Withbody ().
It must be that I was too tired recently, and I made a low grade mistake.haw-haw