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 .Model ;
9
+ import io .swagger .models .ModelImpl ;
10
+ import io .swagger .models .Operation ;
11
+ import io .swagger .models .Path ;
12
+ import io .swagger .models .Response ;
13
+ import io .swagger .models .Swagger ;
14
+ import io .swagger .models .parameters .Parameter ;
8
15
import io .swagger .models .properties .*;
9
16
import org .apache .commons .lang3 .StringUtils ;
10
17
import org .slf4j .Logger ;
11
18
import org .slf4j .LoggerFactory ;
12
19
13
20
import java .io .File ;
14
- import java .util .*;
21
+ import java .util .ArrayList ;
22
+ import java .util .Arrays ;
23
+ import java .util .HashMap ;
24
+ import java .util .HashSet ;
25
+ import java .util .List ;
26
+ import java .util .Map ;
27
+ import java .util .Set ;
15
28
16
29
public abstract class AbstractCSharpCodegen extends DefaultCodegen implements CodegenConfig {
17
30
@@ -22,6 +35,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
22
35
protected boolean useDateTimeOffsetFlag = false ;
23
36
protected boolean useCollection = false ;
24
37
protected boolean returnICollection = false ;
38
+ protected boolean preserveNewLines = false ;
25
39
protected boolean netCoreProjectFileFlag = false ;
26
40
27
41
protected String modelPropertyNaming = CodegenConstants .MODEL_PROPERTY_NAMING_TYPE .PascalCase .name ();
@@ -78,7 +92,7 @@ public AbstractCSharpCodegen() {
78
92
// fully qualified name
79
93
"Client" , "client" , "parameter" ,
80
94
// local variable names in API methods (endpoints)
81
- "localVarPath" , "localVarPathParams" , "localVarQueryParams" , "localVarHeaderParams" ,
95
+ "localVarPath" , "localVarPathParams" , "localVarQueryParams" , "localVarHeaderParams" ,
82
96
"localVarFormParams" , "localVarFileParams" , "localVarStatusCode" , "localVarResponse" ,
83
97
"localVarPostBody" , "localVarHttpHeaderAccepts" , "localVarHttpHeaderAccept" ,
84
98
"localVarHttpContentTypes" , "localVarHttpContentType" ,
@@ -210,14 +224,14 @@ public void processOpts() {
210
224
if (additionalProperties .containsKey (CodegenConstants .INVOKER_PACKAGE )) {
211
225
LOGGER .warn (String .format ("%s is not used by C# generators. Please use %s" , CodegenConstants .INVOKER_PACKAGE , CodegenConstants .PACKAGE_NAME ));
212
226
}
213
-
227
+
214
228
// {{packageTitle}}
215
229
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_TITLE )) {
216
230
setPackageTitle ((String ) additionalProperties .get (CodegenConstants .PACKAGE_TITLE ));
217
231
} else {
218
232
additionalProperties .put (CodegenConstants .PACKAGE_TITLE , packageTitle );
219
233
}
220
-
234
+
221
235
// {{packageProductName}}
222
236
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_PRODUCTNAME )) {
223
237
setPackageProductName ((String ) additionalProperties .get (CodegenConstants .PACKAGE_PRODUCTNAME ));
@@ -231,14 +245,14 @@ public void processOpts() {
231
245
} else {
232
246
additionalProperties .put (CodegenConstants .PACKAGE_DESCRIPTION , packageDescription );
233
247
}
234
-
248
+
235
249
// {{packageCompany}}
236
250
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_COMPANY )) {
237
251
setPackageCompany ((String ) additionalProperties .get (CodegenConstants .PACKAGE_COMPANY ));
238
252
} else {
239
253
additionalProperties .put (CodegenConstants .PACKAGE_COMPANY , packageCompany );
240
254
}
241
-
255
+
242
256
// {{packageCopyright}}
243
257
if (additionalProperties .containsKey (CodegenConstants .PACKAGE_COPYRIGHT )) {
244
258
setPackageCopyright ((String ) additionalProperties .get (CodegenConstants .PACKAGE_COPYRIGHT ));
@@ -252,7 +266,7 @@ public void processOpts() {
252
266
} else {
253
267
additionalProperties .put (CodegenConstants .PACKAGE_AUTHORS , packageAuthors );
254
268
}
255
-
269
+
256
270
// {{useDateTimeOffset}}
257
271
if (additionalProperties .containsKey (CodegenConstants .USE_DATETIME_OFFSET )) {
258
272
useDateTimeOffset (convertPropertyToBooleanAndWriteBack (CodegenConstants .USE_DATETIME_OFFSET ));
@@ -284,6 +298,10 @@ public void processOpts() {
284
298
additionalProperties .put (CodegenConstants .NETCORE_PROJECT_FILE , netCoreProjectFileFlag );
285
299
}
286
300
301
+ if (additionalProperties .containsKey (CodegenConstants .PRESERVE_COMMENT_NEWLINES )) {
302
+ setPreserveNewLines (Boolean .valueOf (additionalProperties .get (CodegenConstants .PRESERVE_COMMENT_NEWLINES ).toString ()));
303
+ }
304
+
287
305
if (additionalProperties .containsKey (CodegenConstants .INTERFACE_PREFIX )) {
288
306
String useInterfacePrefix = additionalProperties .get (CodegenConstants .INTERFACE_PREFIX ).toString ();
289
307
if ("false" .equals (useInterfacePrefix .toLowerCase ())) {
@@ -626,10 +644,10 @@ public String toParamName(String name) {
626
644
}
627
645
628
646
return name ;
629
- }
647
+ }
630
648
631
649
@ Override
632
- public String escapeReservedWord (String name ) {
650
+ public String escapeReservedWord (String name ) {
633
651
if (this .reservedWordsMappings ().containsKey (name )) {
634
652
return this .reservedWordsMappings ().get (name );
635
653
}
@@ -864,31 +882,31 @@ public void setPackageName(String packageName) {
864
882
public void setPackageVersion (String packageVersion ) {
865
883
this .packageVersion = packageVersion ;
866
884
}
867
-
885
+
868
886
public void setPackageTitle (String packageTitle ) {
869
887
this .packageTitle = packageTitle ;
870
888
}
871
-
889
+
872
890
public void setPackageProductName (String packageProductName ) {
873
891
this .packageProductName = packageProductName ;
874
892
}
875
893
876
894
public void setPackageDescription (String packageDescription ) {
877
895
this .packageDescription = packageDescription ;
878
896
}
879
-
897
+
880
898
public void setPackageCompany (String packageCompany ) {
881
899
this .packageCompany = packageCompany ;
882
900
}
883
-
901
+
884
902
public void setPackageCopyright (String packageCopyright ) {
885
903
this .packageCopyright = packageCopyright ;
886
904
}
887
905
888
906
public void setPackageAuthors (String packageAuthors ) {
889
907
this .packageAuthors = packageAuthors ;
890
908
}
891
-
909
+
892
910
public void setSourceFolder (String sourceFolder ) {
893
911
this .sourceFolder = sourceFolder ;
894
912
}
@@ -948,6 +966,75 @@ public String testPackageName() {
948
966
return this .packageName + ".Test" ;
949
967
}
950
968
969
+ public boolean isPreserveNewLines () {
970
+ return preserveNewLines ;
971
+ }
972
+
973
+ public void setPreserveNewLines (boolean preserveNewLines ) {
974
+ this .preserveNewLines = preserveNewLines ;
975
+ }
976
+
977
+ @ Override
978
+ public void preprocessSwagger (Swagger swagger ) {
979
+ if (this .preserveNewLines ) {
980
+ if (swagger .getDefinitions () != null ) {
981
+ for (String name : swagger .getDefinitions ().keySet ()) {
982
+ Model model = swagger .getDefinitions ().get (name );
983
+ if (StringUtils .isNotBlank (model .getDescription ())) {
984
+ model .setDescription (preserveNewlines (model .getDescription (), 1 ));
985
+ }
986
+ if (model instanceof ModelImpl ) {
987
+ ModelImpl impl = (ModelImpl ) model ;
988
+ if (impl .getProperties () != null ) {
989
+ for (String propertyName : impl .getProperties ().keySet ()) {
990
+ Property property = impl .getProperties ().get (propertyName );
991
+ if (StringUtils .isNotBlank (property .getDescription ())) {
992
+ property .setDescription (preserveNewlines (property .getDescription (), 2 ));
993
+ }
994
+ }
995
+ }
996
+ }
997
+ }
998
+ }
999
+ for (String pathname : swagger .getPaths ().keySet ()) {
1000
+ Path path = swagger .getPaths ().get (pathname );
1001
+ for (Operation op : path .getOperations ()) {
1002
+ if (StringUtils .isNotBlank (op .getDescription ())) {
1003
+ op .setDescription (preserveNewlines (op .getDescription (), 2 ));
1004
+ }
1005
+ if (StringUtils .isNotBlank (op .getSummary ())) {
1006
+ op .setSummary (preserveNewlines (op .getSummary (), 2 ));
1007
+ }
1008
+ if (op .getParameters () != null ) {
1009
+ for (Parameter param : op .getParameters ()) {
1010
+ if (StringUtils .isNotBlank (param .getDescription ())) {
1011
+ param .setDescription (preserveNewlines (param .getDescription (), 2 ));
1012
+ }
1013
+ }
1014
+ }
1015
+ if (op .getResponses () != null ) {
1016
+ for (String responseCode : op .getResponses ().keySet ()) {
1017
+ Response response = op .getResponses ().get (responseCode );
1018
+
1019
+ if (StringUtils .isNotBlank (response .getDescription ())) {
1020
+ response .setDescription (preserveNewlines (response .getDescription (), 2 ));
1021
+ }
1022
+ }
1023
+ }
1024
+ }
1025
+ }
1026
+ }
1027
+ }
1028
+
1029
+ public String preserveNewlines (String input , int tabstops ) {
1030
+ if (tabstops == 1 ) {
1031
+ return input .replaceAll ("\\ n" , "~~N1" );
1032
+ } else {
1033
+ // assume 2 tabstops
1034
+ return input .replaceAll ("\\ n" , "~~N2" );
1035
+ }
1036
+ }
1037
+
951
1038
@ Override
952
1039
public String escapeQuotationMark (String input ) {
953
1040
// remove " to avoid code injection
@@ -956,6 +1043,11 @@ public String escapeQuotationMark(String input) {
956
1043
957
1044
@ Override
958
1045
public String escapeUnsafeCharacters (String input ) {
959
- return input .replace ("*/" , "*_/" ).replace ("/*" , "/_*" ).replace ("--" , "- -" );
1046
+ String intermediate = input .replace ("*/" , "*_/" ).replace ("/*" , "/_*" ).replace ("--" , "- -" );
1047
+
1048
+ intermediate = intermediate .replaceAll ("~~N1" , "\n /// " );
1049
+ intermediate = intermediate .replaceAll ("~~N2" , "\n /// " );
1050
+
1051
+ return intermediate ;
960
1052
}
961
1053
}
0 commit comments