[Java Play Framework] Fix collection utils (#6025)

* Major fix to the collection handling in Play Framework Generator

* Major fix to the collection handling in Play Framework Generator (small details missing from previous commit)

* Add the fix to header and form parameters too
This commit is contained in:
Jean-François Côté
2017-07-18 07:36:43 -04:00
committed by wing328
parent dafefc06f1
commit dad3e6018a
6 changed files with 59 additions and 132 deletions

View File

@@ -56,7 +56,7 @@ public class {{classname}}Controller extends Controller {
{{/bodyParams}}
{{#queryParams}}
{{#collectionFormat}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", "{{paramName}}", request().getQueryString("{{baseName}}"));
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", request().queryString().get("{{baseName}}"));
{{{dataType}}} {{paramName}} = new Array{{{dataType}}}();
for (String curParam : {{paramName}}List) {
//noinspection UseBulkOperation
@@ -86,7 +86,7 @@ public class {{classname}}Controller extends Controller {
{{/notFile}}
{{#notFile}}
{{#collectionFormat}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", "{{paramName}}", (request().body().asMultipartFormData().asFormUrlEncoded().get("{{baseName}}"))[0]);
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", request().body().asMultipartFormData().asFormUrlEncoded().get("{{baseName}}"));
{{{dataType}}} {{paramName}} = new Array{{{dataType}}}();
for (String curParam : {{paramName}}List) {
//noinspection UseBulkOperation
@@ -109,7 +109,7 @@ public class {{classname}}Controller extends Controller {
{{/formParams}}
{{#headerParams}}
{{#collectionFormat}}
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", "{{paramName}}", request().getHeader("{{baseName}}"));
List<String> {{paramName}}List = SwaggerUtils.parametersToList("{{collectionFormat}}", request().headers().get("{{baseName}}"));
{{{dataType}}} {{paramName}} = new Array{{{dataType}}}();
for (String curParam : {{paramName}}List) {
//noinspection UseBulkOperation

View File

@@ -19,56 +19,42 @@ public class SwaggerUtils {
}
{{/handleExceptions}}
public static List<String> parametersToList(String collectionFormat, String name, Object value){
public static List<String> parametersToList(String collectionFormat, String[] values){
List<String> params = new ArrayList<>();
// preconditions
if (name == null || name.isEmpty() || value == null) return params;
if (values == null) {
return params;
}
Collection valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection) value;
if (values.length >= 1 && collectionFormat.equals("multi")) {
params.addAll(Arrays.asList(values));
} else {
params.add(parameterToString(value));
return params;
}
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
if (valueCollection.isEmpty()){
return params;
}
String delimiter = ",";
// get the collection format
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
// create the params based on the collection format
if (collectionFormat.equals("multi")) {
for (Object item : valueCollection) {
params.add(parameterToString(item));
switch(collectionFormat) {
case "csv": {
delimiter = ",";
break;
}
case "ssv": {
delimiter = " ";
break;
}
case "tsv": {
delimiter = "\t";
break;
}
case "pipes": {
delimiter = "|";
break;
}
}
return params;
params = Arrays.asList(values[0].split(delimiter));
}
String delimiter = ",";
if (collectionFormat.equals("csv")) {
delimiter = ",";
} else if (collectionFormat.equals("ssv")) {
delimiter = " ";
} else if (collectionFormat.equals("tsv")) {
delimiter = "\t";
} else if (collectionFormat.equals("pipes")) {
delimiter = "|";
}
StringBuilder sb = new StringBuilder() ;
for (Object item : valueCollection) {
sb.append(delimiter);
sb.append(parameterToString(item));
}
params.add(sb.substring(1));
return params;
}

View File

@@ -1 +1 @@
2.3.0-SNAPSHOT
2.2.3-SNAPSHOT

View File

@@ -62,7 +62,7 @@ public class PetApiController extends Controller {
@ApiAction
public Result findPetsByStatus() throws Exception {
List<String> statusList = SwaggerUtils.parametersToList("csv", "status", request().getQueryString("status"));
List<String> statusList = SwaggerUtils.parametersToList("csv", request().queryString().get("status"));
List<String> status = new ArrayList<String>();
for (String curParam : statusList) {
//noinspection UseBulkOperation
@@ -76,7 +76,7 @@ public class PetApiController extends Controller {
@ApiAction
public Result findPetsByTags() throws Exception {
List<String> tagsList = SwaggerUtils.parametersToList("csv", "tags", request().getQueryString("tags"));
List<String> tagsList = SwaggerUtils.parametersToList("csv", request().queryString().get("tags"));
List<String> tags = new ArrayList<String>();
for (String curParam : tagsList) {
//noinspection UseBulkOperation

View File

@@ -17,56 +17,42 @@ public class SwaggerUtils {
public @interface ApiAction {
}
public static List<String> parametersToList(String collectionFormat, String name, Object value){
public static List<String> parametersToList(String collectionFormat, String[] values){
List<String> params = new ArrayList<>();
// preconditions
if (name == null || name.isEmpty() || value == null) return params;
if (values == null) {
return params;
}
Collection valueCollection = null;
if (value instanceof Collection) {
valueCollection = (Collection) value;
if (values.length >= 1 && collectionFormat.equals("multi")) {
params.addAll(Arrays.asList(values));
} else {
params.add(parameterToString(value));
return params;
}
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
if (valueCollection.isEmpty()){
return params;
}
String delimiter = ",";
// get the collection format
collectionFormat = (collectionFormat == null || collectionFormat.isEmpty() ? "csv" : collectionFormat); // default: csv
// create the params based on the collection format
if (collectionFormat.equals("multi")) {
for (Object item : valueCollection) {
params.add(parameterToString(item));
switch(collectionFormat) {
case "csv": {
delimiter = ",";
break;
}
case "ssv": {
delimiter = " ";
break;
}
case "tsv": {
delimiter = "\t";
break;
}
case "pipes": {
delimiter = "|";
break;
}
}
return params;
params = Arrays.asList(values[0].split(delimiter));
}
String delimiter = ",";
if (collectionFormat.equals("csv")) {
delimiter = ",";
} else if (collectionFormat.equals("ssv")) {
delimiter = " ";
} else if (collectionFormat.equals("tsv")) {
delimiter = "\t";
} else if (collectionFormat.equals("pipes")) {
delimiter = "|";
}
StringBuilder sb = new StringBuilder() ;
for (Object item : valueCollection) {
sb.append(delimiter);
sb.append(parameterToString(item));
}
params.add(sb.substring(1));
return params;
}

View File

@@ -723,14 +723,6 @@
},
"title" : "Pet Order",
"description" : "An order for a pets from the pet store",
"example" : {
"petId" : 6,
"quantity" : 1,
"id" : 0,
"shipDate" : "2000-01-23T04:56:07.000+00:00",
"complete" : false,
"status" : "placed"
},
"xml" : {
"name" : "Order"
}
@@ -748,10 +740,6 @@
},
"title" : "Pet catehgry",
"description" : "A category for a pet",
"example" : {
"name" : "aeiou",
"id" : 6
},
"xml" : {
"name" : "Category"
}
@@ -789,16 +777,6 @@
},
"title" : "a User",
"description" : "A User who is purchasing from the pet store",
"example" : {
"firstName" : "aeiou",
"lastName" : "aeiou",
"password" : "aeiou",
"userStatus" : 6,
"phone" : "aeiou",
"id" : 0,
"email" : "aeiou",
"username" : "aeiou"
},
"xml" : {
"name" : "User"
}
@@ -816,10 +794,6 @@
},
"title" : "Pet Tag",
"description" : "A tag for a pet",
"example" : {
"name" : "aeiou",
"id" : 1
},
"xml" : {
"name" : "Tag"
}
@@ -867,20 +841,6 @@
},
"title" : "a Pet",
"description" : "A pet for sale in the pet store",
"example" : {
"photoUrls" : [ "aeiou" ],
"name" : "doggie",
"id" : 0,
"category" : {
"name" : "aeiou",
"id" : 6
},
"tags" : [ {
"name" : "aeiou",
"id" : 1
} ],
"status" : "available"
},
"xml" : {
"name" : "Pet"
}
@@ -900,12 +860,7 @@
}
},
"title" : "An uploaded response",
"description" : "Describes the result of uploading an image resource",
"example" : {
"code" : 0,
"type" : "aeiou",
"message" : "aeiou"
}
"description" : "Describes the result of uploading an image resource"
}
},
"externalDocs" : {