25
25
import io .swagger .v3 .oas .models .parameters .RequestBody ;
26
26
import io .swagger .v3 .oas .models .responses .ApiResponse ;
27
27
import io .swagger .v3 .oas .models .servers .Server ;
28
+ import io .swagger .v3 .oas .models .tags .Tag ;
28
29
import org .apache .commons .io .FilenameUtils ;
29
30
import org .apache .commons .lang3 .StringUtils ;
30
31
import org .openapitools .codegen .*;
31
32
import org .openapitools .codegen .meta .GeneratorMetadata ;
32
33
import org .openapitools .codegen .meta .Stability ;
33
- import org .openapitools .codegen .meta .features .GlobalFeature ;
34
- import org .openapitools .codegen .meta .features .ParameterFeature ;
35
- import org .openapitools .codegen .meta .features .SchemaSupportFeature ;
36
- import org .openapitools .codegen .meta .features .WireFormatFeature ;
34
+ import org .openapitools .codegen .meta .features .*;
37
35
import org .openapitools .codegen .model .ModelMap ;
38
36
import org .openapitools .codegen .model .ModelsMap ;
39
37
import org .openapitools .codegen .model .OperationMap ;
53
51
54
52
public class RustAxumServerCodegen extends AbstractRustCodegen implements CodegenConfig {
55
53
public static final String PROJECT_NAME = "openapi-server" ;
56
- private static final String apiPath = "rust-axum" ;
57
54
58
55
private String packageName ;
59
56
private String packageVersion ;
@@ -99,6 +96,9 @@ public RustAxumServerCodegen() {
99
96
WireFormatFeature .JSON ,
100
97
WireFormatFeature .Custom
101
98
))
99
+ .securityFeatures (EnumSet .of (
100
+ SecurityFeature .ApiKey
101
+ ))
102
102
.excludeGlobalFeatures (
103
103
GlobalFeature .Info ,
104
104
GlobalFeature .ExternalDocumentation ,
@@ -113,9 +113,6 @@ public RustAxumServerCodegen() {
113
113
.excludeSchemaSupportFeatures (
114
114
SchemaSupportFeature .Polymorphism
115
115
)
116
- .excludeParameterFeatures (
117
- ParameterFeature .Cookie
118
- )
119
116
);
120
117
121
118
generatorMetadata = GeneratorMetadata .newBuilder (generatorMetadata )
@@ -436,7 +433,10 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
436
433
}
437
434
pathMethodOpMap
438
435
.computeIfAbsent (axumPath , (key ) -> new ArrayList <>())
439
- .add (new MethodOperation (op .httpMethod .toLowerCase (Locale .ROOT ), underscoredOperationId ));
436
+ .add (new MethodOperation (
437
+ op .httpMethod .toLowerCase (Locale .ROOT ),
438
+ underscoredOperationId ,
439
+ op .vendorExtensions ));
440
440
}
441
441
442
442
// Determine the types that this operation produces. `getProducesInfo`
@@ -488,7 +488,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
488
488
);
489
489
rsp .vendorExtensions .put ("x-response-id" , responseId );
490
490
}
491
-
491
+
492
492
if (rsp .dataType != null ) {
493
493
// Get the mimetype which is produced by this response. Note
494
494
// that although in general responses produces a set of
@@ -562,19 +562,39 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
562
562
}
563
563
}
564
564
}
565
+
566
+ for (CodegenProperty header : rsp .headers ) {
567
+ if (uuidType .equals (header .dataType )) {
568
+ additionalProperties .put ("apiUsesUuid" , true );
569
+ }
570
+ header .nameInPascalCase = toModelName (header .baseName );
571
+ header .nameInLowerCase = header .baseName .toLowerCase (Locale .ROOT );
572
+ }
573
+ }
574
+
575
+ for (CodegenParameter header : op .headerParams ) {
576
+ header .nameInLowerCase = header .baseName .toLowerCase (Locale .ROOT );
577
+ }
578
+
579
+ for (CodegenProperty header : op .responseHeaders ) {
580
+ if (uuidType .equals (header .dataType )) {
581
+ additionalProperties .put ("apiUsesUuid" , true );
582
+ }
583
+ header .nameInPascalCase = toModelName (header .baseName );
584
+ header .nameInLowerCase = header .baseName .toLowerCase (Locale .ROOT );
565
585
}
566
586
567
- // Include renderUuidConversionImpl exactly once in the vendorExtensions map when
568
- // at least one `uuid::Uuid` converted from a header value in the resulting Rust code.
569
- final Boolean renderUuidConversionImpl = op .headerParams .stream ().anyMatch (h -> h .getDataType ().equals (uuidType ));
570
- if (renderUuidConversionImpl ) {
587
+ // Include renderUuidConversionImpl exactly once in the vendorExtensions map when
588
+ // at least one `uuid::Uuid` converted from a header value in the resulting Rust code.
589
+ final boolean renderUuidConversionImpl = op .headerParams .stream ().anyMatch (h -> h .getDataType ().equals (uuidType ));
590
+ if (renderUuidConversionImpl )
571
591
additionalProperties .put ("renderUuidConversionImpl" , "true" );
572
- }
592
+
573
593
return op ;
574
594
}
575
595
576
596
@ Override
577
- public OperationsMap postProcessOperationsWithModels (OperationsMap operationsMap , List <ModelMap > allModels ) {
597
+ public OperationsMap postProcessOperationsWithModels (final OperationsMap operationsMap , List <ModelMap > allModels ) {
578
598
OperationMap operations = operationsMap .getOperations ();
579
599
operations .put ("classnamePascalCase" , camelize (operations .getClassname ()));
580
600
List <CodegenOperation > operationList = operations .getOperation ();
@@ -586,7 +606,7 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap operationsMap
586
606
return operationsMap ;
587
607
}
588
608
589
- private void postProcessOperationWithModels (CodegenOperation op ) {
609
+ private void postProcessOperationWithModels (final CodegenOperation op ) {
590
610
boolean consumesJson = false ;
591
611
boolean consumesPlainText = false ;
592
612
boolean consumesFormUrlEncoded = false ;
@@ -641,6 +661,22 @@ private void postProcessOperationWithModels(CodegenOperation op) {
641
661
param .vendorExtensions .put ("x-consumes-json" , true );
642
662
}
643
663
}
664
+
665
+ if (op .authMethods != null ) {
666
+ for (CodegenSecurity s : op .authMethods ) {
667
+ if (s .isApiKey && (s .isKeyInCookie || s .isKeyInHeader )) {
668
+ if (s .isKeyInCookie ) {
669
+ op .vendorExtensions .put ("x-has-cookie-auth-methods" , "true" );
670
+ op .vendorExtensions .put ("x-api-key-cookie-name" , toModelName (s .keyParamName ));
671
+ } else {
672
+ op .vendorExtensions .put ("x-has-header-auth-methods" , "true" );
673
+ op .vendorExtensions .put ("x-api-key-header-name" , toModelName (s .keyParamName ));
674
+ }
675
+
676
+ op .vendorExtensions .put ("x-has-auth-methods" , "true" );
677
+ }
678
+ }
679
+ }
644
680
}
645
681
646
682
@ Override
@@ -671,14 +707,16 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
671
707
return ;
672
708
}
673
709
674
- // Get all tags sorted by name
675
- final List < String > tags = op .tags .stream (). map ( t -> t . getName ()). sorted (). collect ( Collectors . toList ());
676
- // Combine into a single group
677
- final String combinedTag = tags . stream () .collect (Collectors .joining ("-" ));
710
+ // Get all tags sorted by name & Combine into a single group
711
+ final String combinedTag = op .tags .stream ()
712
+ . map ( Tag :: getName ). sorted ()
713
+ .collect (Collectors .joining ("-" ));
678
714
// Add to group
679
715
super .addOperationToGroup (combinedTag , resourcePath , operation , op , operations );
716
+
680
717
return ;
681
718
}
719
+
682
720
super .addOperationToGroup (tag , resourcePath , operation , op , operations );
683
721
}
684
722
@@ -692,7 +730,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
692
730
// restore things to sensible values.
693
731
@ Override
694
732
public CodegenParameter fromRequestBody (RequestBody body , Set <String > imports , String bodyParameterName ) {
695
- Schema original_schema = ModelUtils .getSchemaFromRequestBody (body );
733
+ final Schema original_schema = ModelUtils .getSchemaFromRequestBody (body );
696
734
CodegenParameter codegenParameter = super .fromRequestBody (body , imports , bodyParameterName );
697
735
698
736
if (StringUtils .isNotBlank (original_schema .get$ref ())) {
@@ -709,12 +747,12 @@ public CodegenParameter fromRequestBody(RequestBody body, Set<String> imports, S
709
747
}
710
748
711
749
@ Override
712
- public String toInstantiationType (Schema p ) {
750
+ public String toInstantiationType (final Schema p ) {
713
751
if (ModelUtils .isArraySchema (p )) {
714
- Schema inner = ModelUtils .getSchemaItems (p );
752
+ final Schema inner = ModelUtils .getSchemaItems (p );
715
753
return instantiationTypes .get ("array" ) + "<" + getSchemaType (inner ) + ">" ;
716
754
} else if (ModelUtils .isMapSchema (p )) {
717
- Schema inner = ModelUtils .getAdditionalProperties (p );
755
+ final Schema inner = ModelUtils .getAdditionalProperties (p );
718
756
return instantiationTypes .get ("map" ) + "<" + typeMapping .get ("string" ) + ", " + getSchemaType (inner ) + ">" ;
719
757
} else {
720
758
return null ;
@@ -727,7 +765,7 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> bun
727
765
728
766
final List <PathMethodOperations > pathMethodOps = pathMethodOpMap .entrySet ().stream ()
729
767
.map (entry -> {
730
- ArrayList <MethodOperation > methodOps = entry .getValue ();
768
+ final ArrayList <MethodOperation > methodOps = entry .getValue ();
731
769
methodOps .sort (Comparator .comparing (a -> a .method ));
732
770
return new PathMethodOperations (entry .getKey (), methodOps );
733
771
})
@@ -739,7 +777,7 @@ public Map<String, Object> postProcessSupportingFileData(Map<String, Object> bun
739
777
}
740
778
741
779
@ Override
742
- public String toDefaultValue (Schema p ) {
780
+ public String toDefaultValue (final Schema p ) {
743
781
String defaultValue = null ;
744
782
if ((ModelUtils .isNullable (p )) && (p .getDefault () != null ) && ("null" .equalsIgnoreCase (p .getDefault ().toString ())))
745
783
return "Nullable::Null" ;
@@ -899,7 +937,7 @@ protected void updateParameterForString(CodegenParameter codegenParameter, Schem
899
937
}
900
938
901
939
@ Override
902
- protected void updatePropertyForAnyType (CodegenProperty property , Schema p ) {
940
+ protected void updatePropertyForAnyType (final CodegenProperty property , final Schema p ) {
903
941
// The 'null' value is allowed when the OAS schema is 'any type'.
904
942
// See https://github.com/OAI/OpenAPI-Specification/issues/1389
905
943
if (Boolean .FALSE .equals (p .getNullable ())) {
@@ -917,7 +955,7 @@ protected void updatePropertyForAnyType(CodegenProperty property, Schema p) {
917
955
}
918
956
919
957
@ Override
920
- protected String getParameterDataType (Parameter parameter , Schema schema ) {
958
+ protected String getParameterDataType (final Parameter parameter , final Schema schema ) {
921
959
if (parameter .get$ref () != null ) {
922
960
String refName = ModelUtils .getSimpleRef (parameter .get$ref ());
923
961
return toModelName (refName );
@@ -938,10 +976,12 @@ static class PathMethodOperations {
938
976
static class MethodOperation {
939
977
public String method ;
940
978
public String operationID ;
979
+ public Map <String , Object > vendorExtensions ;
941
980
942
- MethodOperation (String method , String operationID ) {
981
+ MethodOperation (String method , String operationID , Map < String , Object > vendorExtensions ) {
943
982
this .method = method ;
944
983
this .operationID = operationID ;
984
+ this .vendorExtensions = vendorExtensions ;
945
985
}
946
986
}
947
987
}
0 commit comments