26
26
27
27
import org .apache .commons .lang3 .StringUtils ;
28
28
import org .apache .commons .lang3 .StringEscapeUtils ;
29
- import org .apache .commons .lang3 .text .WordUtils ;
30
29
31
30
import java .util .regex .Matcher ;
32
31
33
32
public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenConfig {
34
33
35
34
// source folder where to write the files
36
35
protected String sourceFolder = "src" ;
37
- protected String apiVersion = "0.0.1" ;
38
36
39
37
protected String artifactId = "swagger-haskell-http-client" ;
40
38
protected String artifactVersion = "1.0.0" ;
@@ -67,7 +65,6 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC
67
65
68
66
protected Map <String , CodegenParameter > uniqueParamsByName = new HashMap <String , CodegenParameter >();
69
67
protected Set <String > typeNames = new HashSet <String >();
70
- protected Map <String , Map <String ,String >> allMimeTypes = new HashMap <String , Map <String ,String >>();
71
68
protected Map <String , String > knownMimeDataTypes = new HashMap <String , String >();
72
69
protected Map <String , Set <String >> modelMimeTypes = new HashMap <String , Set <String >>();
73
70
protected String lastTag = "" ;
@@ -124,7 +121,6 @@ public HaskellHttpClientCodegen() {
124
121
)
125
122
);
126
123
127
- additionalProperties .put ("apiVersion" , apiVersion );
128
124
additionalProperties .put ("artifactId" , artifactId );
129
125
additionalProperties .put ("artifactVersion" , artifactVersion );
130
126
@@ -398,13 +394,7 @@ public void preprocessSwagger(Swagger swagger) {
398
394
additionalProperties .put ("configType" , apiName + "Config" );
399
395
additionalProperties .put ("swaggerVersion" , swagger .getSwagger ());
400
396
401
- //copy input swagger to output folder
402
- try {
403
- String swaggerJson = Json .pretty (swagger );
404
- FileUtils .writeStringToFile (new File (outputFolder + File .separator + "swagger.json" ), swaggerJson );
405
- } catch (IOException e ) {
406
- throw new RuntimeException (e .getMessage (), e .getCause ());
407
- }
397
+ WriteInputSwaggerToFile (swagger );
408
398
409
399
super .preprocessSwagger (swagger );
410
400
}
@@ -461,7 +451,6 @@ public String toInstantiationType(Property p) {
461
451
return null ;
462
452
}
463
453
}
464
-
465
454
@ Override
466
455
public CodegenOperation fromOperation (String resourcePath , String httpMethod , Operation operation , Map <String , Model > definitions , Swagger swagger ) {
467
456
CodegenOperation op = super .fromOperation (resourcePath , httpMethod , operation , definitions , swagger );
@@ -486,100 +475,20 @@ public CodegenOperation fromOperation(String resourcePath, String httpMethod, Op
486
475
if (!param .required ) {
487
476
op .vendorExtensions .put ("x-hasOptionalParams" , true );
488
477
}
489
- if (typeMapping .containsKey (param .dataType ) || param .isPrimitiveType || param .isListContainer || param .isMapContainer || param .isFile ) {
490
- String paramNameType = toTypeName ("Param" , param .paramName );
491
-
492
- if (uniqueParamsByName .containsKey (paramNameType )) {
493
- CodegenParameter lastParam = this .uniqueParamsByName .get (paramNameType );
494
- if (lastParam .dataType != null && lastParam .dataType .equals (param .dataType )) {
495
- param .vendorExtensions .put ("x-duplicate" , true );
496
- } else {
497
- paramNameType = paramNameType + param .dataType ;
498
- while (typeNames .contains (paramNameType )) {
499
- paramNameType = generateNextName (paramNameType );
500
- }
501
- uniqueParamsByName .put (paramNameType , param );
502
- }
503
- } else {
504
- while (typeNames .contains (paramNameType )) {
505
- paramNameType = generateNextName (paramNameType );
506
- }
507
- uniqueParamsByName .put (paramNameType , param );
508
- }
509
478
510
- param .vendorExtensions .put ("x-paramNameType" , paramNameType );
511
- typeNames .add (paramNameType );
512
- }
513
- }
514
- if (op .getHasPathParams ()) {
515
- String remainingPath = op .path ;
516
- for (CodegenParameter param : op .pathParams ) {
517
- String [] pieces = remainingPath .split ("\\ {" + param .baseName + "\\ }" );
518
- if (pieces .length == 0 )
519
- throw new RuntimeException ("paramName {" + param .baseName + "} not in path " + op .path );
520
- if (pieces .length > 2 )
521
- throw new RuntimeException ("paramName {" + param .baseName + "} found multiple times in path " + op .path );
522
- if (pieces .length == 2 ) {
523
- param .vendorExtensions .put ("x-pathPrefix" , pieces [0 ]);
524
- remainingPath = pieces [1 ];
525
- } else {
526
- if (remainingPath .startsWith ("{" + param .baseName + "}" )) {
527
- remainingPath = pieces [0 ];
528
- } else {
529
- param .vendorExtensions .put ("x-pathPrefix" , pieces [0 ]);
530
- remainingPath = "" ;
531
- }
532
- }
533
- }
534
- op .vendorExtensions .put ("x-hasPathParams" , true );
535
- if (remainingPath .length () > 0 ) {
536
- op .vendorExtensions .put ("x-pathSuffix" , remainingPath );
537
- }
538
- } else {
539
- op .vendorExtensions .put ("x-hasPathParams" , false );
540
- op .vendorExtensions .put ("x-pathSuffix" , op .path );
541
- }
542
- for (CodegenParameter param : op .queryParams ) {
543
- }
544
- for (CodegenParameter param : op .headerParams ) {
545
- }
546
- for (CodegenParameter param : op .bodyParams ) {
547
- }
548
- for (CodegenParameter param : op .formParams ) {
479
+ deduplicateParameter (param );
549
480
}
550
481
551
- if (op .hasConsumes ) {
552
- for (Map <String , String > m : op .consumes ) {
553
- processMediaType (op ,m );
554
- }
555
- if (isMultipartOperation (op .consumes )) {
556
- op .isMultipart = Boolean .TRUE ;
557
- }
558
- }
559
- if (op .hasProduces ) {
560
- for (Map <String , String > m : op .produces ) {
561
- processMediaType (op ,m );
562
- }
563
- }
482
+ processPathExpr (op );
564
483
565
- String returnType = op .returnType ;
566
- if (returnType == null || returnType .equals ("null" )) {
567
- if (op .hasProduces ) {
568
- returnType = "res" ;
569
- op .vendorExtensions .put ("x-hasUnknownReturn" , true );
570
- } else {
571
- returnType = "NoContent" ;
572
- }
573
- }
574
- if (returnType .indexOf (" " ) >= 0 ) {
575
- returnType = "(" + returnType + ")" ;
576
- }
577
- op .vendorExtensions .put ("x-returnType" , returnType );
484
+ processProducesConsumes (op );
578
485
486
+ processReturnType (op );
579
487
580
488
return op ;
581
489
}
582
-
490
+
491
+ @ Override
583
492
public List <CodegenSecurity > fromSecurity (Map <String , SecuritySchemeDefinition > schemes ) {
584
493
List <CodegenSecurity > secs = super .fromSecurity (schemes );
585
494
for (CodegenSecurity sec : secs ) {
@@ -603,8 +512,26 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
603
512
}
604
513
605
514
additionalProperties .put ("x-hasUnknownMimeTypes" , !unknownMimeTypes .isEmpty ());
515
+
516
+ Collections .sort (unknownMimeTypes , new Comparator <Map <String , String >>() {
517
+ @ Override
518
+ public int compare (Map <String , String > o1 , Map <String , String > o2 ) {
519
+ return o1 .get (MEDIA_TYPE ).compareTo (o2 .get (MEDIA_TYPE ));
520
+ }
521
+ });
606
522
additionalProperties .put ("x-unknownMimeTypes" , unknownMimeTypes );
607
- additionalProperties .put ("x-allUniqueParams" , uniqueParamsByName .values ());
523
+
524
+ ArrayList <CodegenParameter > params = new ArrayList <>(uniqueParamsByName .values ());
525
+ Collections .sort (params , new Comparator <CodegenParameter >() {
526
+ @ Override
527
+ public int compare (CodegenParameter o1 , CodegenParameter o2 ) {
528
+ return
529
+ ((String ) o1 .vendorExtensions .get ("x-paramNameType" ))
530
+ .compareTo (
531
+ (String ) o2 .vendorExtensions .get ("x-paramNameType" ));
532
+ }
533
+ });
534
+ additionalProperties .put ("x-allUniqueParams" , params );
608
535
609
536
return ret ;
610
537
}
@@ -687,10 +614,114 @@ public boolean isDataTypeBinary(final String dataType) {
687
614
return dataType != null && dataType .equals ("B.ByteString" );
688
615
}
689
616
617
+ //copy input swagger to output folder
618
+ private void WriteInputSwaggerToFile (Swagger swagger ) {
619
+ try {
620
+ String swaggerJson = Json .pretty (swagger );
621
+ FileUtils .writeStringToFile (new File (outputFolder + File .separator + "swagger.json" ), swaggerJson );
622
+ } catch (IOException e ) {
623
+ throw new RuntimeException (e .getMessage (), e .getCause ());
624
+ }
625
+ }
626
+
627
+ private void processReturnType (CodegenOperation op ) {
628
+ String returnType = op .returnType ;
629
+ if (returnType == null || returnType .equals ("null" )) {
630
+ if (op .hasProduces ) {
631
+ returnType = "res" ;
632
+ op .vendorExtensions .put ("x-hasUnknownReturn" , true );
633
+ } else {
634
+ returnType = "NoContent" ;
635
+ }
636
+ }
637
+ if (returnType .indexOf (" " ) >= 0 ) {
638
+ returnType = "(" + returnType + ")" ;
639
+ }
640
+ op .vendorExtensions .put ("x-returnType" , returnType );
641
+ }
642
+
643
+ private void processProducesConsumes (CodegenOperation op ) {
644
+ if (op .hasConsumes ) {
645
+ for (Map <String , String > m : op .consumes ) {
646
+ processMediaType (op ,m );
647
+ }
648
+ if (isMultipartOperation (op .consumes )) {
649
+ op .isMultipart = Boolean .TRUE ;
650
+ }
651
+ }
652
+ if (op .hasProduces ) {
653
+ for (Map <String , String > m : op .produces ) {
654
+ processMediaType (op ,m );
655
+ }
656
+ }
657
+ }
658
+
659
+ private void deduplicateParameter (CodegenParameter param ) {
660
+ if (typeMapping .containsKey (param .dataType ) || param .isPrimitiveType || param .isListContainer || param .isMapContainer || param .isFile ) {
661
+
662
+ String paramNameType = toTypeName ("Param" , param .paramName );
663
+
664
+ if (uniqueParamsByName .containsKey (paramNameType )) {
665
+ if (!checkParamForDuplicates (paramNameType , param )) {
666
+ paramNameType = paramNameType + param .dataType ;
667
+ if (!checkParamForDuplicates (paramNameType , param )) {
668
+ while (typeNames .contains (paramNameType )) {
669
+ paramNameType = generateNextName (paramNameType );
670
+ if (checkParamForDuplicates (paramNameType , param )) {
671
+ break ;
672
+ }
673
+ }
674
+ }
675
+
676
+ uniqueParamsByName .put (paramNameType , param );
677
+ }
678
+ } else {
679
+
680
+ while (typeNames .contains (paramNameType )) {
681
+ paramNameType = generateNextName (paramNameType );
682
+ if (checkParamForDuplicates (paramNameType , param )) {
683
+ break ;
684
+ }
685
+ }
686
+
687
+ uniqueParamsByName .put (paramNameType , param );
688
+ }
689
+
690
+ param .vendorExtensions .put ("x-paramNameType" , paramNameType );
691
+ typeNames .add (paramNameType );
692
+ }
693
+ }
694
+
695
+ public Boolean checkParamForDuplicates (String paramNameType , CodegenParameter param ) {
696
+ CodegenParameter lastParam = this .uniqueParamsByName .get (paramNameType );
697
+ if (lastParam != null && lastParam .dataType != null && lastParam .dataType .equals (param .dataType )) {
698
+ param .vendorExtensions .put ("x-duplicate" , true );
699
+ return true ;
700
+ }
701
+ return false ;
702
+ }
703
+
704
+ // build the parameterized path segments, according to pathParams
705
+ private void processPathExpr (CodegenOperation op ) {
706
+ String xPath = "[\" " + escapeText (op .path ) + "\" ]" ;
707
+ if (op .getHasPathParams ()) {
708
+ for (CodegenParameter param : op .pathParams ) {
709
+ xPath = xPath .replaceAll ("\\ {" + param .baseName + "\\ }" , "\" ,toPath " + param .paramName + ",\" " );
710
+ }
711
+ xPath = xPath .replaceAll (",\" \" ," , "," );
712
+ xPath = xPath .replaceAll ("\" \" ," , "," );
713
+ xPath = xPath .replaceAll (",\" \" " , "," );
714
+ xPath = xPath .replaceAll ("^\\ [," , "[" );
715
+ xPath = xPath .replaceAll (",\\ ]$" , "]" );
716
+ }
717
+ op .vendorExtensions .put ("x-path" , xPath );
718
+ }
719
+
720
+
690
721
private void processMediaType (CodegenOperation op , Map <String , String > m ) {
691
722
String mediaType = m .get (MEDIA_TYPE );
692
723
693
- if (StringUtils .isBlank (mediaType )) return ;
724
+ if (StringUtils .isBlank (mediaType )) return ;
694
725
695
726
String mimeType = getMimeDataType (mediaType );
696
727
typeNames .add (mimeType );
@@ -699,8 +730,7 @@ private void processMediaType(CodegenOperation op, Map<String, String> m) {
699
730
m .put (MEDIA_IS_JSON , "true" );
700
731
}
701
732
702
- allMimeTypes .put (mediaType , m );
703
- if (!knownMimeDataTypes .containsKey (mediaType ) && !unknownMimeTypes .contains (m )) {
733
+ if (!knownMimeDataTypes .containsValue (mimeType ) && !unknownMimeTypesContainsType (mimeType )) {
704
734
unknownMimeTypes .add (m );
705
735
}
706
736
for (CodegenParameter param : op .allParams ) {
@@ -712,6 +742,17 @@ private void processMediaType(CodegenOperation op, Map<String, String> m) {
712
742
}
713
743
}
714
744
745
+ private Boolean unknownMimeTypesContainsType (String mimeType ) {
746
+ for (Map <String ,String > m : unknownMimeTypes ) {
747
+ String mimeType0 = m .get (MEDIA_DATA_TYPE );
748
+ if (mimeType0 != null && mimeType0 .equals (mimeType )) {
749
+ return true ;
750
+ }
751
+ }
752
+
753
+ return false ;
754
+ }
755
+
715
756
public String firstLetterToUpper (String word ) {
716
757
if (word .length () == 0 ) {
717
758
return word ;
0 commit comments