Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java Play Framework] Add required exception when a required parameters is not provided #6673

Merged
merged 1 commit into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,35 @@ public class {{classname}}Controller extends Controller {
{{^collectionFormat}}
JsonNode node{{paramName}} = request().body().asJson();
{{{dataType}}} {{paramName}};
{{^required}}
if (node{{paramName}} != null) {
{{paramName}} = mapper.readValue(node{{paramName}}.toString(), {{#isContainer}}new TypeReference<{{{dataType}}}>(){}{{/isContainer}}{{^isContainer}}{{{dataType}}}.class{{/isContainer}});{{/required}}
{{#required}}{{paramName}} = mapper.readValue(node{{paramName}}.toString(), {{#isContainer}}new TypeReference<{{{dataType}}}>(){}{{/isContainer}}{{^isContainer}}{{{dataType}}}.class{{/isContainer}});{{/required}}{{#useBeanValidation}}
{{#isListContainer}}for ({{{baseType}}} curItem : {{paramName}}) {
curItem.validate();
}{{/isListContainer}}{{#isMapContainer}}for (Map.Entry<String, {{{baseType}}}> entry : {{paramName}}.entrySet()) {
entry.getValue().validate();
}
{{/isMapContainer}}{{^isContainer}}{{paramName}}.validate();{{/isContainer}}{{/useBeanValidation}}
{{^required}}
{{paramName}} = mapper.readValue(node{{paramName}}.toString(), {{#isContainer}}new TypeReference<{{{dataType}}}>(){}{{/isContainer}}{{^isContainer}}{{{dataType}}}.class{{/isContainer}});
{{#useBeanValidation}}
{{#isListContainer}}for ({{{baseType}}} curItem : {{paramName}}) {
curItem.validate();
}{{/isListContainer}}{{#isMapContainer}}for (Map.Entry<String, {{{baseType}}}> entry : {{paramName}}.entrySet()) {
entry.getValue().validate();
}
{{/isMapContainer}}{{^isContainer}}{{paramName}}.validate();{{/isContainer}}
{{/useBeanValidation}}
} else {
{{#required}}
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
{{/required}}
{{^required}}
{{paramName}} = null;
}{{/required}}
{{/required}}
}
{{/collectionFormat}}
{{/bodyParams}}
{{#queryParams}}
{{#collectionFormat}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", request().queryString().get("{{baseName}}"));
String[] {{paramName}}Array = request().queryString().get("{{baseName}}");
{{#required}}
if ({{paramName}}Array == null) {
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
}
{{/required}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", {{paramName}}Array);
{{{dataType}}} {{paramName}} = new Array{{{dataType}}}();
for (String curParam : {{paramName}}List) {
//noinspection UseBulkOperation
Expand All @@ -80,28 +90,36 @@ public class {{classname}}Controller extends Controller {
{{^collectionFormat}}
String value{{paramName}} = request().getQueryString("{{baseName}}");
{{{dataType}}} {{paramName}};
{{^required}}
if (value{{paramName}} != null) {
{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};{{/required}}
{{#required}}{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};{{/required}}
{{^required}}
{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};
} else {
{{#required}}
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
{{/required}}
{{^required}}
{{paramName}} = {{>paramDefaultValue}};
}{{/required}}
{{/required}}
}
{{/collectionFormat}}
{{/queryParams}}
{{#formParams}}
{{^notFile}}
{{{dataType}}} {{paramName}} = request().body().asMultipartFormData().getFile("{{baseName}}");
{{#required}}
if (({{paramName}} == null || ((File) {{paramName}}.getFile()).length() == 0)) {
throw new RuntimeException("File cannot be empty");
throw new IllegalArgumentException("'{{baseName}}' file cannot be empty");
}
{{/required}}
{{/notFile}}
{{#notFile}}
{{#collectionFormat}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", request().body().asMultipartFormData().asFormUrlEncoded().get("{{baseName}}"));
String[] {{paramName}}Array = request().body().asMultipartFormData().asFormUrlEncoded().get("{{baseName}}");
{{#required}}
if ({{paramName}}Array == null) {
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
}
{{/required}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", {{paramName}}Array);
{{{dataType}}} {{paramName}} = new Array{{{dataType}}}();
for (String curParam : {{paramName}}List) {
//noinspection UseBulkOperation
Expand All @@ -111,20 +129,28 @@ public class {{classname}}Controller extends Controller {
{{^collectionFormat}}
String value{{paramName}} = (request().body().asMultipartFormData().asFormUrlEncoded().get("{{baseName}}"))[0];
{{{dataType}}} {{paramName}};
{{^required}}
if (value{{paramName}} != null) {
{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};{{/required}}
{{#required}}{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};{{/required}}
{{^required}}
{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};
} else {
{{#required}}
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
{{/required}}
{{^required}}
{{paramName}} = {{>paramDefaultValue}};
}{{/required}}
{{/required}}
}
{{/collectionFormat}}
{{/notFile}}
{{/formParams}}
{{#headerParams}}
{{#collectionFormat}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", request().headers().get("{{baseName}}"));
String[] {{paramName}}Array = request().headers().get("{{baseName}}");
{{#required}}
if ({{paramName}}Array == null) {
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
}
{{/required}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", {{paramName}}Array);
{{{dataType}}} {{paramName}} = new Array{{{dataType}}}();
for (String curParam : {{paramName}}List) {
//noinspection UseBulkOperation
Expand All @@ -134,14 +160,16 @@ public class {{classname}}Controller extends Controller {
{{^collectionFormat}}
String value{{paramName}} = request().getHeader("{{baseName}}");
{{{dataType}}} {{paramName}};
{{^required}}
if (value{{paramName}} != null) {
{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};{{/required}}
{{#required}}{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};{{/required}}
{{^required}}
{{paramName}} = {{>conversionBegin}}value{{paramName}}{{>conversionEnd}};
} else {
{{#required}}
throw new IllegalArgumentException("'{{baseName}}' parameter is required");
{{/required}}
{{^required}}
{{paramName}} = {{>paramDefaultValue}};
}{{/required}}
{{/required}}
}
{{/collectionFormat}}
{{/headerParams}}
{{^controllerOnly}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ private PetApiController() {
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();
}

Expand All @@ -49,7 +51,6 @@ public Result deletePet(Long petId) throws Exception {
String apiKey;
if (valueapiKey != null) {
apiKey = valueapiKey;

} else {
apiKey = null;
}
Expand All @@ -58,7 +59,11 @@ public Result deletePet(Long petId) throws Exception {

@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
Expand All @@ -69,7 +74,11 @@ public Result findPetsByStatus() throws Exception {

@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
Expand All @@ -87,10 +96,12 @@ public Result getPetById(Long petId) throws Exception {
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();
}

Expand All @@ -100,15 +111,13 @@ public Result updatePetWithForm(Long petId) throws Exception {
String name;
if (valuename != null) {
name = valuename;

} else {
name = null;
}
String valuestatus = (request().body().asMultipartFormData().asFormUrlEncoded().get("status"))[0];
String status;
if (valuestatus != null) {
status = valuestatus;

} else {
status = null;
}
Expand All @@ -121,7 +130,6 @@ public Result uploadFile(Long petId) throws Exception {
String additionalMetadata;
if (valueadditionalMetadata != null) {
additionalMetadata = valueadditionalMetadata;

} else {
additionalMetadata = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public Result getOrderById( @Min(1) @Max(5)Long orderId) throws Exception {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,42 @@ private UserApiController() {
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();
}

@ApiAction
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();
}

@ApiAction
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();
}

Expand All @@ -82,14 +88,18 @@ public Result getUserByName(String username) throws Exception {
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();
}

Expand All @@ -102,10 +112,12 @@ public Result logoutUser() throws Exception {
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class Module extends AbstractModule {

@Override
protected void configure() {
bind(AnotherFakeApiControllerImpInterface.class).to(AnotherFakeApiControllerImp.class);
bind(FakeApiControllerImpInterface.class).to(FakeApiControllerImp.class);
bind(FakeClassnameTags123ApiControllerImpInterface.class).to(FakeClassnameTags123ApiControllerImp.class);
bind(PetApiControllerImpInterface.class).to(PetApiControllerImp.class);
Expand Down
Loading