Skip to content

Commit

Permalink
Fixes #1160: CollectionFormat default value for form and query parame…
Browse files Browse the repository at this point in the history
…ters has been changed to multi
  • Loading branch information
lugaru1234 committed Jun 30, 2015
1 parent ef70819 commit 0e6d817
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
}
processAllowedValues(allowableValues, true, args);
PropertyBuilder.merge(p.getItems(), args);
p.collectionFormat("csv");
} else {
if (StringUtils.isNotEmpty(defaultValue)) {
p.setDefaultValue(defaultValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.swagger;

import io.swagger.jaxrs.Reader;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.parameters.AbstractSerializableParameter;
import org.testng.annotations.Test;
import resources.CollectionFormatResource;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

public class CollectionFormatTest{
private static final String MULTI = "multi";
private static final String CSV = "csv";
private final Swagger swagger = new Reader(new Swagger()).read(CollectionFormatResource.class);

@Test(testName = "check collection format for QueryParam")
public void readQueryParamTest() {
Operation operation = getOperation("testQueryParam");
assertEquals(getCollectionFormat(operation, 0), MULTI);
assertNull(getCollectionFormat(operation, 1));
assertEquals(getCollectionFormat(operation, 2), MULTI);
}

@Test(testName = "check collection format for FormParam")
public void readFormParamTest() {
Operation operation = getOperation("testFormParam");
assertEquals(getCollectionFormat(operation, 0), MULTI);
assertNull(getCollectionFormat(operation, 1));
assertEquals(getCollectionFormat(operation, 2), MULTI);
}

@Test(testName = "check collection format for PathParam")
public void readPathParamTest() {
Operation operation = getOperation("testPathParam");
assertEquals(getCollectionFormat(operation, 0), CSV);
assertNull(getCollectionFormat(operation, 1));
assertEquals(getCollectionFormat(operation, 2), CSV);
}

@Test(testName = "check collection format for HeaderParam")
public void readHeaderParamTest() {
Operation operation = getOperation("testHeaderParam");
assertEquals(getCollectionFormat(operation, 0), CSV);
assertNull(getCollectionFormat(operation, 1));
assertEquals(getCollectionFormat(operation, 2), CSV);
}

@Test(testName = "check collection format for CookieParam")
public void readCookieParamTest() {
Operation operation = getOperation("testCookieParam");
assertEquals(getCollectionFormat(operation, 0), CSV);
assertNull(getCollectionFormat(operation, 1));
assertEquals(getCollectionFormat(operation, 2), CSV);
}

@Test(testName = "check collection format for Mixed Param")
public void readMixedParamTest() {
Operation operation = getOperation("testMixedParam");
assertEquals(getCollectionFormat(operation, 0), MULTI);
assertEquals(getCollectionFormat(operation, 1), CSV);
assertNull(getCollectionFormat(operation, 2));
assertEquals(getCollectionFormat(operation, 3), CSV);
}

private Operation getOperation(String name) {
return swagger.getPath("/collectionFormat/" + name).getPost();
}

private String getCollectionFormat(Operation op, int index) {
return ((AbstractSerializableParameter) op.getParameters().get(index)).getCollectionFormat();
}
}
2 changes: 1 addition & 1 deletion modules/swagger-jaxrs/src/test/scala/GenericsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GenericsTest extends FlatSpec with Matchers {
p.getName should be(name)
p.getType should be("array")
p.getFormat should be(null)
p.getCollectionFormat should be("csv")
p.getCollectionFormat should be("multi")
p.getItems should not be (null)
val schema = p.getItems.asInstanceOf[Property]
schema.getType should be(`type`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package resources;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.ws.rs.CookieParam;
import javax.ws.rs.FormParam;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import java.util.List;

@Api
@Path("/collectionFormat")
public class CollectionFormatResource {

@POST
@Path("/testQueryParam")
@ApiOperation("Tests Query Param")
public void testQueryParam(@QueryParam("list") List<Integer> list, @QueryParam("scalar") Integer scalar,
@QueryParam("forced") @ApiParam(allowMultiple = true) int forced) {
}

@POST
@Path("/testFormParam")
@ApiOperation("Tests Form Param")
public void testFormParam(@FormParam("list") List<Integer> list, @FormParam("scalar") Integer scalar,
@FormParam("forced") @ApiParam(allowMultiple = true) int forced) {
}

@POST
@Path("/testPathParam")
@ApiOperation("Tests Path Param")
public void testPathParam(@PathParam("list") List<Integer> list, @PathParam("scalar") Integer scalar,
@PathParam("forced") @ApiParam(allowMultiple = true) int forced) {
}

@POST
@Path("/testHeaderParam")
@ApiOperation("Tests Header Param")
public void testHeaderParam(@HeaderParam("list") List<Integer> list, @HeaderParam("scalar") Integer scalar,
@HeaderParam("forced") @ApiParam(allowMultiple = true) int forced) {
}

@POST
@Path("/testCookieParam")
@ApiOperation("Tests Cookie Param")
public void testCookieParam(@CookieParam("list") List<Integer> list, @CookieParam("scalar") Integer scalar,
@CookieParam("forced") @ApiParam(allowMultiple = true) int forced) {
}

@POST
@Path("/testMixedParam")
@ApiOperation("Tests Mixed Param")
public void testMixedParam(@QueryParam("queryList") List<Integer> queryList,
@PathParam("pathList") List<Integer> pathList, @HeaderParam("scalar") Integer scalar,
@CookieParam("forced") @ApiParam(allowMultiple = true) int forced) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public T collectionFormat(String collectionFormat) {
return castThis();
}

@JsonIgnore
protected String getDefaultCollectionFormat() {
return "csv";
}

public T items(Property items) {
this.items = items;
return castThis();
Expand Down Expand Up @@ -99,6 +104,7 @@ public String getType() {

public void setType(String type) {
this.type = type;
setCollectionFormat(ArrayProperty.isType(type) ? getDefaultCollectionFormat() : null);
}

public String getCollectionFormat() {
Expand All @@ -110,7 +116,7 @@ public void setCollectionFormat(String collectionFormat) {
}

public void setProperty(Property property) {
this.type = property.getType();
setType(property.getType());
this.format = property.getFormat();
if (property instanceof StringProperty) {
final StringProperty string = (StringProperty) property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ public class FormParameter extends AbstractSerializableParameter<FormParameter>
public FormParameter() {
super.setIn("formData");
}

@Override
protected String getDefaultCollectionFormat() {
return "multi";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ public class QueryParameter extends AbstractSerializableParameter<QueryParameter
public QueryParameter() {
super.setIn("query");
}

@Override
protected String getDefaultCollectionFormat() {
return "multi";
}
}

0 comments on commit 0e6d817

Please sign in to comment.