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 ;
8
10
import io .swagger .models .Operation ;
9
11
import io .swagger .models .Path ;
12
+ import io .swagger .models .Response ;
10
13
import io .swagger .models .Swagger ;
14
+ import io .swagger .models .parameters .Parameter ;
11
15
import io .swagger .models .properties .*;
12
16
import org .apache .commons .lang3 .StringUtils ;
13
17
import org .slf4j .Logger ;
@@ -972,20 +976,63 @@ public void setPreserveNewLines(boolean preserveNewLines) {
972
976
973
977
@ Override
974
978
public void preprocessSwagger (Swagger swagger ) {
975
- if (this .preserveNewLines ) {
976
- for (String pathname : swagger .getPaths ().keySet ()) {
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 ()) {
977
1000
Path path = swagger .getPaths ().get (pathname );
978
1001
for (Operation op : path .getOperations ()) {
979
- if (op .getDescription () != null ) {
980
- op .setDescription (preservation (op .getDescription ()));
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
+ }
981
1023
}
982
1024
}
983
1025
}
984
1026
}
985
1027
}
986
1028
987
- public String preservation (String input ) {
988
- return input .replaceAll ("\\ n" , "~~N" );
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
+ }
989
1036
}
990
1037
991
1038
@ Override
@@ -998,7 +1045,8 @@ public String escapeQuotationMark(String input) {
998
1045
public String escapeUnsafeCharacters (String input ) {
999
1046
String intermediate = input .replace ("*/" , "*_/" ).replace ("/*" , "/_*" ).replace ("--" , "- -" );
1000
1047
1001
- intermediate = intermediate .replaceAll ("~~N" , "\n /// " );
1048
+ intermediate = intermediate .replaceAll ("~~N1" , "\n /// " );
1049
+ intermediate = intermediate .replaceAll ("~~N2" , "\n /// " );
1002
1050
1003
1051
return intermediate ;
1004
1052
}
0 commit comments