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

@@ -36,10 +36,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");
}
return ok();
}
@@ -49,7 +51,6 @@ public class PetApiController extends Controller {
String apiKey;
if (valueapiKey != null) {
apiKey = valueapiKey;
} else {
apiKey = null;
}
@@ -58,7 +59,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
@@ -69,7 +74,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
@@ -87,10 +96,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");
}
return ok();
}
@@ -100,7 +111,6 @@ public class PetApiController extends Controller {
String name;
if (valuename != null) {
name = valuename;
} else {
name = null;
}
@@ -108,7 +118,6 @@ public class PetApiController extends Controller {
String status;
if (valuestatus != null) {
status = valuestatus;
} else {
status = null;
}
@@ -121,7 +130,6 @@ public class PetApiController extends Controller {
String additionalMetadata;
if (valueadditionalMetadata != null) {
additionalMetadata = valueadditionalMetadata;
} else {
additionalMetadata = null;
}

View File

@@ -50,10 +50,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");
}
return ok();
}
}

View File

@@ -35,10 +35,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");
}
return ok();
}
@@ -46,12 +48,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");
}
return ok();
}
@@ -59,12 +63,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");
}
return ok();
}
@@ -82,14 +88,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");
}
return ok();
}
@@ -102,10 +112,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");
}
return ok();
}
}