26
26
import io .swagger .v3 .oas .models .parameters .RequestBody ;
27
27
import io .swagger .v3 .oas .models .responses .ApiResponse ;
28
28
import io .swagger .v3 .oas .models .responses .ApiResponses ;
29
+ import io .swagger .v3 .oas .models .security .SecurityScheme ;
29
30
import org .apache .commons .lang3 .StringUtils ;
30
31
import org .openapitools .codegen .utils .ModelUtils ;
31
32
import org .slf4j .Logger ;
@@ -101,6 +102,10 @@ public class OpenAPINormalizer {
101
102
String fixDuplicatedOperationId ;
102
103
HashSet <String > operationIdSet = new HashSet <>();
103
104
105
+ // when set to true, if a securityScheme is found with the specified name, it will be converted to bearerAuth
106
+ final String SET_BEARER_AUTH_FOR_NAME = "SET_BEARER_AUTH_FOR_NAME" ;
107
+ String bearerAuthSecuritySchemeName ;
108
+
104
109
// when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1)
105
110
// by adding x-unsigned to the schema
106
111
final String ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE = "ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE" ;
@@ -167,6 +172,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
167
172
ruleNames .add (SET_TAGS_TO_OPERATIONID );
168
173
ruleNames .add (SET_TAGS_TO_VENDOR_EXTENSION );
169
174
ruleNames .add (FIX_DUPLICATED_OPERATIONID );
175
+ ruleNames .add (SET_BEARER_AUTH_FOR_NAME );
170
176
ruleNames .add (ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE );
171
177
ruleNames .add (REFACTOR_ALLOF_WITH_PROPERTIES_ONLY );
172
178
ruleNames .add (NORMALIZE_31SPEC );
@@ -301,6 +307,11 @@ public void processRules(Map<String, String> inputRules) {
301
307
LOGGER .error ("SET_PRIMITIVE_TYPES_TO_NULLABLE rule must be in the form of `string|integer|number|boolean`, e.g. `string`, `integer|number`: {}" , inputRules .get (SET_PRIMITIVE_TYPES_TO_NULLABLE ));
302
308
}
303
309
}
310
+
311
+ bearerAuthSecuritySchemeName = inputRules .get (SET_BEARER_AUTH_FOR_NAME );
312
+ if (bearerAuthSecuritySchemeName != null ) {
313
+ rules .put (SET_BEARER_AUTH_FOR_NAME , true );
314
+ }
304
315
}
305
316
306
317
/**
@@ -322,6 +333,7 @@ void normalize() {
322
333
323
334
normalizeInfo ();
324
335
normalizePaths ();
336
+ normalizeComponentsSecuritySchemes ();
325
337
normalizeComponentsSchemas ();
326
338
normalizeComponentsResponses ();
327
339
}
@@ -547,6 +559,36 @@ private void normalizeHeaders(Map<String, Header> headers) {
547
559
}
548
560
}
549
561
562
+ /**
563
+ * Normalizes securitySchemes in components
564
+ */
565
+ private void normalizeComponentsSecuritySchemes () {
566
+ if (StringUtils .isEmpty (bearerAuthSecuritySchemeName )) {
567
+ return ;
568
+ }
569
+
570
+ Map <String , SecurityScheme > schemes = openAPI .getComponents ().getSecuritySchemes ();
571
+ if (schemes == null ) {
572
+ return ;
573
+ }
574
+
575
+ for (String schemeKey : schemes .keySet ()) {
576
+ if (schemeKey .equals (bearerAuthSecuritySchemeName )) {
577
+ SecurityScheme scheme = schemes .get (schemeKey );
578
+ scheme .setType (SecurityScheme .Type .HTTP );
579
+ scheme .setScheme ("bearer" );
580
+ scheme .setIn (null );
581
+ scheme .setName (null );
582
+ scheme .setBearerFormat (null );
583
+ scheme .setFlows (null );
584
+ scheme .setOpenIdConnectUrl (null );
585
+ scheme .setExtensions (null );
586
+ scheme .set$ref (null );
587
+ schemes .put (schemeKey , scheme );
588
+ }
589
+ }
590
+ }
591
+
550
592
/**
551
593
* Normalizes schemas in components
552
594
*/
@@ -560,7 +602,7 @@ private void normalizeComponentsSchemas() {
560
602
for (String schemaName : schemaNames ) {
561
603
Schema schema = schemas .get (schemaName );
562
604
if (schema == null ) {
563
- LOGGER .warn ("{} not fount found in openapi/components/schemas." , schemaName );
605
+ LOGGER .warn ("{} not found in openapi/components/schemas." , schemaName );
564
606
} else {
565
607
// remove x-internal if needed
566
608
if (schema .getExtensions () != null && getRule (REMOVE_X_INTERNAL )) {
@@ -1053,7 +1095,6 @@ private void processFixDuplicatedOperationId(Operation operation) {
1053
1095
}
1054
1096
}
1055
1097
1056
-
1057
1098
/**
1058
1099
* If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to
1059
1100
* ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment.
0 commit comments