There was no validation when a required field was null, creating crash and null pointer exception further down the line in the business code. Now, it throws a InvalidArgumentException. (#6673)

This commit is contained in:
Jean-François Côté
2017-10-16 09:23:05 -04:00
committed by wing328
parent 619c391be9
commit 8b70f24371
39 changed files with 747 additions and 425 deletions

View File

@@ -38,10 +38,12 @@ public class PetApiController extends Controller {
public Result addPet() throws Exception {
JsonNode nodebody = request().body().asJson();
Pet body;
body = mapper.readValue(nodebody.toString(), Pet.class);
body.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), Pet.class);
body.validate();
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
imp.addPet(body);
return ok();
}
@@ -52,7 +54,6 @@ public class PetApiController extends Controller {
String apiKey;
if (valueapiKey != null) {
apiKey = valueapiKey;
} else {
apiKey = null;
}
@@ -62,7 +63,11 @@ public class PetApiController extends Controller {
@ApiAction
public Result findPetsByStatus() throws Exception {
List<String> statusList = SwaggerUtils.parametersToList("csv", request().queryString().get("status"));
String[] statusArray = request().queryString().get("status");
if (statusArray == null) {
throw new IllegalArgumentException("'status' parameter is required");
}
List<String> statusList = SwaggerUtils.parametersToList("csv", statusArray);
List<String> status = new ArrayList<String>();
for (String curParam : statusList) {
//noinspection UseBulkOperation
@@ -78,7 +83,11 @@ public class PetApiController extends Controller {
@ApiAction
public Result findPetsByTags() throws Exception {
List<String> tagsList = SwaggerUtils.parametersToList("csv", request().queryString().get("tags"));
String[] tagsArray = request().queryString().get("tags");
if (tagsArray == null) {
throw new IllegalArgumentException("'tags' parameter is required");
}
List<String> tagsList = SwaggerUtils.parametersToList("csv", tagsArray);
List<String> tags = new ArrayList<String>();
for (String curParam : tagsList) {
//noinspection UseBulkOperation
@@ -104,10 +113,12 @@ public class PetApiController extends Controller {
public Result updatePet() throws Exception {
JsonNode nodebody = request().body().asJson();
Pet body;
body = mapper.readValue(nodebody.toString(), Pet.class);
body.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), Pet.class);
body.validate();
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
imp.updatePet(body);
return ok();
}
@@ -118,7 +129,6 @@ public class PetApiController extends Controller {
String name;
if (valuename != null) {
name = valuename;
} else {
name = null;
}
@@ -126,7 +136,6 @@ public class PetApiController extends Controller {
String status;
if (valuestatus != null) {
status = valuestatus;
} else {
status = null;
}
@@ -140,7 +149,6 @@ public class PetApiController extends Controller {
String additionalMetadata;
if (valueadditionalMetadata != null) {
additionalMetadata = valueadditionalMetadata;
} else {
additionalMetadata = null;
}

View File

@@ -58,10 +58,12 @@ public class StoreApiController extends Controller {
public Result placeOrder() throws Exception {
JsonNode nodebody = request().body().asJson();
Order body;
body = mapper.readValue(nodebody.toString(), Order.class);
body.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), Order.class);
body.validate();
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
Order obj = imp.placeOrder(body);
obj.validate();
JsonNode result = mapper.valueToTree(obj);

View File

@@ -37,10 +37,12 @@ public class UserApiController extends Controller {
public Result createUser() throws Exception {
JsonNode nodebody = request().body().asJson();
User body;
body = mapper.readValue(nodebody.toString(), User.class);
body.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), User.class);
body.validate();
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
imp.createUser(body);
return ok();
}
@@ -49,12 +51,14 @@ public class UserApiController extends Controller {
public Result createUsersWithArrayInput() throws Exception {
JsonNode nodebody = request().body().asJson();
List<User> body;
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
for (User curItem : body) {
curItem.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
for (User curItem : body) {
curItem.validate();
}
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
imp.createUsersWithArrayInput(body);
return ok();
}
@@ -63,12 +67,14 @@ public class UserApiController extends Controller {
public Result createUsersWithListInput() throws Exception {
JsonNode nodebody = request().body().asJson();
List<User> body;
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
for (User curItem : body) {
curItem.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), new TypeReference<List<User>>(){});
for (User curItem : body) {
curItem.validate();
}
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
imp.createUsersWithListInput(body);
return ok();
}
@@ -91,14 +97,18 @@ public class UserApiController extends Controller {
public Result loginUser() throws Exception {
String valueusername = request().getQueryString("username");
String username;
username = valueusername;
if (valueusername != null) {
username = valueusername;
} else {
throw new IllegalArgumentException("'username' parameter is required");
}
String valuepassword = request().getQueryString("password");
String password;
password = valuepassword;
if (valuepassword != null) {
password = valuepassword;
} else {
throw new IllegalArgumentException("'password' parameter is required");
}
String obj = imp.loginUser(username, password);
JsonNode result = mapper.valueToTree(obj);
return ok(result);
@@ -114,10 +124,12 @@ public class UserApiController extends Controller {
public Result updateUser(String username) throws Exception {
JsonNode nodebody = request().body().asJson();
User body;
body = mapper.readValue(nodebody.toString(), User.class);
body.validate();
if (nodebody != null) {
body = mapper.readValue(nodebody.toString(), User.class);
body.validate();
} else {
throw new IllegalArgumentException("'body' parameter is required");
}
imp.updateUser(username, body);
return ok();
}

View File

@@ -9,7 +9,7 @@
"email" : "apiteam@swagger.io"
},
"license" : {
"name" : "Apache 2.0",
"name" : "Apache-2.0",
"url" : "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},