|
14 | 14 | import io.swagger.v3.oas.models.info.License; |
15 | 15 | import io.swagger.v3.oas.models.media.Schema; |
16 | 16 | import io.swagger.v3.oas.models.parameters.Parameter; |
| 17 | +import io.swagger.v3.oas.models.security.Scopes; |
17 | 18 | import io.swagger.v3.oas.models.security.SecurityRequirement; |
18 | 19 | import io.swagger.v3.oas.models.security.SecurityScheme; |
19 | 20 | import io.swagger.v3.oas.models.tags.Tag; |
|
45 | 46 | import java.util.Set; |
46 | 47 | import java.util.TreeMap; |
47 | 48 | import java.util.TreeSet; |
| 49 | +import java.util.stream.Collectors; |
48 | 50 |
|
49 | 51 | //import io.swagger.codegen.languages.AbstractJavaCodegen; |
50 | 52 |
|
@@ -911,17 +913,68 @@ private void processOperation(String resourcePath, String httpMethod, Operation |
911 | 913 | codegenOperation.tags = new ArrayList<>(tags); |
912 | 914 | config.addOperationToGroup(config.sanitizeTag(tag.getName()), resourcePath, operation, codegenOperation, operations); |
913 | 915 |
|
914 | | - List<SecurityRequirement> securities = operation.getSecurity(); |
915 | | - if (securities != null && securities.isEmpty()) { |
916 | | - continue; |
917 | | - } |
918 | | - Map<String, SecurityScheme> authMethods = getAuthMethods(securities, securitySchemes); |
919 | | - if (authMethods == null || authMethods.isEmpty()) { |
920 | | - authMethods = getAuthMethods(globalSecurities, securitySchemes); |
| 916 | + final List<SecurityRequirement> securities = new ArrayList<>(); |
| 917 | + |
| 918 | + if (globalSecurities != null) { |
| 919 | + for (SecurityRequirement globalSecurityRequirement : globalSecurities) { |
| 920 | + SecurityRequirement copy = new SecurityRequirement(); |
| 921 | + for (Map.Entry<String, List<String>> entry : globalSecurityRequirement.entrySet()) { |
| 922 | + copy.put(entry.getKey(), new ArrayList<>(entry.getValue())); |
| 923 | + } |
| 924 | + securities.add(copy); |
| 925 | + for (Map.Entry<String, List<String>> entry : copy.entrySet()) { |
| 926 | + if (operation.getSecurity() != null) { |
| 927 | + for (SecurityRequirement operationSecurityRequirement : operation.getSecurity()) { |
| 928 | + List<String> scopes = operationSecurityRequirement.get(entry.getKey()); |
| 929 | + if (scopes != null) { |
| 930 | + entry.getValue().addAll(scopes.stream() |
| 931 | + .filter(s -> entry.getValue().stream().noneMatch(s::equals)).collect(Collectors.toList())); |
| 932 | + } else { |
| 933 | + securities.add(operationSecurityRequirement); |
| 934 | + } |
| 935 | + } |
| 936 | + } |
| 937 | + } |
| 938 | + } |
| 939 | + } else if (operation.getSecurity() != null) { |
| 940 | + for (SecurityRequirement operationSecurityRequirement : operation.getSecurity()) { |
| 941 | + securities.add(operationSecurityRequirement); |
| 942 | + } |
921 | 943 | } |
922 | 944 |
|
| 945 | + Map<String, SecurityScheme> authMethods = getAuthMethods(securities, securitySchemes); |
| 946 | + codegenOperation.authMethods = config.fromSecurity(authMethods); |
| 947 | + List<CodegenSecurity> codegenSecurities = new ArrayList<>(); |
923 | 948 | if (authMethods != null && !authMethods.isEmpty()) { |
924 | | - codegenOperation.authMethods = config.fromSecurity(authMethods); |
| 949 | + for (CodegenSecurity codegenSecurity : codegenOperation.authMethods) { |
| 950 | + for (SecurityRequirement securityRequirement : securities) { |
| 951 | + if (!securityRequirement.containsKey(codegenSecurity.name)) { |
| 952 | + continue; |
| 953 | + } |
| 954 | + CodegenSecurity codegenSecurityCopy = new CodegenSecurity(); |
| 955 | + codegenSecurityCopy.vendorExtensions = codegenSecurity.vendorExtensions; |
| 956 | + if (codegenSecurityCopy.vendorExtensions != null) { |
| 957 | + for (Map.Entry<String, Object> entry : codegenSecurityCopy.vendorExtensions.entrySet()) { |
| 958 | + codegenSecurityCopy.vendorExtensions.put(entry.getKey(), entry.getValue()); |
| 959 | + } |
| 960 | + } |
| 961 | + codegenSecurityCopy.scopes = new Scopes(); |
| 962 | + codegenSecurityCopy.name = codegenSecurity.name; |
| 963 | + codegenSecurityCopy.type = codegenSecurity.type; |
| 964 | + codegenSecurityCopy.authorizationUrl = codegenSecurity.authorizationUrl; |
| 965 | + codegenSecurityCopy.flow = codegenSecurity.flow; |
| 966 | + codegenSecurityCopy.keyParamName = codegenSecurity.keyParamName; |
| 967 | + codegenSecurityCopy.tokenUrl = codegenSecurity.tokenUrl; |
| 968 | + codegenSecurities.add(codegenSecurityCopy); |
| 969 | + List<String> scopes = securityRequirement.get(codegenSecurity.name); |
| 970 | + if (scopes != null && codegenSecurity.scopes != null) { |
| 971 | + for (String scope : scopes) { |
| 972 | + codegenSecurityCopy.scopes.addString(scope, codegenSecurity.scopes.get(scope)); |
| 973 | + } |
| 974 | + } |
| 975 | + } |
| 976 | + } |
| 977 | + codegenOperation.authMethods = codegenSecurities; |
925 | 978 | codegenOperation.getVendorExtensions().put(CodegenConstants.HAS_AUTH_METHODS_EXT_NAME, Boolean.TRUE); |
926 | 979 | } |
927 | 980 | } catch (Exception ex) { |
|
0 commit comments