diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java index 4a4f9e037ef2..8cbb9749f352 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java @@ -62,4 +62,9 @@ public class CodegenConstants { public static final String OPTIONAL_ASSEMBLY_INFO = "optionalAssemblyInfo"; public static final String OPTIONAL_ASSEMBLY_INFO_DESC = "Generate AssemblyInfo.cs (Default: true)."; + public static final String USE_COLLECTION = "useCollection"; + public static final String USE_COLLECTION_DESC = "Deserialize array types to Collection instead of List."; + + public static final String RETURN_ICOLLECTION = "returnICollection"; + public static final String RETURN_ICOLLECTION_DESC = "Return ICollection instead of the concrete type."; } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java index 55a4abbf4173..10c339fb404e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java @@ -7,6 +7,7 @@ import io.swagger.codegen.DefaultCodegen; import io.swagger.codegen.SupportingFile; import io.swagger.codegen.CodegenProperty; import io.swagger.codegen.CodegenModel; +import io.swagger.codegen.CodegenOperation; import io.swagger.models.properties.*; import io.swagger.codegen.CliOption; @@ -27,6 +28,8 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig protected boolean optionalAssemblyInfoFlag = true; protected boolean optionalMethodArgumentFlag = true; protected boolean useDateTimeOffsetFlag = false; + protected boolean useCollection = false; + protected boolean returnICollection = false; protected String packageTitle = "Swagger Library"; protected String packageProductName = "SwaggerLibrary"; protected String packageDescription = "A library generated from a Swagger doc"; @@ -75,6 +78,8 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig "long?", "float?", "byte[]", + "ICollection", + "Collection", "List", "Dictionary", "DateTime?", @@ -88,7 +93,9 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig "Stream", // not really a primitive, we include it to avoid model import "Object") ); + instantiationTypes.put("array", "List"); + instantiationTypes.put("list", "List"); instantiationTypes.put("map", "Dictionary"); typeMapping = new HashMap(); @@ -120,6 +127,11 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig CodegenConstants.OPTIONAL_ASSEMBLY_INFO_DESC).defaultValue(Boolean.TRUE.toString())); cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC).defaultValue(sourceFolder)); cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_DATETIME_OFFSET, CodegenConstants.USE_DATETIME_OFFSET_DESC)); + + cliOptions.add( CliOption.newBoolean(CodegenConstants.USE_COLLECTION, CodegenConstants.USE_COLLECTION_DESC) + .defaultValue(Boolean.FALSE.toString()) ); + cliOptions.add( CliOption.newBoolean(CodegenConstants.RETURN_ICOLLECTION, CodegenConstants.RETURN_ICOLLECTION_DESC) + .defaultValue(Boolean.FALSE.toString()) ); } @Override @@ -176,6 +188,14 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig .get(CodegenConstants.OPTIONAL_ASSEMBLY_INFO).toString())); } + if (additionalProperties.containsKey(CodegenConstants.USE_COLLECTION)){ + setUseCollection(Boolean.valueOf(additionalProperties.get(CodegenConstants.USE_COLLECTION).toString())); + } + + if (additionalProperties.containsKey(CodegenConstants.RETURN_ICOLLECTION)){ + setReturnICollection(Boolean.valueOf(additionalProperties.get(CodegenConstants.RETURN_ICOLLECTION).toString())); + } + supportingFiles.add(new SupportingFile("Configuration.mustache", sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "Configuration.cs")); supportingFiles.add(new SupportingFile("ApiClient.mustache", @@ -289,6 +309,32 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig return toModelName(name); } + @Override + public Map postProcessOperations(Map objs) { + super.postProcessOperations(objs); + if(objs != null) { + Map operations = (Map) objs.get("operations"); + if (operations != null) { + List ops = (List) operations.get("operation"); + for (CodegenOperation operation : ops) { + if (operation.returnType != null) { + operation.returnContainer = operation.returnType; + if( this.returnICollection && ( + operation.returnType.startsWith("List")|| + operation.returnType.startsWith("Collection")) ) { + // NOTE: ICollection works for both List and Collection + int genericStart = operation.returnType.indexOf("<"); + if(genericStart > 0) { + operation.returnType = "ICollection" + operation.returnType.substring(genericStart); + } + } + } + } + } + } + + return objs; + } @Override public String getTypeDeclaration(Property p) { @@ -343,6 +389,21 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig this.optionalMethodArgumentFlag = flag; } + public void setReturnICollection(boolean returnICollection) { + this.returnICollection = returnICollection; + } + + public void setUseCollection(boolean useCollection) { + this.useCollection = useCollection; + if(useCollection){ + typeMapping.put("array", "Collection"); + typeMapping.put("list", "Collection"); + + instantiationTypes.put("array", "Collection"); + instantiationTypes.put("list", "Collection"); + } + } + public void useDateTimeOffset(boolean flag) { this.useDateTimeOffsetFlag = flag; if (flag) diff --git a/modules/swagger-codegen/src/main/resources/csharp/api.mustache b/modules/swagger-codegen/src/main/resources/csharp/api.mustache index 28919d6be491..e4e1ba7aa906 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/api.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/api.mustache @@ -1,6 +1,7 @@ using System; using System.IO; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using RestSharp; using {{packageName}}.Client; @@ -239,7 +240,7 @@ namespace {{packageName}}.Api {{#returnType}}return new ApiResponse<{{{returnType}}}>(statusCode, response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), - ({{{returnType}}}) Configuration.ApiClient.Deserialize(response, typeof({{{returnType}}})));{{/returnType}} + ({{{returnType}}}) Configuration.ApiClient.Deserialize(response, typeof({{#returnContainer}}{{{returnContainer}}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}})));{{/returnType}} {{^returnType}}return new ApiResponse(statusCode, response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), null);{{/returnType}} @@ -344,7 +345,7 @@ namespace {{packageName}}.Api {{#returnType}}return new ApiResponse<{{{returnType}}}>(statusCode, response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), - ({{{returnType}}}) Configuration.ApiClient.Deserialize(response, typeof({{{returnType}}})));{{/returnType}} + ({{{returnType}}}) Configuration.ApiClient.Deserialize(response, typeof({{#returnContainer}}{{{returnContainer}}}{{/returnContainer}}{{^returnContainer}}{{{returnType}}}{{/returnContainer}})));{{/returnType}} {{^returnType}}return new ApiResponse(statusCode, response.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()), null);{{/returnType}} diff --git a/modules/swagger-codegen/src/main/resources/csharp/compile-mono.sh.mustache b/modules/swagger-codegen/src/main/resources/csharp/compile-mono.sh.mustache index 9472dcc8d3db..b84363cae39f 100644 --- a/modules/swagger-codegen/src/main/resources/csharp/compile-mono.sh.mustache +++ b/modules/swagger-codegen/src/main/resources/csharp/compile-mono.sh.mustache @@ -1,3 +1,4 @@ +#!/usr/bin/env bash wget -nc https://nuget.org/nuget.exe; mozroots --import --sync mono nuget.exe install vendor/packages.config -o vendor; diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java index 0c2da1f3fa2a..767119410047 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java @@ -26,6 +26,8 @@ public class CSharpClientOptionsProvider implements OptionsProvider { .put(CodegenConstants.OPTIONAL_ASSEMBLY_INFO, "true") .put(CodegenConstants.USE_DATETIME_OFFSET, "true") .put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE) + .put(CodegenConstants.USE_COLLECTION, "false") + .put(CodegenConstants.RETURN_ICOLLECTION, "false") .build(); } diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/compile-mono.sh b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/compile-mono.sh new file mode 100644 index 000000000000..67bc67e57a84 --- /dev/null +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/compile-mono.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +wget -nc https://nuget.org/nuget.exe; +mozroots --import --sync +mono nuget.exe install vendor/packages.config -o vendor; +mkdir -p bin; +mcs -sdk:45 -r:vendor/Newtonsoft.Json.8.0.2/lib/net45/Newtonsoft.Json.dll,\ +vendor/RestSharp.105.2.3/lib/net45/RestSharp.dll,\ +System.Runtime.Serialization.dll \ +-target:library \ +-out:bin/IO.Swagger.dll \ +-recurse:'src/*.cs' \ +-doc:bin/IO.Swagger.xml \ +-platform:anycpu diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs index 892412e5b3e1..fa1479f50932 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using RestSharp; using IO.Swagger.Client; diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs index a92876f1a53b..69499f4fd9bd 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/StoreApi.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using RestSharp; using IO.Swagger.Client; diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs index 0b9e633c5f3f..0c178a5637df 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/UserApi.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using RestSharp; using IO.Swagger.Client; diff --git a/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/vendor/packages.config b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/vendor/packages.config new file mode 100644 index 000000000000..c87bbb79fa67 --- /dev/null +++ b/samples/client/petstore/csharp/SwaggerClientTest/Lib/SwaggerClient/vendor/packages.config @@ -0,0 +1,5 @@ + + + + + diff --git a/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs b/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs index 001b052bf76a..3ee63f1e9244 100644 --- a/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs +++ b/samples/client/petstore/csharp/SwaggerClientTest/TestPet.cs @@ -221,7 +221,7 @@ namespace SwaggerClientTest.TestPet { Assembly _assembly = Assembly.GetExecutingAssembly(); Stream _imageStream = _assembly.GetManifestResourceStream("SwaggerClientTest.swagger-logo.png"); - PetApi petApi = new PetApi (); + PetApi petApi = new PetApi (); // test file upload with form parameters petApi.UploadFile(petId, "new form name", _imageStream); diff --git a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll index ad3f04827de3..e61191e219a9 100755 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll and b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll differ diff --git a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb index 9fd5b3c6f762..0d7773b3f908 100644 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb and b/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb differ diff --git a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll index ad3f04827de3..e61191e219a9 100755 Binary files a/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll and b/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll differ