Skip to content

Commit

Permalink
Merge pull request #1732 from lnash94/config_issue
Browse files Browse the repository at this point in the history
Fix authentication configs issue in client generation
  • Loading branch information
lnash94 authored Jun 27, 2024
2 parents 6bcffcb + 34b95f1 commit 17cefe7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@

import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.ParameterNode;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.compiler.syntax.tree.TypeDefinitionNode;
import io.ballerina.openapi.core.generators.client.AuthConfigGeneratorImp;
import io.ballerina.openapi.core.generators.client.BallerinaClientGenerator;
import io.ballerina.openapi.core.generators.client.exception.ClientException;
import io.ballerina.openapi.core.generators.client.model.OASClientConfig;
import io.ballerina.openapi.core.generators.common.GeneratorUtils;
import io.ballerina.openapi.core.generators.common.TypeHandler;
import io.ballerina.openapi.core.generators.common.exception.BallerinaOpenApiException;
import io.ballerina.openapi.core.generators.common.model.Filter;
import io.ballerina.openapi.generators.common.TestConstants;
import io.swagger.v3.oas.models.OpenAPI;
import org.testng.Assert;
Expand All @@ -37,6 +42,8 @@
import java.util.List;
import java.util.Optional;

import static io.ballerina.openapi.generators.common.TestConstants.MIXED_AUTH_INIT_STATEMENTS;

/**
* All the tests related to the auth related code snippet generation for api key auth mechanism.
*/
Expand All @@ -54,6 +61,17 @@ public void testGetConfigRecord(String yamlFile) throws IOException, BallerinaOp
Optional<TypeDefinitionNode> connectionConfig = authRelatedTypeDefinitionNodes.stream()
.filter(typeDefinitionNode -> typeDefinitionNode.typeName().text().equals("ConnectionConfig"))
.findFirst();
TypeHandler.createInstance(openAPI, false);
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
.withFilters(new Filter())
.withOpenAPI(openAPI)
.withResourceMode(false).build();
BallerinaClientGenerator ballerinaClientGenerator = new BallerinaClientGenerator(oasClientConfig);
SyntaxTree syntaxTree = ballerinaClientGenerator.generateSyntaxTree();
Assert.assertTrue(syntaxTree.toSourceCode().trim().replaceAll("\\s+", "")
.contains(MIXED_AUTH_INIT_STATEMENTS.trim().replaceAll("\\s+", "")));

if (connectionConfig.isPresent()) {
String expectedRecord = TestConstants.CONNECTION_CONFIG_MIXED_AUTH;
String generatedRecord = connectionConfig.get().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,11 @@ public class TestConstants {
public static final String OAUTH2_REFRESH_TOKEN_GRANT_CONFIG_RECORD = "#OAuth2ClientCredentialsGrantConfigs" +
"publictypeOAuth2ClientCredentialsGrantConfigrecord{|*http:OAuth2ClientCredentialsGrantConfig;" +
"#TokenURLstringtokenUrl=\"https://domain/services/data/oauth2/token\";|};";
public static final String MIXED_AUTH_INIT_STATEMENTS = "if config.auth is ApiKeysConfig {\n" +
" self.apiKeyConfig = (<ApiKeysConfig>config.auth).cloneReadOnly();\n" +
" } else {\n" +
" httpClientConfig.auth = <OAuth2ClientCredentialsGrantConfig|http:BearerTokenConfig|" +
"OAuth2RefreshTokenGrantConfig>config.auth;\n" +
" self.apiKeyConfig = ();\n" +
" }";
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public isolated client class Client {
if config.auth is ApiKeysConfig {
self.apiKeyConfig = (<ApiKeysConfig>config.auth).cloneReadOnly();
} else {
config.auth = <http:BearerTokenConfig>config.auth;
httpClientConfig.auth = <http:BearerTokenConfig>config.auth;
self.apiKeyConfig = ();
}
http:Client httpEp = check new (serviceUrl, httpClientConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public isolated client class Client {
if config.auth is ApiKeysConfig {
self.apiKeyConfig = (<ApiKeysConfig>config.auth).cloneReadOnly();
} else {
config.auth = <http:BearerTokenConfig>config.auth;
httpClientConfig.auth = <http:BearerTokenConfig>config.auth;
self.apiKeyConfig = ();
}
http:Client httpEp = check new (serviceUrl, httpClientConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public isolated client class Client {
if config.auth is ApiKeysConfig {
self.apiKeyConfig = (<ApiKeysConfig>config.auth).cloneReadOnly();
} else {
config.auth = <OAuth2ClientCredentialsGrantConfig|http:BearerTokenConfig|OAuth2RefreshTokenGrantConfig>config.auth;
httpClientConfig.auth = <OAuth2ClientCredentialsGrantConfig|http:BearerTokenConfig|OAuth2RefreshTokenGrantConfig>config.auth;
self.apiKeyConfig = ();
}
http:Client httpEp = check new (serviceUrl, httpClientConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1367,9 +1367,9 @@ public IfElseStatementNode handleInitForMixOfApiKeyAndHTTPOrOAuth() {

List<StatementNode> clientConfigAssignmentNodes = new ArrayList<>();

// config.auth = <http:BearerTokenConfig>config.auth;
// httpClientConfig.auth = <http:BearerTokenConfig>config.auth;
FieldAccessExpressionNode clientConfigAuthRef = createFieldAccessExpressionNode(
createSimpleNameReferenceNode(createIdentifierToken(CONFIG)), createToken(DOT_TOKEN),
createSimpleNameReferenceNode(createIdentifierToken(HTTP_CLIENT_CONFIG)), createToken(DOT_TOKEN),
createSimpleNameReferenceNode(createIdentifierToken(AUTH)));
SimpleNameReferenceNode clientConfigExpr = createSimpleNameReferenceNode(
createIdentifierToken("<" + getAuthFieldTypeName() +
Expand Down

0 comments on commit 17cefe7

Please sign in to comment.