3
3
import java .io .File ;
4
4
import java .util .ArrayList ;
5
5
import java .util .Arrays ;
6
+ import java .util .Collection ;
7
+ import java .util .Collections ;
6
8
import java .util .HashMap ;
7
9
import java .util .HashSet ;
8
10
import java .util .Iterator ;
12
14
import java .util .regex .Matcher ;
13
15
import java .util .regex .Pattern ;
14
16
17
+ import javax .annotation .Nullable ;
18
+
15
19
import org .apache .commons .lang .StringUtils ;
16
20
import org .slf4j .Logger ;
17
21
import org .slf4j .LoggerFactory ;
18
22
23
+ import com .google .common .base .Function ;
24
+ import com .google .common .collect .Lists ;
19
25
import com .wordnik .swagger .codegen .examples .ExampleGenerator ;
20
26
import com .wordnik .swagger .models .ArrayModel ;
27
+ import com .wordnik .swagger .models .ComposedModel ;
21
28
import com .wordnik .swagger .models .Model ;
22
29
import com .wordnik .swagger .models .ModelImpl ;
23
30
import com .wordnik .swagger .models .Operation ;
53
60
import com .wordnik .swagger .models .properties .StringProperty ;
54
61
import com .wordnik .swagger .util .Json ;
55
62
63
+
56
64
public class DefaultCodegen {
57
- Logger LOGGER = LoggerFactory .getLogger (DefaultCodegen .class );
65
+ private static final Logger LOGGER = LoggerFactory .getLogger (DefaultCodegen .class );
58
66
59
67
protected String outputFolder = "" ;
60
68
protected Set <String > defaultIncludes = new HashSet <String >();
@@ -192,6 +200,7 @@ public String toVarName(String name) {
192
200
}
193
201
194
202
public String toParamName (String name ) {
203
+ name = removeNonNameElementToCamelCase (name );
195
204
if (reservedWords .contains (name )) {
196
205
return escapeReservedWord (name );
197
206
}
@@ -466,100 +475,28 @@ public CodegenModel fromModel(String name, Model model) {
466
475
m .classVarName = toVarName (name );
467
476
m .modelJson = Json .pretty (model );
468
477
m .externalDocs = model .getExternalDocs ();
469
- int count = 0 ;
470
478
if (model instanceof ArrayModel ) {
471
479
ArrayModel am = (ArrayModel ) model ;
472
480
ArrayProperty arrayProperty = new ArrayProperty (am .getItems ());
473
- CodegenProperty cp = fromProperty (name , arrayProperty );
474
- if (cp .complexType != null && !defaultIncludes .contains (cp .complexType ))
475
- m .imports .add (cp .complexType );
476
- m .parent = toInstantiationType (arrayProperty );
477
- String containerType = cp .containerType ;
478
- if (instantiationTypes .containsKey (containerType ))
479
- m .imports .add (instantiationTypes .get (containerType ));
480
- if (typeMapping .containsKey (containerType )) {
481
- containerType = typeMapping .get (containerType );
482
- cp .containerType = containerType ;
483
- m .imports .add (containerType );
484
- }
481
+ addParentContainer (m , name , arrayProperty );
485
482
}
486
483
else if (model instanceof RefModel ) {
487
484
// TODO
488
- }
489
- else {
485
+ } else if (model instanceof ComposedModel ) {
486
+ final ComposedModel composed = (ComposedModel ) model ;
487
+ final RefModel parent = (RefModel ) composed .getParent ();
488
+ final String parentModel = toModelName (parent .getSimpleRef ());
489
+ m .parent = parentModel ;
490
+ addImport (m , parentModel );
491
+ final ModelImpl child = (ModelImpl ) composed .getChild ();
492
+ addVars (m , child .getProperties (), child .getRequired ());
493
+ } else {
490
494
ModelImpl impl = (ModelImpl ) model ;
491
495
if (impl .getAdditionalProperties () != null ) {
492
496
MapProperty mapProperty = new MapProperty (impl .getAdditionalProperties ());
493
- CodegenProperty cp = fromProperty (name , mapProperty );
494
- if (cp .complexType != null && !defaultIncludes .contains (cp .complexType ))
495
- m .imports .add (cp .complexType );
496
- m .parent = toInstantiationType (mapProperty );
497
- String containerType = cp .containerType ;
498
- if (instantiationTypes .containsKey (containerType ))
499
- m .imports .add (instantiationTypes .get (containerType ));
500
- if (typeMapping .containsKey (containerType )) {
501
- containerType = typeMapping .get (containerType );
502
- cp .containerType = containerType ;
503
- m .imports .add (containerType );
504
- }
505
- }
506
- if (impl .getProperties () != null && impl .getProperties ().size () > 0 ) {
507
- m .hasVars = true ;
508
- m .hasEnums = false ;
509
- for (String key : impl .getProperties ().keySet ()) {
510
- Property prop = impl .getProperties ().get (key );
511
-
512
- if (prop == null ) {
513
- LOGGER .warn ("null property for " + key );
514
- }
515
- else {
516
- CodegenProperty cp ;
517
- try {
518
- cp = fromProperty (key , prop );
519
- }
520
- catch (Exception e ) {
521
- System .out .println ("failed to process model " + name );
522
- throw new RuntimeException (e );
523
- }
524
- cp .required = null ;
525
- if (impl .getRequired () != null ) {
526
- for (String req : impl .getRequired ()) {
527
- if (key .equals (req ))
528
- cp .required = true ;
529
- }
530
- }
531
- if (cp .complexType != null && !defaultIncludes .contains (cp .complexType )) {
532
- m .imports .add (cp .complexType );
533
- }
534
- m .vars .add (cp );
535
- count += 1 ;
536
- if (cp .isEnum )
537
- m .hasEnums = true ;
538
- if (count != impl .getProperties ().keySet ().size ())
539
- cp .hasMore = new Boolean (true );
540
- if (cp .isContainer != null ) {
541
- String arrayImport = typeMapping .get ("array" );
542
- if (arrayImport != null &&
543
- !languageSpecificPrimitives .contains (arrayImport ) &&
544
- !defaultIncludes .contains (arrayImport ))
545
- m .imports .add (arrayImport );
546
- }
547
-
548
- if (cp .complexType != null &&
549
- !languageSpecificPrimitives .contains (cp .complexType ) &&
550
- !defaultIncludes .contains (cp .complexType ))
551
- m .imports .add (cp .complexType );
552
-
553
- if (cp .baseType != null &&
554
- !languageSpecificPrimitives .contains (cp .baseType ) &&
555
- !defaultIncludes .contains (cp .baseType ))
556
- m .imports .add (cp .baseType );
557
- }
558
- }
559
- }
560
- else {
561
- m .emptyVars = true ;
497
+ addParentContainer (m , name , mapProperty );
562
498
}
499
+ addVars (m , impl .getProperties (), impl .getRequired ());
563
500
}
564
501
return m ;
565
502
}
@@ -718,6 +655,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
718
655
operationId = builder .toString ();
719
656
LOGGER .warn ("generated operationId " + operationId );
720
657
}
658
+ operationId = removeNonNameElementToCamelCase (operationId );
721
659
op .path = path ;
722
660
op .operationId = toOperationId (operationId );
723
661
op .summary = escapeText (operation .getSummary ());
@@ -1141,6 +1079,62 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
1141
1079
co .baseName = tag ;
1142
1080
}
1143
1081
1082
+ private void addParentContainer (CodegenModel m , String name , Property property ) {
1083
+ final CodegenProperty tmp = fromProperty (name , property );
1084
+ addImport (m , tmp .complexType );
1085
+ m .parent = toInstantiationType (property );
1086
+ final String containerType = tmp .containerType ;
1087
+ final String instantiationType = instantiationTypes .get (containerType );
1088
+ if (instantiationType != null ) {
1089
+ addImport (m , instantiationType );
1090
+ }
1091
+ final String mappedType = typeMapping .get (containerType );
1092
+ if (mappedType != null ) {
1093
+ addImport (m , mappedType );
1094
+ }
1095
+ }
1096
+
1097
+ private void addImport (CodegenModel m , String type ) {
1098
+ if (type != null && !languageSpecificPrimitives .contains (type ) && !defaultIncludes .contains (type )) {
1099
+ m .imports .add (type );
1100
+ }
1101
+ }
1102
+
1103
+ private void addVars (CodegenModel m , Map <String , Property > properties , Collection <String > required ) {
1104
+ if (properties != null && properties .size () > 0 ) {
1105
+ m .hasVars = true ;
1106
+ m .hasEnums = false ;
1107
+ final int totalCount = properties .size ();
1108
+ final Set <String > mandatory = required == null ? Collections .<String > emptySet () : new HashSet <String >(required );
1109
+ int count = 0 ;
1110
+ for (Map .Entry <String , Property > entry : properties .entrySet ()) {
1111
+ final String key = entry .getKey ();
1112
+ final Property prop = entry .getValue ();
1113
+
1114
+ if (prop == null ) {
1115
+ LOGGER .warn ("null property for " + key );
1116
+ } else {
1117
+ final CodegenProperty cp = fromProperty (key , prop );
1118
+ cp .required = mandatory .contains (key ) ? true : null ;
1119
+ if (cp .isEnum ) {
1120
+ m .hasEnums = true ;
1121
+ }
1122
+ count += 1 ;
1123
+ if (count != totalCount )
1124
+ cp .hasMore = true ;
1125
+ if (cp .isContainer != null ) {
1126
+ addImport (m , typeMapping .get ("array" ));
1127
+ }
1128
+ addImport (m , cp .baseType );
1129
+ addImport (m , cp .complexType );
1130
+ m .vars .add (cp );
1131
+ }
1132
+ }
1133
+ } else {
1134
+ m .emptyVars = true ;
1135
+ }
1136
+ }
1137
+
1144
1138
/* underscore and camelize are copied from Twitter elephant bird
1145
1139
* https://github.com/twitter/elephant-bird/blob/master/core/src/main/java/com/twitter/elephantbird/util/Strings.java
1146
1140
*/
@@ -1166,6 +1160,26 @@ public static String underscore(String word) {
1166
1160
return word ;
1167
1161
}
1168
1162
1163
+ /**
1164
+ * Remove characters not suitable for variable or method name from the input and camelize it
1165
+ * @param name
1166
+ * @return
1167
+ */
1168
+ public String removeNonNameElementToCamelCase (String name ) {
1169
+ String nonNameElementPattern = "[-_:;#]" ;
1170
+ name = StringUtils .join (Lists .transform (Lists .newArrayList (name .split (nonNameElementPattern )), new Function <String , String >() {
1171
+ @ Nullable
1172
+ @ Override
1173
+ public String apply (String input ) {
1174
+ return StringUtils .capitalize (input );
1175
+ }
1176
+ }), "" );
1177
+ if (name .length () > 0 ) {
1178
+ name = name .substring (0 , 1 ).toLowerCase () + name .substring (1 );
1179
+ }
1180
+ return name ;
1181
+ }
1182
+
1169
1183
public static String camelize (String word ) {
1170
1184
return camelize (word , false );
1171
1185
}
0 commit comments