5
5
import io .swagger .codegen .*;
6
6
import io .swagger .codegen .mustache .*;
7
7
import io .swagger .codegen .utils .ModelUtils ;
8
+ import io .swagger .models .Operation ;
9
+ import io .swagger .models .Path ;
10
+ import io .swagger .models .Swagger ;
8
11
import io .swagger .models .properties .*;
9
12
import org .apache .commons .lang3 .StringUtils ;
10
13
import org .slf4j .Logger ;
11
14
import org .slf4j .LoggerFactory ;
12
15
13
16
import java .io .File ;
14
- import java .util .*;
17
+ import java .util .ArrayList ;
18
+ import java .util .Arrays ;
19
+ import java .util .HashMap ;
20
+ import java .util .HashSet ;
21
+ import java .util .List ;
22
+ import java .util .Map ;
23
+ import java .util .Set ;
15
24
16
25
public abstract class AbstractCSharpCodegen extends DefaultCodegen implements CodegenConfig {
17
26
@@ -22,6 +31,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
22
31
protected boolean useDateTimeOffsetFlag = false ;
23
32
protected boolean useCollection = false ;
24
33
protected boolean returnICollection = false ;
34
+ protected boolean preserveNewLines = false ;
25
35
protected boolean netCoreProjectFileFlag = false ;
26
36
27
37
protected String modelPropertyNaming = CodegenConstants .MODEL_PROPERTY_NAMING_TYPE .PascalCase .name ();
@@ -78,7 +88,7 @@ public AbstractCSharpCodegen() {
78
88
// fully qualified name
79
89
"Client" , "client" , "parameter" ,
80
90
// local variable names in API methods (endpoints)
81
- "localVarPath" , "localVarPathParams" , "localVarQueryParams" , "localVarHeaderParams" ,
91
+ "localVarPath" , "localVarPathParams" , "localVarQueryParams" , "localVarHeaderParams" ,
82
92
"localVarFormParams" , "localVarFileParams" , "localVarStatusCode" , "localVarResponse" ,
83
93
"localVarPostBody" , "localVarHttpHeaderAccepts" , "localVarHttpHeaderAccept" ,
84
94
"localVarHttpContentTypes" , "localVarHttpContentType" ,
@@ -210,14 +220,14 @@ public void processOpts() {
210
220
if (additionalProperties .containsKey (CodegenConstants .INVOKER_PACKAGE )) {
211
221
LOGGER .warn (String .format ("%s is not used by C# generators. Please use %s" , CodegenConstants .INVOKER_PACKAGE , CodegenConstants .PACKAGE_NAME ));
212
222
}
213
-
223
+
214
224
// {{packageTitle}}
215
225
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_TITLE )) {
216
226
setPackageTitle ((String ) additionalProperties .get (CodegenConstants .PACKAGE_TITLE ));
217
227
} else {
218
228
additionalProperties .put (CodegenConstants .PACKAGE_TITLE , packageTitle );
219
229
}
220
-
230
+
221
231
// {{packageProductName}}
222
232
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_PRODUCTNAME )) {
223
233
setPackageProductName ((String ) additionalProperties .get (CodegenConstants .PACKAGE_PRODUCTNAME ));
@@ -231,14 +241,14 @@ public void processOpts() {
231
241
} else {
232
242
additionalProperties .put (CodegenConstants .PACKAGE_DESCRIPTION , packageDescription );
233
243
}
234
-
244
+
235
245
// {{packageCompany}}
236
246
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_COMPANY )) {
237
247
setPackageCompany ((String ) additionalProperties .get (CodegenConstants .PACKAGE_COMPANY ));
238
248
} else {
239
249
additionalProperties .put (CodegenConstants .PACKAGE_COMPANY , packageCompany );
240
250
}
241
-
251
+
242
252
// {{packageCopyright}}
243
253
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_COPYRIGHT )) {
244
254
setPackageCopyright ((String ) additionalProperties .get (CodegenConstants .PACKAGE_COPYRIGHT ));
@@ -252,7 +262,7 @@ public void processOpts() {
252
262
} else {
253
263
additionalProperties .put (CodegenConstants .PACKAGE_AUTHORS , packageAuthors );
254
264
}
255
-
265
+
256
266
// {{useDateTimeOffset}}
257
267
if (additionalProperties .containsKey (CodegenConstants .USE_DATETIME_OFFSET )) {
258
268
useDateTimeOffset (convertPropertyToBooleanAndWriteBack (CodegenConstants .USE_DATETIME_OFFSET ));
@@ -284,6 +294,10 @@ public void processOpts() {
284
294
additionalProperties .put (CodegenConstants .NETCORE_PROJECT_FILE , netCoreProjectFileFlag );
285
295
}
286
296
297
+ if (additionalProperties .containsKey (CodegenConstants .PRESERVE_COMMENT_NEWLINES )) {
298
+ setPreserveNewLines (Boolean .valueOf (additionalProperties .get (CodegenConstants .PRESERVE_COMMENT_NEWLINES ).toString ()));
299
+ }
300
+
287
301
if (additionalProperties .containsKey (CodegenConstants .INTERFACE_PREFIX )) {
288
302
String useInterfacePrefix = additionalProperties .get (CodegenConstants .INTERFACE_PREFIX ).toString ();
289
303
if ("false" .equals (useInterfacePrefix .toLowerCase ())) {
@@ -626,10 +640,10 @@ public String toParamName(String name) {
626
640
}
627
641
628
642
return name ;
629
- }
643
+ }
630
644
631
645
@ Override
632
- public String escapeReservedWord (String name ) {
646
+ public String escapeReservedWord (String name ) {
633
647
if (this .reservedWordsMappings ().containsKey (name )) {
634
648
return this .reservedWordsMappings ().get (name );
635
649
}
@@ -864,31 +878,31 @@ public void setPackageName(String packageName) {
864
878
public void setPackageVersion (String packageVersion ) {
865
879
this .packageVersion = packageVersion ;
866
880
}
867
-
881
+
868
882
public void setPackageTitle (String packageTitle ) {
869
883
this .packageTitle = packageTitle ;
870
884
}
871
-
885
+
872
886
public void setPackageProductName (String packageProductName ) {
873
887
this .packageProductName = packageProductName ;
874
888
}
875
889
876
890
public void setPackageDescription (String packageDescription ) {
877
891
this .packageDescription = packageDescription ;
878
892
}
879
-
893
+
880
894
public void setPackageCompany (String packageCompany ) {
881
895
this .packageCompany = packageCompany ;
882
896
}
883
-
897
+
884
898
public void setPackageCopyright (String packageCopyright ) {
885
899
this .packageCopyright = packageCopyright ;
886
900
}
887
901
888
902
public void setPackageAuthors (String packageAuthors ) {
889
903
this .packageAuthors = packageAuthors ;
890
904
}
891
-
905
+
892
906
public void setSourceFolder (String sourceFolder ) {
893
907
this .sourceFolder = sourceFolder ;
894
908
}
@@ -948,6 +962,32 @@ public String testPackageName() {
948
962
return this .packageName + ".Test" ;
949
963
}
950
964
965
+ public boolean isPreserveNewLines () {
966
+ return preserveNewLines ;
967
+ }
968
+
969
+ public void setPreserveNewLines (boolean preserveNewLines ) {
970
+ this .preserveNewLines = preserveNewLines ;
971
+ }
972
+
973
+ @ Override
974
+ public void preprocessSwagger (Swagger swagger ) {
975
+ if (this .preserveNewLines ) {
976
+ for (String pathname : swagger .getPaths ().keySet ()) {
977
+ Path path = swagger .getPaths ().get (pathname );
978
+ for (Operation op : path .getOperations ()) {
979
+ if (op .getDescription () != null ) {
980
+ op .setDescription (preservation (op .getDescription ()));
981
+ }
982
+ }
983
+ }
984
+ }
985
+ }
986
+
987
+ public String preservation (String input ) {
988
+ return input .replaceAll ("\\ n" , "~~N" );
989
+ }
990
+
951
991
@ Override
952
992
public String escapeQuotationMark (String input ) {
953
993
// remove " to avoid code injection
@@ -956,6 +996,10 @@ public String escapeQuotationMark(String input) {
956
996
957
997
@ Override
958
998
public String escapeUnsafeCharacters (String input ) {
959
- return input .replace ("*/" , "*_/" ).replace ("/*" , "/_*" ).replace ("--" , "- -" );
999
+ String intermediate = input .replace ("*/" , "*_/" ).replace ("/*" , "/_*" ).replace ("--" , "- -" );
1000
+
1001
+ intermediate = intermediate .replaceAll ("~~N" , "\n /// " );
1002
+
1003
+ return intermediate ;
960
1004
}
961
1005
}
0 commit comments