5
5
import io .swagger .models .ModelImpl ;
6
6
import io .swagger .models .Operation ;
7
7
import io .swagger .models .Swagger ;
8
- import io .swagger .models .properties .ArrayProperty ;
9
- import io .swagger .models .properties .MapProperty ;
10
- import io .swagger .models .properties .Property ;
8
+ import io .swagger .models .properties .*;
11
9
12
10
import java .util .*;
13
11
import java .util .regex .Pattern ;
14
12
15
13
import org .apache .commons .io .FileUtils ;
16
14
15
+ import io .swagger .models .auth .SecuritySchemeDefinition ;
17
16
import io .swagger .codegen .CliOption ;
18
17
import io .swagger .codegen .CodegenConstants ;
19
18
import io .swagger .codegen .CodegenModel ;
26
25
import java .io .File ;
27
26
28
27
import org .apache .commons .lang3 .StringUtils ;
28
+ import org .apache .commons .lang3 .StringEscapeUtils ;
29
29
import org .apache .commons .lang3 .text .WordUtils ;
30
30
31
31
import java .util .regex .Matcher ;
@@ -65,8 +65,8 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC
65
65
static final String MEDIA_IS_JSON = "x-mediaIsJson" ;
66
66
67
67
68
- protected Map <String , CodegenParameter > uniqueOptionalParamsByName = new HashMap <String , CodegenParameter >();
69
- protected Map <String , CodegenModel > modelNames = new HashMap <String , CodegenModel >();
68
+ protected Map <String , CodegenParameter > uniqueParamsByName = new HashMap <String , CodegenParameter >();
69
+ protected Set <String > typeNames = new HashSet <String >();
70
70
protected Map <String , Map <String ,String >> allMimeTypes = new HashMap <String , Map <String ,String >>();
71
71
protected Map <String , String > knownMimeDataTypes = new HashMap <String , String >();
72
72
protected Map <String , Set <String >> modelMimeTypes = new HashMap <String , Set <String >>();
@@ -466,42 +466,44 @@ public String toInstantiationType(Property p) {
466
466
public CodegenOperation fromOperation (String resourcePath , String httpMethod , Operation operation , Map <String , Model > definitions , Swagger swagger ) {
467
467
CodegenOperation op = super .fromOperation (resourcePath , httpMethod , operation , definitions , swagger );
468
468
469
- op .vendorExtensions .put ("x-baseOperationId" , op .operationId );
469
+ String operationType = toTypeName ("Op" , op .operationId );
470
+ op .vendorExtensions .put ("x-operationType" , operationType );
471
+ typeNames .add (operationType );
472
+
470
473
op .vendorExtensions .put ("x-haddockPath" , String .format ("%s %s" , op .httpMethod , op .path .replace ("/" , "\\ /" )));
471
- op .operationId = toVarName (op .operationId );
472
- op .vendorExtensions .put ("x-operationType" , toTypeName ("Op" , op .operationId ));
473
474
op .vendorExtensions .put ("x-hasBodyOrFormParam" , op .getHasBodyParam () || op .getHasFormParams ());
474
475
475
476
for (CodegenParameter param : op .allParams ) {
476
- param .vendorExtensions .put ("x-operationType" , WordUtils . capitalize ( op . operationId ) );
477
+ param .vendorExtensions .put ("x-operationType" , operationType );
477
478
param .vendorExtensions .put ("x-isBodyOrFormParam" , param .isBodyParam || param .isFormParam );
478
479
if (!StringUtils .isBlank (param .collectionFormat )) {
479
480
param .vendorExtensions .put ("x-collectionFormat" , mapCollectionFormat (param .collectionFormat ));
480
481
}
481
- if (!param .required ) {
482
+ if (!param .required ) {
482
483
op .vendorExtensions .put ("x-hasOptionalParams" , true );
483
-
484
+ }
485
+ if (typeMapping .containsKey (param .dataType ) || param .isPrimitiveType || param .isListContainer || param .isMapContainer || param .isFile ) {
484
486
String paramNameType = toTypeName ("Param" , param .paramName );
485
487
486
- if (uniqueOptionalParamsByName .containsKey (paramNameType )) {
487
- CodegenParameter lastParam = this .uniqueOptionalParamsByName .get (paramNameType );
488
+ if (uniqueParamsByName .containsKey (paramNameType )) {
489
+ CodegenParameter lastParam = this .uniqueParamsByName .get (paramNameType );
488
490
if (lastParam .dataType != null && lastParam .dataType .equals (param .dataType )) {
489
491
param .vendorExtensions .put ("x-duplicate" , true );
490
492
} else {
491
493
paramNameType = paramNameType + param .dataType ;
492
- while (modelNames . containsKey (paramNameType )) {
494
+ while (typeNames . contains (paramNameType )) {
493
495
paramNameType = generateNextName (paramNameType );
494
496
}
495
497
}
496
498
} else {
497
- while (modelNames . containsKey (paramNameType )) {
499
+ while (typeNames . contains (paramNameType )) {
498
500
paramNameType = generateNextName (paramNameType );
499
501
}
500
- uniqueOptionalParamsByName .put (paramNameType , param );
502
+ uniqueParamsByName .put (paramNameType , param );
501
503
}
502
504
503
505
param .vendorExtensions .put ("x-paramNameType" , paramNameType );
504
- op . vendorExtensions . put ( "x-hasBodyOrFormParam" , op . getHasBodyParam () || op . getHasFormParams () );
506
+ typeNames . add ( paramNameType );
505
507
}
506
508
}
507
509
if (op .getHasPathParams ()) {
@@ -572,7 +574,18 @@ public CodegenOperation fromOperation(String resourcePath, String httpMethod, Op
572
574
573
575
return op ;
574
576
}
575
-
577
+
578
+ public List <CodegenSecurity > fromSecurity (Map <String , SecuritySchemeDefinition > schemes ) {
579
+ List <CodegenSecurity > secs = super .fromSecurity (schemes );
580
+ for (CodegenSecurity sec : secs ) {
581
+ String prefix = "" ;
582
+ if (sec .isBasic ) prefix = "AuthBasic" ;
583
+ if (sec .isApiKey ) prefix = "AuthApiKey" ;
584
+ if (sec .isOAuth ) prefix = "AuthOAuth" ;
585
+ sec .name = prefix + toTypeName ("" ,sec .name );
586
+ }
587
+ return secs ;
588
+ }
576
589
577
590
@ Override
578
591
public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
@@ -619,12 +632,13 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
619
632
public CodegenModel fromModel (String name , Model mod , Map <String , Model > allDefinitions ) {
620
633
CodegenModel model = super .fromModel (name , mod , allDefinitions );
621
634
622
- while (uniqueOptionalParamsByName . containsKey (model .classname )) {
635
+ while (typeNames . contains (model .classname )) {
623
636
model .classname = generateNextName (model .classname );
624
637
}
638
+ typeNames .add (model .classname );
625
639
626
640
// From the model name, compute the prefix for the fields.
627
- String prefix = WordUtils .uncapitalize (model .classname );
641
+ String prefix = StringUtils .uncapitalize (model .classname );
628
642
for (CodegenProperty prop : model .vars ) {
629
643
prop .name = toVarName (prefix , prop .name );
630
644
}
@@ -635,7 +649,6 @@ public CodegenModel fromModel(String name, Model mod, Map<String, Model> allDefi
635
649
return model ;
636
650
}
637
651
638
- modelNames .put (model .classname , model );
639
652
return model ;
640
653
}
641
654
@@ -674,6 +687,7 @@ private void processMediaType(CodegenOperation op, Map<String, String> m) {
674
687
if (StringUtils .isBlank (mediaType )) return ;
675
688
676
689
String mimeType = getMimeDataType (mediaType );
690
+ typeNames .add (mimeType );
677
691
m .put (MEDIA_DATA_TYPE , mimeType );
678
692
if (isJsonMimeType (mediaType )) {
679
693
m .put (MEDIA_IS_JSON , "true" );
@@ -761,6 +775,7 @@ private static boolean isMultipartOperation(List<Map<String, String>> consumes)
761
775
}
762
776
return false ;
763
777
}
778
+
764
779
@ Override
765
780
public String toVarName (String name ) {
766
781
return toVarName ("" , name );
@@ -794,8 +809,28 @@ public String toModelFilename(String name) {
794
809
return toTypeName ("Model" , name );
795
810
}
796
811
public String toTypeName (String prefix , String name ) {
797
- name = camelize (underscore (sanitizeName (name )));
798
-
812
+ name = escapeIdentifier (prefix , camelize (sanitizeName (name )));
813
+ return name ;
814
+ }
815
+ @ Override
816
+ public String toOperationId (String operationId ) {
817
+ if (StringUtils .isEmpty (operationId )) {
818
+ throw new RuntimeException ("Empty method/operation name (operationId) not allowed" );
819
+ }
820
+ operationId = escapeIdentifier ("op" ,camelize (sanitizeName (operationId ), true ));
821
+ String uniqueName = operationId ;
822
+ String uniqueNameType = toTypeName ("Op" , operationId );
823
+ while (typeNames .contains (uniqueNameType )) {
824
+ uniqueName = generateNextName (uniqueName );
825
+ uniqueNameType = toTypeName ("Op" , uniqueName );
826
+ }
827
+ typeNames .add (uniqueNameType );
828
+ if (!operationId .equals (uniqueName )) {
829
+ LOGGER .warn ("generated unique operationId `" + uniqueName + "`" );
830
+ }
831
+ return uniqueName ;
832
+ }
833
+ public String escapeIdentifier (String prefix , String name ) {
799
834
if (StringUtils .isBlank (prefix )) return name ;
800
835
801
836
if (isReservedWord (name )) {
@@ -815,4 +850,65 @@ public String toTypeName(String prefix, String name) {
815
850
static boolean isJsonMimeType (String mime ) {
816
851
return mime != null && JSON_MIME_PATTERN .matcher (mime ).matches ();
817
852
}
853
+
854
+ @ Override
855
+ public String toDefaultValue (Property p ) {
856
+ if (p instanceof StringProperty ) {
857
+ StringProperty dp = (StringProperty ) p ;
858
+ if (dp .getDefault () != null ) {
859
+ return "\" " + escapeText (dp .getDefault ()) + "\" " ;
860
+ }
861
+ } else if (p instanceof BooleanProperty ) {
862
+ BooleanProperty dp = (BooleanProperty ) p ;
863
+ if (dp .getDefault () != null ) {
864
+ if (dp .getDefault ().toString ().equalsIgnoreCase ("false" ))
865
+ return "False" ;
866
+ else
867
+ return "True" ;
868
+ }
869
+ } else if (p instanceof DoubleProperty ) {
870
+ DoubleProperty dp = (DoubleProperty ) p ;
871
+ if (dp .getDefault () != null ) {
872
+ return dp .getDefault ().toString ();
873
+ }
874
+ } else if (p instanceof FloatProperty ) {
875
+ FloatProperty dp = (FloatProperty ) p ;
876
+ if (dp .getDefault () != null ) {
877
+ return dp .getDefault ().toString ();
878
+ }
879
+ } else if (p instanceof IntegerProperty ) {
880
+ IntegerProperty dp = (IntegerProperty ) p ;
881
+ if (dp .getDefault () != null ) {
882
+ return dp .getDefault ().toString ();
883
+ }
884
+ } else if (p instanceof LongProperty ) {
885
+ LongProperty dp = (LongProperty ) p ;
886
+ if (dp .getDefault () != null ) {
887
+ return dp .getDefault ().toString ();
888
+ }
889
+ }
890
+
891
+ return null ;
892
+ }
893
+
894
+ // override with any special text escaping logic
895
+ @ SuppressWarnings ("static-method" )
896
+ public String escapeText (String input ) {
897
+ if (input == null ) {
898
+ return input ;
899
+ }
900
+
901
+ // remove \t, \n, \r
902
+ // replace \ with \\
903
+ // replace " with \"
904
+ // outter unescape to retain the original multi-byte characters
905
+ // finally escalate characters avoiding code injection
906
+ return escapeUnsafeCharacters (
907
+ StringEscapeUtils .unescapeJava (
908
+ StringEscapeUtils .escapeJava (input )
909
+ .replace ("\\ /" , "/" ))
910
+ .replaceAll ("[\\ t\\ n\\ r]" ," " )
911
+ .replace ("\\ " , "\\ \\ " )
912
+ .replace ("\" " , "\\ \" " ));
913
+ }
818
914
}
0 commit comments