Skip to content

Commit 696d74a

Browse files
committed
[csharp] Options: useCollection, returnICollection
Added generator options for csharp to: * useCollection: Deserialize responses into and return Collection<T> * returnICollection: For List<T> or Collection<T>, return ICollection<T> instead of the concrete type
1 parent 060aae7 commit 696d74a

File tree

10 files changed

+133
-93
lines changed

10 files changed

+133
-93
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenConstants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ public class CodegenConstants {
6262
public static final String OPTIONAL_ASSEMBLY_INFO = "optionalAssemblyInfo";
6363
public static final String OPTIONAL_ASSEMBLY_INFO_DESC = "Generate AssemblyInfo.cs (Default: true).";
6464

65+
public static final String USE_COLLECTION = "useCollection";
66+
public static final String USE_COLLECTION_DESC = "Deserialize array types to Collection<T> instead of List<T>.";
67+
68+
public static final String RETURN_ICOLLECTION = "returnICollection";
69+
public static final String RETURN_ICOLLECTION_DESC = "Return ICollection<T> instead of the concrete type.";
6570
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/CSharpClientCodegen.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class CSharpClientCodegen extends DefaultCodegen implements CodegenConfig
2828
protected boolean optionalAssemblyInfoFlag = true;
2929
protected boolean optionalMethodArgumentFlag = true;
3030
protected boolean useDateTimeOffsetFlag = false;
31+
protected boolean useCollection = false;
32+
protected boolean returnICollection = false;
3133
protected String packageTitle = "Swagger Library";
3234
protected String packageProductName = "SwaggerLibrary";
3335
protected String packageDescription = "A library generated from a Swagger doc";
@@ -78,6 +80,7 @@ public CSharpClientCodegen() {
7880
"byte[]",
7981
"ICollection",
8082
"Collection",
83+
"List",
8184
"Dictionary",
8285
"DateTime?",
8386
"DateTimeOffset?",
@@ -91,7 +94,8 @@ public CSharpClientCodegen() {
9194
"Object")
9295
);
9396

94-
instantiationTypes.put("array", "Collection");
97+
instantiationTypes.put("array", "List");
98+
instantiationTypes.put("list", "List");
9599
instantiationTypes.put("map", "Dictionary");
96100

97101
typeMapping = new HashMap<String, String>();
@@ -106,8 +110,8 @@ public CSharpClientCodegen() {
106110
typeMapping.put("datetime", "DateTime?");
107111
typeMapping.put("date", "DateTime?");
108112
typeMapping.put("file", "Stream");
109-
typeMapping.put("array", "ICollection");
110-
typeMapping.put("list", "ICollection");
113+
typeMapping.put("array", "List");
114+
typeMapping.put("list", "List");
111115
typeMapping.put("map", "Dictionary");
112116
typeMapping.put("object", "Object");
113117

@@ -123,6 +127,11 @@ public CSharpClientCodegen() {
123127
CodegenConstants.OPTIONAL_ASSEMBLY_INFO_DESC).defaultValue(Boolean.TRUE.toString()));
124128
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, CodegenConstants.SOURCE_FOLDER_DESC).defaultValue(sourceFolder));
125129
cliOptions.add(CliOption.newBoolean(CodegenConstants.USE_DATETIME_OFFSET, CodegenConstants.USE_DATETIME_OFFSET_DESC));
130+
131+
cliOptions.add( CliOption.newBoolean(CodegenConstants.USE_COLLECTION, CodegenConstants.USE_COLLECTION_DESC)
132+
.defaultValue(Boolean.FALSE.toString()) );
133+
cliOptions.add( CliOption.newBoolean(CodegenConstants.RETURN_ICOLLECTION, CodegenConstants.RETURN_ICOLLECTION_DESC)
134+
.defaultValue(Boolean.FALSE.toString()) );
126135
}
127136

128137
@Override
@@ -179,6 +188,14 @@ public void processOpts() {
179188
.get(CodegenConstants.OPTIONAL_ASSEMBLY_INFO).toString()));
180189
}
181190

191+
if (additionalProperties.containsKey(CodegenConstants.USE_COLLECTION)){
192+
setUseCollection(Boolean.valueOf(additionalProperties.get(CodegenConstants.USE_COLLECTION).toString()));
193+
}
194+
195+
if (additionalProperties.containsKey(CodegenConstants.RETURN_ICOLLECTION)){
196+
setReturnICollection(Boolean.valueOf(additionalProperties.get(CodegenConstants.RETURN_ICOLLECTION).toString()));
197+
}
198+
182199
supportingFiles.add(new SupportingFile("Configuration.mustache",
183200
sourceFolder + File.separator + clientPackage.replace(".", java.io.File.separator), "Configuration.cs"));
184201
supportingFiles.add(new SupportingFile("ApiClient.mustache",
@@ -301,13 +318,15 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
301318
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
302319
for (CodegenOperation operation : ops) {
303320
if (operation.returnType != null) {
304-
// TODO: also change map from Dictionary to IDictionary
305-
if (operation.returnType.startsWith("ICollection") ||
306-
operation.returnType.startsWith("IDictionary")) {
307-
// Whenever returnType is an interface, deserialize to the concrete type
308-
operation.returnContainer = operation.returnType.substring(1);
309-
} else {
310-
operation.returnContainer = operation.returnType;
321+
operation.returnContainer = operation.returnType;
322+
if( this.returnICollection && (
323+
operation.returnType.startsWith("List")||
324+
operation.returnType.startsWith("Collection")) ) {
325+
// NOTE: ICollection works for both List<T> and Collection<T>
326+
int genericStart = operation.returnType.indexOf("<");
327+
if(genericStart > 0) {
328+
operation.returnType = "ICollection" + operation.returnType.substring(genericStart);
329+
}
311330
}
312331
}
313332
}
@@ -370,6 +389,21 @@ public void setOptionalMethodArgumentFlag(boolean flag) {
370389
this.optionalMethodArgumentFlag = flag;
371390
}
372391

392+
public void setReturnICollection(boolean returnICollection) {
393+
this.returnICollection = returnICollection;
394+
}
395+
396+
public void setUseCollection(boolean useCollection) {
397+
this.useCollection = useCollection;
398+
if(useCollection){
399+
typeMapping.put("array", "Collection");
400+
typeMapping.put("list", "Collection");
401+
402+
instantiationTypes.put("array", "Collection");
403+
instantiationTypes.put("list", "Collection");
404+
}
405+
}
406+
373407
public void useDateTimeOffset(boolean flag) {
374408
this.useDateTimeOffsetFlag = flag;
375409
if (flag)

modules/swagger-codegen/src/test/java/io/swagger/codegen/options/CSharpClientOptionsProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public Map<String, String> createOptions() {
2626
.put(CodegenConstants.OPTIONAL_ASSEMBLY_INFO, "true")
2727
.put(CodegenConstants.USE_DATETIME_OFFSET, "true")
2828
.put(CodegenConstants.SOURCE_FOLDER, SOURCE_FOLDER_VALUE)
29+
.put(CodegenConstants.USE_COLLECTION, "false")
30+
.put(CodegenConstants.RETURN_ICOLLECTION, "false")
2931
.build();
3032
}
3133

0 commit comments

Comments
 (0)