From 0e6d8175159c82a920b3d3146ce3c6edaeb200f6 Mon Sep 17 00:00:00 2001 From: Nadezhda Makarkina Date: Thu, 25 Jun 2015 16:11:10 +0300 Subject: [PATCH] Fixes #1160: CollectionFormat default value for form and query parameters has been changed to multi --- .../io/swagger/jaxrs/ParameterProcessor.java | 1 - .../java/io/swagger/CollectionFormatTest.java | 74 +++++++++++++++++++ .../src/test/scala/GenericsTest.scala | 2 +- .../resources/CollectionFormatResource.java | 62 ++++++++++++++++ .../AbstractSerializableParameter.java | 8 +- .../models/parameters/FormParameter.java | 5 ++ .../models/parameters/QueryParameter.java | 5 ++ 7 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 modules/swagger-jaxrs/src/test/java/io/swagger/CollectionFormatTest.java create mode 100644 modules/swagger-jaxrs/src/test/scala/resources/CollectionFormatResource.java diff --git a/modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/ParameterProcessor.java b/modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/ParameterProcessor.java index 9392d29534..384262964e 100644 --- a/modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/ParameterProcessor.java +++ b/modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/ParameterProcessor.java @@ -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); diff --git a/modules/swagger-jaxrs/src/test/java/io/swagger/CollectionFormatTest.java b/modules/swagger-jaxrs/src/test/java/io/swagger/CollectionFormatTest.java new file mode 100644 index 0000000000..624ba837b5 --- /dev/null +++ b/modules/swagger-jaxrs/src/test/java/io/swagger/CollectionFormatTest.java @@ -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(); + } +} diff --git a/modules/swagger-jaxrs/src/test/scala/GenericsTest.scala b/modules/swagger-jaxrs/src/test/scala/GenericsTest.scala index 904a160709..c73c1f94d3 100644 --- a/modules/swagger-jaxrs/src/test/scala/GenericsTest.scala +++ b/modules/swagger-jaxrs/src/test/scala/GenericsTest.scala @@ -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`) diff --git a/modules/swagger-jaxrs/src/test/scala/resources/CollectionFormatResource.java b/modules/swagger-jaxrs/src/test/scala/resources/CollectionFormatResource.java new file mode 100644 index 0000000000..c43956f35e --- /dev/null +++ b/modules/swagger-jaxrs/src/test/scala/resources/CollectionFormatResource.java @@ -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 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 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 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 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 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 queryList, + @PathParam("pathList") List pathList, @HeaderParam("scalar") Integer scalar, + @CookieParam("forced") @ApiParam(allowMultiple = true) int forced) { + } +} diff --git a/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java b/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java index e4fcbf7aa6..945ddc458f 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/parameters/AbstractSerializableParameter.java @@ -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(); @@ -99,6 +104,7 @@ public String getType() { public void setType(String type) { this.type = type; + setCollectionFormat(ArrayProperty.isType(type) ? getDefaultCollectionFormat() : null); } public String getCollectionFormat() { @@ -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; diff --git a/modules/swagger-models/src/main/java/io/swagger/models/parameters/FormParameter.java b/modules/swagger-models/src/main/java/io/swagger/models/parameters/FormParameter.java index ea65f90b0d..3e77ad9bdf 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/parameters/FormParameter.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/parameters/FormParameter.java @@ -5,4 +5,9 @@ public class FormParameter extends AbstractSerializableParameter public FormParameter() { super.setIn("formData"); } + + @Override + protected String getDefaultCollectionFormat() { + return "multi"; + } } diff --git a/modules/swagger-models/src/main/java/io/swagger/models/parameters/QueryParameter.java b/modules/swagger-models/src/main/java/io/swagger/models/parameters/QueryParameter.java index 4db0b0c2c8..4334f110e9 100644 --- a/modules/swagger-models/src/main/java/io/swagger/models/parameters/QueryParameter.java +++ b/modules/swagger-models/src/main/java/io/swagger/models/parameters/QueryParameter.java @@ -5,4 +5,9 @@ public class QueryParameter extends AbstractSerializableParameter