From c3c73b8dae86fe15b665f3af6980766ba7a3ad44 Mon Sep 17 00:00:00 2001 From: Vivin Paliath Date: Wed, 17 Feb 2016 13:37:44 -0700 Subject: [PATCH] issue #1347 This is a fix to support enums in query parameters. Enum-related information was not being stored on `CodegenParameter` previously; it is now. Test cases have been added to make sure that the enum information is being properly processed from the model. --- .../io/swagger/codegen/CodegenParameter.java | 9 ++- .../io/swagger/codegen/DefaultCodegen.java | 7 ++ .../java/io/swagger/codegen/CodegenTest.java | 34 ++++++++++ .../src/test/resources/2_0/petstore.json | 65 ++++++++++++++++++- 4 files changed, 110 insertions(+), 5 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java index 55e21b7c421..4a8c34e374f 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java @@ -7,9 +7,9 @@ public class CodegenParameter { public Boolean isFormParam, isQueryParam, isPathParam, isHeaderParam, - isCookieParam, isBodyParam, hasMore, isContainer, + isCookieParam, isBodyParam, hasMore, isContainer, secondaryParam, isCollectionFormatMulti; - public String baseName, paramName, dataType, collectionFormat, description, baseType, defaultValue; + public String baseName, paramName, dataType, datatypeWithEnum, collectionFormat, description, baseType, defaultValue; public String jsonSchema; public Boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime; public Boolean isListContainer, isMapContainer; @@ -17,6 +17,7 @@ public class CodegenParameter { public boolean isEnum; public List _enum; public Map allowableValues; + public CodegenProperty items; public Map vendorExtensions; /** @@ -81,6 +82,7 @@ public CodegenParameter copy() { output.baseName = this.baseName; output.paramName = this.paramName; output.dataType = this.dataType; + output.datatypeWithEnum = this.datatypeWithEnum; output.collectionFormat = this.collectionFormat; output.isCollectionFormatMulti = this.isCollectionFormatMulti; output.description = this.description; @@ -112,6 +114,9 @@ public CodegenParameter copy() { if (this.allowableValues != null) { output.allowableValues = new HashMap(this.allowableValues); } + if (this.items != null) { + output.items = this.items; + } output.vendorExtensions = this.vendorExtensions; output.isBinary = this.isBinary; output.isByteArray = this.isByteArray; diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 37793bb2fb7..7bb2ed58501 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1660,9 +1660,16 @@ public CodegenParameter fromParameter(Parameter param, Set imports) { setParameterBooleanFlagWithCodegenProperty(p, model); p.dataType = model.datatype; + if(model.isEnum) { + p.datatypeWithEnum = model.datatypeWithEnum; + } p.isEnum = model.isEnum; p._enum = model._enum; p.allowableValues = model.allowableValues; + if(model.items != null && model.items.isEnum) { + p.datatypeWithEnum = model.datatypeWithEnum; + p.items = model.items; + } p.collectionFormat = collectionFormat; if(collectionFormat != null && collectionFormat.equals("multi")) { p.isCollectionFormatMulti = true; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java index 91a1240ced8..70880a29a3b 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/CodegenTest.java @@ -81,6 +81,40 @@ public void formParamTest() { Assert.assertNull(statusParam.hasMore); } + @Test(description = "handle enum array in query parameter test") + public void enumArrayQueryParameterTest() { + final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json"); + final DefaultCodegen codegen = new DefaultCodegen(); + final String path = "/pet/findByStatus"; + final Operation p = model.getPaths().get(path).getGet(); + final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions()); + + Assert.assertEquals(op.queryParams.size(), 1); + + final CodegenParameter statusParam = op.queryParams.get(0); + Assert.assertEquals(statusParam.items.datatypeWithEnum, "StatusEnum"); + Assert.assertNotNull(statusParam.items); + Assert.assertTrue(statusParam.items.isEnum); + Assert.assertEquals(statusParam.items._enum.size(), 3); + } + + @Test(description = "handle enum in query parameter test") + public void enumQueryParameterTest() { + final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/petstore.json"); + final DefaultCodegen codegen = new DefaultCodegen(); + final String path = "/store/findByStatus"; + final Operation p = model.getPaths().get(path).getGet(); + final CodegenOperation op = codegen.fromOperation(path, "get", p, model.getDefinitions()); + + Assert.assertEquals(op.queryParams.size(), 1); + + final CodegenParameter statusParam = op.queryParams.get(0); + Assert.assertEquals(statusParam.datatypeWithEnum, "StatusEnum"); + Assert.assertTrue(statusParam.isEnum); + Assert.assertEquals(statusParam._enum.size(), 3); + } + + @Test(description = "handle required parameters from a 2.0 spec as required when figuring out Swagger types") public void requiredParametersTest() { final Swagger model = parseAndPrepareSwagger("src/test/resources/2_0/requiredTest.json"); diff --git a/modules/swagger-codegen/src/test/resources/2_0/petstore.json b/modules/swagger-codegen/src/test/resources/2_0/petstore.json index b74b455add1..e11ee16fd33 100644 --- a/modules/swagger-codegen/src/test/resources/2_0/petstore.json +++ b/modules/swagger-codegen/src/test/resources/2_0/petstore.json @@ -156,7 +156,7 @@ "pet" ], "summary": "Finds Pets by status", - "description": "Multiple status values can be provided with comma seperated strings", + "description": "Multiple status values can be provided with comma separated strings", "operationId": "findPetsByStatus", "produces": [ "application/json", @@ -166,13 +166,23 @@ { "name": "status", "in": "query", - "description": "Status values that need to be considered for filter", + "description": "Status values that need to be considered for query", "required": false, "type": "array", "items": { - "type": "string" + "type": "string", + "enum": [ + "available", + "pending", + "sold" + ] }, "collectionFormat": "multi", + "enum": [ + "available", + "pending", + "sold" + ], "default": "available" } ], @@ -567,6 +577,55 @@ } ] } + }, + "/store/findByStatus": { + "get": { + "tags": [ + "store" + ], + "summary": "Finds orders by status", + "description": "A single status value can be provided as a string", + "operationId": "findOrdersByStatus", + "produces": [ + "application/json", + "application/xml" + ], + "parameters": [ + { + "name": "status", + "in": "query", + "description": "Status value that needs to be considered for query", + "required": false, + "type": "string", + "enum": [ + "placed", + "approved", + "delivered" + ], + "default": "placed" + } + ], + "responses": { + "200": { + "description": "successful operation", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Order" + } + } + }, + "400": { + "description": "Invalid status value" + } + }, + "security": [ + { + "test_api_client_id": [], + "test_api_client_secret": [] + } + ] + } }, "/store/order/{orderId}": { "get": {