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,45 @@ 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
}
497
+ uniqueParamsByName .put (paramNameType , param );
495
498
}
496
499
} else {
497
- while (modelNames . containsKey (paramNameType )) {
500
+ while (typeNames . contains (paramNameType )) {
498
501
paramNameType = generateNextName (paramNameType );
499
502
}
500
- uniqueOptionalParamsByName .put (paramNameType , param );
503
+ uniqueParamsByName .put (paramNameType , param );
501
504
}
502
505
503
506
param .vendorExtensions .put ("x-paramNameType" , paramNameType );
504
- op . vendorExtensions . put ( "x-hasBodyOrFormParam" , op . getHasBodyParam () || op . getHasFormParams () );
507
+ typeNames . add ( paramNameType );
505
508
}
506
509
}
507
510
if (op .getHasPathParams ()) {
@@ -572,7 +575,18 @@ public CodegenOperation fromOperation(String resourcePath, String httpMethod, Op
572
575
573
576
return op ;
574
577
}
575
-
578
+
579
+ public List <CodegenSecurity > fromSecurity (Map <String , SecuritySchemeDefinition > schemes ) {
580
+ List <CodegenSecurity > secs = super .fromSecurity (schemes );
581
+ for (CodegenSecurity sec : secs ) {
582
+ String prefix = "" ;
583
+ if (sec .isBasic ) prefix = "AuthBasic" ;
584
+ if (sec .isApiKey ) prefix = "AuthApiKey" ;
585
+ if (sec .isOAuth ) prefix = "AuthOAuth" ;
586
+ sec .name = prefix + toTypeName ("" ,sec .name );
587
+ }
588
+ return secs ;
589
+ }
576
590
577
591
@ Override
578
592
public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
@@ -586,6 +600,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
586
600
587
601
additionalProperties .put ("x-hasUnknownMimeTypes" , !unknownMimeTypes .isEmpty ());
588
602
additionalProperties .put ("x-unknownMimeTypes" , unknownMimeTypes );
603
+ additionalProperties .put ("x-allUniqueParams" , uniqueParamsByName .values ());
589
604
590
605
return ret ;
591
606
}
@@ -619,12 +634,13 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> o
619
634
public CodegenModel fromModel (String name , Model mod , Map <String , Model > allDefinitions ) {
620
635
CodegenModel model = super .fromModel (name , mod , allDefinitions );
621
636
622
- while (uniqueOptionalParamsByName . containsKey (model .classname )) {
637
+ while (typeNames . contains (model .classname )) {
623
638
model .classname = generateNextName (model .classname );
624
639
}
640
+ typeNames .add (model .classname );
625
641
626
642
// From the model name, compute the prefix for the fields.
627
- String prefix = WordUtils .uncapitalize (model .classname );
643
+ String prefix = StringUtils .uncapitalize (model .classname );
628
644
for (CodegenProperty prop : model .vars ) {
629
645
prop .name = toVarName (prefix , prop .name );
630
646
}
@@ -635,7 +651,6 @@ public CodegenModel fromModel(String name, Model mod, Map<String, Model> allDefi
635
651
return model ;
636
652
}
637
653
638
- modelNames .put (model .classname , model );
639
654
return model ;
640
655
}
641
656
@@ -674,6 +689,7 @@ private void processMediaType(CodegenOperation op, Map<String, String> m) {
674
689
if (StringUtils .isBlank (mediaType )) return ;
675
690
676
691
String mimeType = getMimeDataType (mediaType );
692
+ typeNames .add (mimeType );
677
693
m .put (MEDIA_DATA_TYPE , mimeType );
678
694
if (isJsonMimeType (mediaType )) {
679
695
m .put (MEDIA_IS_JSON , "true" );
@@ -761,6 +777,7 @@ private static boolean isMultipartOperation(List<Map<String, String>> consumes)
761
777
}
762
778
return false ;
763
779
}
780
+
764
781
@ Override
765
782
public String toVarName (String name ) {
766
783
return toVarName ("" , name );
@@ -794,8 +811,28 @@ public String toModelFilename(String name) {
794
811
return toTypeName ("Model" , name );
795
812
}
796
813
public String toTypeName (String prefix , String name ) {
797
- name = camelize (underscore (sanitizeName (name )));
798
-
814
+ name = escapeIdentifier (prefix , camelize (sanitizeName (name )));
815
+ return name ;
816
+ }
817
+ @ Override
818
+ public String toOperationId (String operationId ) {
819
+ if (StringUtils .isEmpty (operationId )) {
820
+ throw new RuntimeException ("Empty method/operation name (operationId) not allowed" );
821
+ }
822
+ operationId = escapeIdentifier ("op" ,camelize (sanitizeName (operationId ), true ));
823
+ String uniqueName = operationId ;
824
+ String uniqueNameType = toTypeName ("Op" , operationId );
825
+ while (typeNames .contains (uniqueNameType )) {
826
+ uniqueName = generateNextName (uniqueName );
827
+ uniqueNameType = toTypeName ("Op" , uniqueName );
828
+ }
829
+ typeNames .add (uniqueNameType );
830
+ if (!operationId .equals (uniqueName )) {
831
+ LOGGER .warn ("generated unique operationId `" + uniqueName + "`" );
832
+ }
833
+ return uniqueName ;
834
+ }
835
+ public String escapeIdentifier (String prefix , String name ) {
799
836
if (StringUtils .isBlank (prefix )) return name ;
800
837
801
838
if (isReservedWord (name )) {
@@ -815,4 +852,65 @@ public String toTypeName(String prefix, String name) {
815
852
static boolean isJsonMimeType (String mime ) {
816
853
return mime != null && JSON_MIME_PATTERN .matcher (mime ).matches ();
817
854
}
855
+
856
+ @ Override
857
+ public String toDefaultValue (Property p ) {
858
+ if (p instanceof StringProperty ) {
859
+ StringProperty dp = (StringProperty ) p ;
860
+ if (dp .getDefault () != null ) {
861
+ return "\" " + escapeText (dp .getDefault ()) + "\" " ;
862
+ }
863
+ } else if (p instanceof BooleanProperty ) {
864
+ BooleanProperty dp = (BooleanProperty ) p ;
865
+ if (dp .getDefault () != null ) {
866
+ if (dp .getDefault ().toString ().equalsIgnoreCase ("false" ))
867
+ return "False" ;
868
+ else
869
+ return "True" ;
870
+ }
871
+ } else if (p instanceof DoubleProperty ) {
872
+ DoubleProperty dp = (DoubleProperty ) p ;
873
+ if (dp .getDefault () != null ) {
874
+ return dp .getDefault ().toString ();
875
+ }
876
+ } else if (p instanceof FloatProperty ) {
877
+ FloatProperty dp = (FloatProperty ) p ;
878
+ if (dp .getDefault () != null ) {
879
+ return dp .getDefault ().toString ();
880
+ }
881
+ } else if (p instanceof IntegerProperty ) {
882
+ IntegerProperty dp = (IntegerProperty ) p ;
883
+ if (dp .getDefault () != null ) {
884
+ return dp .getDefault ().toString ();
885
+ }
886
+ } else if (p instanceof LongProperty ) {
887
+ LongProperty dp = (LongProperty ) p ;
888
+ if (dp .getDefault () != null ) {
889
+ return dp .getDefault ().toString ();
890
+ }
891
+ }
892
+
893
+ return null ;
894
+ }
895
+
896
+ // override with any special text escaping logic
897
+ @ SuppressWarnings ("static-method" )
898
+ public String escapeText (String input ) {
899
+ if (input == null ) {
900
+ return input ;
901
+ }
902
+
903
+ // remove \t, \n, \r
904
+ // replace \ with \\
905
+ // replace " with \"
906
+ // outter unescape to retain the original multi-byte characters
907
+ // finally escalate characters avoiding code injection
908
+ return escapeUnsafeCharacters (
909
+ StringEscapeUtils .unescapeJava (
910
+ StringEscapeUtils .escapeJava (input )
911
+ .replace ("\\ /" , "/" ))
912
+ .replaceAll ("[\\ t\\ n\\ r]" ," " )
913
+ .replace ("\\ " , "\\ \\ " )
914
+ .replace ("\" " , "\\ \" " ));
915
+ }
818
916
}
0 commit comments