2626import io .swagger .v3 .oas .models .parameters .RequestBody ;
2727import io .swagger .v3 .oas .models .responses .ApiResponse ;
2828import io .swagger .v3 .oas .models .responses .ApiResponses ;
29+ import io .swagger .v3 .oas .models .security .SecurityScheme ;
2930import org .apache .commons .lang3 .StringUtils ;
3031import org .openapitools .codegen .utils .ModelUtils ;
3132import org .slf4j .Logger ;
@@ -101,6 +102,10 @@ public class OpenAPINormalizer {
101102 String fixDuplicatedOperationId ;
102103 HashSet <String > operationIdSet = new HashSet <>();
103104
105+ // when set to true, if a securityScheme is found with the specified name, it will be converted to bearerAuth
106+ final String DETECT_BEARER_AUTH_FROM_NAME = "DETECT_BEARER_AUTH_FROM_NAME" ;
107+ String bearerAuthSecuritySchemeName ;
108+
104109 // when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1)
105110 // by adding x-unsigned to the schema
106111 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) {
167172 ruleNames .add (SET_TAGS_TO_OPERATIONID );
168173 ruleNames .add (SET_TAGS_TO_VENDOR_EXTENSION );
169174 ruleNames .add (FIX_DUPLICATED_OPERATIONID );
175+ ruleNames .add (DETECT_BEARER_AUTH_FROM_NAME );
170176 ruleNames .add (ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE );
171177 ruleNames .add (REFACTOR_ALLOF_WITH_PROPERTIES_ONLY );
172178 ruleNames .add (NORMALIZE_31SPEC );
@@ -301,6 +307,11 @@ public void processRules(Map<String, String> inputRules) {
301307 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 ));
302308 }
303309 }
310+
311+ bearerAuthSecuritySchemeName = inputRules .get (DETECT_BEARER_AUTH_FROM_NAME );
312+ if (bearerAuthSecuritySchemeName != null ) {
313+ rules .put (DETECT_BEARER_AUTH_FROM_NAME , true );
314+ }
304315 }
305316
306317 /**
@@ -322,6 +333,7 @@ void normalize() {
322333
323334 normalizeInfo ();
324335 normalizePaths ();
336+ normalizeComponentsSecuritySchemes ();
325337 normalizeComponentsSchemas ();
326338 normalizeComponentsResponses ();
327339 }
@@ -547,6 +559,31 @@ private void normalizeHeaders(Map<String, Header> headers) {
547559 }
548560 }
549561
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+ schemes .put (schemeKey , scheme );
583+ }
584+ }
585+ }
586+
550587 /**
551588 * Normalizes schemas in components
552589 */
@@ -560,7 +597,7 @@ private void normalizeComponentsSchemas() {
560597 for (String schemaName : schemaNames ) {
561598 Schema schema = schemas .get (schemaName );
562599 if (schema == null ) {
563- LOGGER .warn ("{} not fount found in openapi/components/schemas." , schemaName );
600+ LOGGER .warn ("{} not found in openapi/components/schemas." , schemaName );
564601 } else {
565602 // remove x-internal if needed
566603 if (schema .getExtensions () != null && getRule (REMOVE_X_INTERNAL )) {
@@ -1053,7 +1090,6 @@ private void processFixDuplicatedOperationId(Operation operation) {
10531090 }
10541091 }
10551092
1056-
10571093 /**
10581094 * If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to
10591095 * ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment.
0 commit comments