mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-12-03 00:43:46 +00:00
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:
committed by
wing328
parent
619c391be9
commit
8b70f24371
@@ -39,10 +39,12 @@ public class PetApiController extends Controller {
|
||||
public Result addPet() throws IOException {
|
||||
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();
|
||||
}
|
||||
@@ -53,7 +55,6 @@ public class PetApiController extends Controller {
|
||||
String apiKey;
|
||||
if (valueapiKey != null) {
|
||||
apiKey = valueapiKey;
|
||||
|
||||
} else {
|
||||
apiKey = null;
|
||||
}
|
||||
@@ -63,7 +64,11 @@ public class PetApiController extends Controller {
|
||||
|
||||
@ApiAction
|
||||
public Result findPetsByStatus() {
|
||||
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
|
||||
@@ -79,7 +84,11 @@ public class PetApiController extends Controller {
|
||||
|
||||
@ApiAction
|
||||
public Result findPetsByTags() {
|
||||
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
|
||||
@@ -105,10 +114,12 @@ public class PetApiController extends Controller {
|
||||
public Result updatePet() throws IOException {
|
||||
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();
|
||||
}
|
||||
@@ -119,7 +130,6 @@ public class PetApiController extends Controller {
|
||||
String name;
|
||||
if (valuename != null) {
|
||||
name = valuename;
|
||||
|
||||
} else {
|
||||
name = null;
|
||||
}
|
||||
@@ -127,7 +137,6 @@ public class PetApiController extends Controller {
|
||||
String status;
|
||||
if (valuestatus != null) {
|
||||
status = valuestatus;
|
||||
|
||||
} else {
|
||||
status = null;
|
||||
}
|
||||
@@ -141,7 +150,6 @@ public class PetApiController extends Controller {
|
||||
String additionalMetadata;
|
||||
if (valueadditionalMetadata != null) {
|
||||
additionalMetadata = valueadditionalMetadata;
|
||||
|
||||
} else {
|
||||
additionalMetadata = null;
|
||||
}
|
||||
|
||||
@@ -59,10 +59,12 @@ public class StoreApiController extends Controller {
|
||||
public Result placeOrder() throws IOException {
|
||||
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);
|
||||
|
||||
@@ -38,10 +38,12 @@ public class UserApiController extends Controller {
|
||||
public Result createUser() throws IOException {
|
||||
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();
|
||||
}
|
||||
@@ -50,12 +52,14 @@ public class UserApiController extends Controller {
|
||||
public Result createUsersWithArrayInput() throws IOException {
|
||||
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();
|
||||
}
|
||||
@@ -64,12 +68,14 @@ public class UserApiController extends Controller {
|
||||
public Result createUsersWithListInput() throws IOException {
|
||||
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();
|
||||
}
|
||||
@@ -92,14 +98,18 @@ public class UserApiController extends Controller {
|
||||
public Result loginUser() {
|
||||
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);
|
||||
@@ -115,10 +125,12 @@ public class UserApiController extends Controller {
|
||||
public Result updateUser(String username) throws IOException {
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user