Skip to content

Commit 0e6d817

Browse files
committed
Fixes #1160: CollectionFormat default value for form and query parameters has been changed to multi
1 parent ef70819 commit 0e6d817

File tree

7 files changed

+154
-3
lines changed

7 files changed

+154
-3
lines changed

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/ParameterProcessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
9494
}
9595
processAllowedValues(allowableValues, true, args);
9696
PropertyBuilder.merge(p.getItems(), args);
97-
p.collectionFormat("csv");
9897
} else {
9998
if (StringUtils.isNotEmpty(defaultValue)) {
10099
p.setDefaultValue(defaultValue);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package io.swagger;
2+
3+
import io.swagger.jaxrs.Reader;
4+
import io.swagger.models.Operation;
5+
import io.swagger.models.Swagger;
6+
import io.swagger.models.parameters.AbstractSerializableParameter;
7+
import org.testng.annotations.Test;
8+
import resources.CollectionFormatResource;
9+
10+
import static org.testng.Assert.assertEquals;
11+
import static org.testng.Assert.assertNull;
12+
13+
public class CollectionFormatTest{
14+
private static final String MULTI = "multi";
15+
private static final String CSV = "csv";
16+
private final Swagger swagger = new Reader(new Swagger()).read(CollectionFormatResource.class);
17+
18+
@Test(testName = "check collection format for QueryParam")
19+
public void readQueryParamTest() {
20+
Operation operation = getOperation("testQueryParam");
21+
assertEquals(getCollectionFormat(operation, 0), MULTI);
22+
assertNull(getCollectionFormat(operation, 1));
23+
assertEquals(getCollectionFormat(operation, 2), MULTI);
24+
}
25+
26+
@Test(testName = "check collection format for FormParam")
27+
public void readFormParamTest() {
28+
Operation operation = getOperation("testFormParam");
29+
assertEquals(getCollectionFormat(operation, 0), MULTI);
30+
assertNull(getCollectionFormat(operation, 1));
31+
assertEquals(getCollectionFormat(operation, 2), MULTI);
32+
}
33+
34+
@Test(testName = "check collection format for PathParam")
35+
public void readPathParamTest() {
36+
Operation operation = getOperation("testPathParam");
37+
assertEquals(getCollectionFormat(operation, 0), CSV);
38+
assertNull(getCollectionFormat(operation, 1));
39+
assertEquals(getCollectionFormat(operation, 2), CSV);
40+
}
41+
42+
@Test(testName = "check collection format for HeaderParam")
43+
public void readHeaderParamTest() {
44+
Operation operation = getOperation("testHeaderParam");
45+
assertEquals(getCollectionFormat(operation, 0), CSV);
46+
assertNull(getCollectionFormat(operation, 1));
47+
assertEquals(getCollectionFormat(operation, 2), CSV);
48+
}
49+
50+
@Test(testName = "check collection format for CookieParam")
51+
public void readCookieParamTest() {
52+
Operation operation = getOperation("testCookieParam");
53+
assertEquals(getCollectionFormat(operation, 0), CSV);
54+
assertNull(getCollectionFormat(operation, 1));
55+
assertEquals(getCollectionFormat(operation, 2), CSV);
56+
}
57+
58+
@Test(testName = "check collection format for Mixed Param")
59+
public void readMixedParamTest() {
60+
Operation operation = getOperation("testMixedParam");
61+
assertEquals(getCollectionFormat(operation, 0), MULTI);
62+
assertEquals(getCollectionFormat(operation, 1), CSV);
63+
assertNull(getCollectionFormat(operation, 2));
64+
assertEquals(getCollectionFormat(operation, 3), CSV);
65+
}
66+
67+
private Operation getOperation(String name) {
68+
return swagger.getPath("/collectionFormat/" + name).getPost();
69+
}
70+
71+
private String getCollectionFormat(Operation op, int index) {
72+
return ((AbstractSerializableParameter) op.getParameters().get(index)).getCollectionFormat();
73+
}
74+
}

modules/swagger-jaxrs/src/test/scala/GenericsTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GenericsTest extends FlatSpec with Matchers {
2525
p.getName should be(name)
2626
p.getType should be("array")
2727
p.getFormat should be(null)
28-
p.getCollectionFormat should be("csv")
28+
p.getCollectionFormat should be("multi")
2929
p.getItems should not be (null)
3030
val schema = p.getItems.asInstanceOf[Property]
3131
schema.getType should be(`type`)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package resources;
2+
3+
import io.swagger.annotations.Api;
4+
import io.swagger.annotations.ApiOperation;
5+
import io.swagger.annotations.ApiParam;
6+
7+
import javax.ws.rs.CookieParam;
8+
import javax.ws.rs.FormParam;
9+
import javax.ws.rs.HeaderParam;
10+
import javax.ws.rs.POST;
11+
import javax.ws.rs.Path;
12+
import javax.ws.rs.PathParam;
13+
import javax.ws.rs.QueryParam;
14+
import java.util.List;
15+
16+
@Api
17+
@Path("/collectionFormat")
18+
public class CollectionFormatResource {
19+
20+
@POST
21+
@Path("/testQueryParam")
22+
@ApiOperation("Tests Query Param")
23+
public void testQueryParam(@QueryParam("list") List<Integer> list, @QueryParam("scalar") Integer scalar,
24+
@QueryParam("forced") @ApiParam(allowMultiple = true) int forced) {
25+
}
26+
27+
@POST
28+
@Path("/testFormParam")
29+
@ApiOperation("Tests Form Param")
30+
public void testFormParam(@FormParam("list") List<Integer> list, @FormParam("scalar") Integer scalar,
31+
@FormParam("forced") @ApiParam(allowMultiple = true) int forced) {
32+
}
33+
34+
@POST
35+
@Path("/testPathParam")
36+
@ApiOperation("Tests Path Param")
37+
public void testPathParam(@PathParam("list") List<Integer> list, @PathParam("scalar") Integer scalar,
38+
@PathParam("forced") @ApiParam(allowMultiple = true) int forced) {
39+
}
40+
41+
@POST
42+
@Path("/testHeaderParam")
43+
@ApiOperation("Tests Header Param")
44+
public void testHeaderParam(@HeaderParam("list") List<Integer> list, @HeaderParam("scalar") Integer scalar,
45+
@HeaderParam("forced") @ApiParam(allowMultiple = true) int forced) {
46+
}
47+
48+
@POST
49+
@Path("/testCookieParam")
50+
@ApiOperation("Tests Cookie Param")
51+
public void testCookieParam(@CookieParam("list") List<Integer> list, @CookieParam("scalar") Integer scalar,
52+
@CookieParam("forced") @ApiParam(allowMultiple = true) int forced) {
53+
}
54+
55+
@POST
56+
@Path("/testMixedParam")
57+
@ApiOperation("Tests Mixed Param")
58+
public void testMixedParam(@QueryParam("queryList") List<Integer> queryList,
59+
@PathParam("pathList") List<Integer> pathList, @HeaderParam("scalar") Integer scalar,
60+
@CookieParam("forced") @ApiParam(allowMultiple = true) int forced) {
61+
}
62+
}

modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ public T collectionFormat(String collectionFormat) {
5959
return castThis();
6060
}
6161

62+
@JsonIgnore
63+
protected String getDefaultCollectionFormat() {
64+
return "csv";
65+
}
66+
6267
public T items(Property items) {
6368
this.items = items;
6469
return castThis();
@@ -99,6 +104,7 @@ public String getType() {
99104

100105
public void setType(String type) {
101106
this.type = type;
107+
setCollectionFormat(ArrayProperty.isType(type) ? getDefaultCollectionFormat() : null);
102108
}
103109

104110
public String getCollectionFormat() {
@@ -110,7 +116,7 @@ public void setCollectionFormat(String collectionFormat) {
110116
}
111117

112118
public void setProperty(Property property) {
113-
this.type = property.getType();
119+
setType(property.getType());
114120
this.format = property.getFormat();
115121
if (property instanceof StringProperty) {
116122
final StringProperty string = (StringProperty) property;

modules/swagger-models/src/main/java/io/swagger/models/parameters/FormParameter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ public class FormParameter extends AbstractSerializableParameter<FormParameter>
55
public FormParameter() {
66
super.setIn("formData");
77
}
8+
9+
@Override
10+
protected String getDefaultCollectionFormat() {
11+
return "multi";
12+
}
813
}

modules/swagger-models/src/main/java/io/swagger/models/parameters/QueryParameter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ public class QueryParameter extends AbstractSerializableParameter<QueryParameter
55
public QueryParameter() {
66
super.setIn("query");
77
}
8+
9+
@Override
10+
protected String getDefaultCollectionFormat() {
11+
return "multi";
12+
}
813
}

0 commit comments

Comments
 (0)