From 89e4b242955bede518f21fd14d68bf2b7bd38426 Mon Sep 17 00:00:00 2001 From: Tiffany Marrel Date: Sat, 20 May 2023 14:12:18 +0200 Subject: [PATCH] [Ada] [Java] fix Codegen copies (#15513) * fix incomplete codegensecurity copies * refactoring --- .../openapitools/codegen/CodegenSecurity.java | 69 +++++++++++-------- .../codegen/languages/AbstractAdaCodegen.java | 22 +----- .../languages/JavaPlayFrameworkCodegen.java | 46 +++---------- 3 files changed, 52 insertions(+), 85 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java index 0d70d5f1917..e76d927bfbe 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenSecurity.java @@ -46,36 +46,49 @@ public class CodegenSecurity { // OpenId specific public String openIdConnectUrl; - // Return a copy of the security object, filtering out any scopes from the passed-in list. - public CodegenSecurity filterByScopeNames(List filterScopes) { - CodegenSecurity filteredSecurity = new CodegenSecurity(); - // Copy all fields except the scopes. - filteredSecurity.name = name; - filteredSecurity.description = description; - filteredSecurity.type = type; - filteredSecurity.isBasic = isBasic; - filteredSecurity.isBasicBasic = isBasicBasic; - filteredSecurity.isHttpSignature = isHttpSignature; - filteredSecurity.isBasicBearer = isBasicBearer; - filteredSecurity.isApiKey = isApiKey; - filteredSecurity.isOAuth = isOAuth; - filteredSecurity.isOpenId = isOpenId; - filteredSecurity.keyParamName = keyParamName; - filteredSecurity.isCode = isCode; - filteredSecurity.isImplicit = isImplicit; - filteredSecurity.isApplication = isApplication; - filteredSecurity.isPassword = isPassword; - filteredSecurity.isKeyInCookie = isKeyInCookie; - filteredSecurity.isKeyInHeader = isKeyInHeader; - filteredSecurity.isKeyInQuery = isKeyInQuery; - filteredSecurity.flow = flow; - filteredSecurity.tokenUrl = tokenUrl; - filteredSecurity.authorizationUrl = authorizationUrl; - filteredSecurity.refreshUrl = refreshUrl; - filteredSecurity.openIdConnectUrl = openIdConnectUrl; + public CodegenSecurity () { + } + + public CodegenSecurity (CodegenSecurity original) { + this.name = original.name; + this.description = original.description; + this.type = original.type; + this.scheme = original.scheme; + this.isBasic = original.isBasic; + this.isBasicBasic = original.isBasicBasic; + this.isHttpSignature = original.isHttpSignature; + this.bearerFormat = original.bearerFormat; + this.isBasicBearer = original.isBasicBearer; + this.isApiKey = original.isApiKey; + this.isOAuth = original.isOAuth; + this.isOpenId = original.isOpenId; + this.keyParamName = original.keyParamName; + this.isCode = original.isCode; + this.isImplicit = original.isImplicit; + this.isApplication = original.isApplication; + this.isPassword = original.isPassword; + this.isKeyInCookie = original.isKeyInCookie; + this.isKeyInHeader = original.isKeyInHeader; + this.isKeyInQuery = original.isKeyInQuery; + this.flow = original.flow; + this.tokenUrl = original.tokenUrl; + this.authorizationUrl = original.authorizationUrl; + this.refreshUrl = original.refreshUrl; + this.openIdConnectUrl = original.openIdConnectUrl; + // It is not possible to deep copy the extensions, as we have no idea what types they are. // So the filtered method *will* refer to the original extensions, if any. - filteredSecurity.vendorExtensions = new HashMap(vendorExtensions); + this.vendorExtensions = original.vendorExtensions == null ? null : new HashMap(original.vendorExtensions); + + // It is not possible to deep copy the extensions, as we have no idea what type their values are. + // So the filtered method *will* refer to the original scopes, if any. + this.scopes = original.scopes == null ? null : new ArrayList>(original.scopes); + } + + // Return a copy of the security object, filtering out any scopes from the passed-in list. + public CodegenSecurity filterByScopeNames(List filterScopes) { + CodegenSecurity filteredSecurity = new CodegenSecurity(this); + List> returnedScopes = new ArrayList>(); Map lastScope = null; for (String filterScopeName : filterScopes) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java index dd67f9f4b57..88f9f088550 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java @@ -829,27 +829,7 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg List opScopes = (scopes == null) ? null : scopes.get(authMethod.name); authMethod.name = camelize(sanitizeName(authMethod.name), LOWERCASE_FIRST_LETTER); if (opScopes != null) { - CodegenSecurity opSecurity = new CodegenSecurity(); - opSecurity.name = authMethod.name; - opSecurity.type = authMethod.type; - opSecurity.isBasic = authMethod.isBasic; - opSecurity.isApiKey = authMethod.isApiKey; - opSecurity.isKeyInCookie = authMethod.isKeyInCookie; - opSecurity.isKeyInHeader = authMethod.isKeyInHeader; - opSecurity.isKeyInQuery = authMethod.isKeyInQuery; - opSecurity.flow = authMethod.flow; - opSecurity.tokenUrl = authMethod.tokenUrl; - List> opAuthScopes = new ArrayList<>(); - for (String opScopeName : opScopes) { - for (Map scope : authMethod.scopes) { - String name = (String) scope.get("scope"); - if (opScopeName.equals(name)) { - opAuthScopes.add(scope); - break; - } - } - } - opSecurity.scopes = opAuthScopes; + CodegenSecurity opSecurity = authMethod.filterByScopeNames(opScopes); result.add(opSecurity); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaPlayFrameworkCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaPlayFrameworkCodegen.java index 04abfd95b93..40ba4295bf3 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaPlayFrameworkCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaPlayFrameworkCodegen.java @@ -393,17 +393,6 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea for (CodegenSecurity codegenSecurity : securities) { ExtendedCodegenSecurity extendedCodegenSecurity = new ExtendedCodegenSecurity(codegenSecurity); - Object jwksUrl = extendedCodegenSecurity.vendorExtensions.get(X_JWKS_URL); - - if (jwksUrl instanceof String) { - extendedCodegenSecurity.jwksUrl = (String) jwksUrl; - } - - Object tokenIntrospectUrl = extendedCodegenSecurity.vendorExtensions.get(X_TOKEN_INTROSPECT_URL); - - if (tokenIntrospectUrl instanceof String) { - extendedCodegenSecurity.tokenIntrospectUrl = (String) tokenIntrospectUrl; - } extendedSecurities.add(extendedCodegenSecurity); } @@ -416,32 +405,17 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea public String tokenIntrospectUrl; public ExtendedCodegenSecurity(CodegenSecurity cm) { - super(); + super(cm); - this.name = cm.name; - this.type = cm.type; - this.scheme = cm.scheme; - this.isBasic = cm.isBasic; - this.isOAuth = cm.isOAuth; - this.isApiKey = cm.isApiKey; - this.isBasicBasic = cm.isBasicBasic; - this.isBasicBearer = cm.isBasicBearer; - this.isHttpSignature = cm.isHttpSignature; - this.bearerFormat = cm.bearerFormat; - this.vendorExtensions = new HashMap(cm.vendorExtensions); - this.keyParamName = cm.keyParamName; - this.isKeyInQuery = cm.isKeyInQuery; - this.isKeyInHeader = cm.isKeyInHeader; - this.isKeyInCookie = cm.isKeyInCookie; - this.flow = cm.flow; - this.authorizationUrl = cm.authorizationUrl; - this.tokenUrl = cm.tokenUrl; - this.refreshUrl = cm.refreshUrl; - this.scopes = cm.scopes; - this.isCode = cm.isCode; - this.isPassword = cm.isPassword; - this.isApplication = cm.isApplication; - this.isImplicit = cm.isImplicit; + Object cmJwksUrl = cm.vendorExtensions.get(X_JWKS_URL); + if (cmJwksUrl instanceof String) { + this.jwksUrl = (String) cmJwksUrl; + } + + Object cmTokenIntrospectUrl = cm.vendorExtensions.get(X_TOKEN_INTROSPECT_URL); + if (cmTokenIntrospectUrl instanceof String) { + this.tokenIntrospectUrl = (String) cmTokenIntrospectUrl; + } } @Override