Skip to content

Commit

Permalink
Try changing apicurio model to rc-2 for second time
Browse files Browse the repository at this point in the history
  • Loading branch information
thushalya committed May 2, 2023
1 parent 4b599c7 commit 8d88455
Show file tree
Hide file tree
Showing 12 changed files with 985 additions and 965 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.ballerina.asyncapi.core.generators.client.BallerinaClientGenerator;
import io.ballerina.asyncapi.core.generators.client.BallerinaTestGenerator;
import io.ballerina.asyncapi.core.generators.client.model.AASClientConfig;
import io.ballerina.asyncapi.core.generators.schema.BallerinaTypesGenerator;
//import io.ballerina.asyncapi.core.generators.schema.BallerinaTypesGenerator;
import io.ballerina.asyncapi.core.model.GenSrcFile;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
Expand Down Expand Up @@ -329,20 +330,20 @@ private List<GenSrcFile> generateClientFiles(Path asyncAPI, boolean nullable)
List<TypeDefinitionNode> preGeneratedTypeDefNodes = new ArrayList<>(
ballerinaClientGenerator.getBallerinaAuthConfigGenerator().getAuthRelatedTypeDefinitionNodes());
preGeneratedTypeDefNodes.addAll(ballerinaClientGenerator.getTypeDefinitionNodeList());
// Generate ballerina records to represent schemas.
// BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(
// asyncAPIDef, nullable, preGeneratedTypeDefNodes);
// Generate ballerina records to represent schemas.
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(
asyncAPIDef, nullable, preGeneratedTypeDefNodes);

// SyntaxTree schemaSyntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
// String schemaContent = Formatter.format(schemaSyntaxTree).toString();
SyntaxTree schemaSyntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
String schemaContent = Formatter.format(schemaSyntaxTree).toString();
// if (filter.getTags().size() > 0) {
// // Remove unused records and enums when generating the client by the tags given.
// schemaContent = GeneratorUtils.removeUnusedEntities(schemaSyntaxTree, mainContent, schemaContent, null);
// }
// if (!schemaContent.isBlank()) {
// sourceFiles.add(new GenSrcFile(GenSrcFile.GenFileType.MODEL_SRC, srcPackage, TYPE_FILE_NAME,
// schemaContent));
// }
if (!schemaContent.isBlank()) {
sourceFiles.add(new GenSrcFile(GenSrcFile.GenFileType.MODEL_SRC, srcPackage, TYPE_FILE_NAME,
schemaContent));
}

// Generate test boilerplate code for test cases
if (this.includeTestFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public String getValue() {
public static final String CLIENT_CLASS = "Client";
public static final String CLIENT_EP = "clientEp";
public static final String EQUAL = "=";
public static final String CONFIG = "config";
public static final String CLIENT_CONFIG = "clientConfig";
public static final String CONFIG = "clientConfig";
public static final String FUNCTION = "function";
public static final String PREFIX_TEST = " test";
public static final String ANNOT_TEST = "test:Config";
Expand Down Expand Up @@ -154,7 +155,7 @@ public String getValue() {
public static final String API_KEY_CONFIG_PARAM = "apiKeyConfig";
public static final String AUTH = "auth";
public static final String AUTH_CONFIG = "authConfig";
public static final String WEBSOCKET_CLIENT_CONFIG = "websocketClientConfig";

public static final String CLIENT_HTTP1_SETTINGS = "ClientHttp1Settings";
public static final String CLIENT_HTTP1_SETTINGS_FIELD = "http1Settings";
// public static final String HTTP2_SETTINGS = "http:Cookie";
Expand All @@ -167,7 +168,7 @@ public String getValue() {
public static final String SECURE_SOCKET = "websocket:ClientSecureSocket";
public static final String SECURE_SOCKET_FIELD = "secureSocket";

public static final String PING_PONG_SERVICE_FIELD= "PingPongService";
public static final String PING_PONG_SERVICE_FIELD= "pingPongHandler";
public static final String WEB_SOCKET_RETRY_CONFIG = "websocket:WebSocketRetryConfig";

public static final String KEEP_ALIVE = "keepAlive";
Expand All @@ -177,7 +178,12 @@ public String getValue() {
public static final String REFRESH_TOKEN = "refresh_token";
public static final String CLIENT_CRED = "client_cred";
public static final String PASSWORD = "password";
public static final String CONNECTION_CONFIG = "ConnectionConfig";

public static final String REMOTE_METHOD_NAME_PREFIX ="do";

// public static final String CONNECTION_CONFIG = "ConnectionConfig";

public static final String CONNECTION_CONFIG = "websocket:ClientConfiguration";
public static final String OAUTH2 = "oauth2";
public static final String SSL_FIELD_NAME = "secureSocket";
public static final String RETRY_CONFIG_FIELD = "retryConfig";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public static String convertAsyncAPITypeToBallerina(String type) throws Ballerin
if (GeneratorConstants.TYPE_MAP.containsKey(type)) {
return GeneratorConstants.TYPE_MAP.get(type);
} else {
throw new BallerinaAsyncApiException("Unsupported OAS data type `" + type + "`");
throw new BallerinaAsyncApiException("Unsupported AsyncAPI data type `" + type + "`");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,13 @@
package io.ballerina.asyncapi.core.generators.client;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.TextNode;
import io.apicurio.datamodels.models.ServerVariable;
import io.apicurio.datamodels.models.asyncapi.AsyncApiMessage;
import io.apicurio.datamodels.models.asyncapi.AsyncApiServer;
import io.apicurio.datamodels.models.asyncapi.v25.*;
import io.ballerina.asyncapi.core.exception.BallerinaAsyncApiException;
import io.ballerina.asyncapi.core.generators.schema.BallerinaTypesGenerator;
import io.ballerina.compiler.syntax.tree.AnnotationNode;
import io.ballerina.compiler.syntax.tree.AssignmentStatementNode;
import io.ballerina.compiler.syntax.tree.ClassDefinitionNode;
import io.ballerina.compiler.syntax.tree.FieldAccessExpressionNode;
import io.ballerina.compiler.syntax.tree.FunctionBodyNode;
import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode;
import io.ballerina.compiler.syntax.tree.FunctionSignatureNode;
import io.ballerina.compiler.syntax.tree.IdentifierToken;
import io.ballerina.compiler.syntax.tree.ImportDeclarationNode;
import io.ballerina.compiler.syntax.tree.MarkdownDocumentationLineNode;
import io.ballerina.compiler.syntax.tree.MarkdownDocumentationNode;
import io.ballerina.compiler.syntax.tree.MarkdownParameterDocumentationLineNode;
import io.ballerina.compiler.syntax.tree.MetadataNode;
import io.ballerina.compiler.syntax.tree.ModuleMemberDeclarationNode;
import io.ballerina.compiler.syntax.tree.ModulePartNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NodeList;
import io.ballerina.compiler.syntax.tree.ObjectFieldNode;
import io.ballerina.compiler.syntax.tree.OptionalTypeDescriptorNode;
import io.ballerina.compiler.syntax.tree.ParameterNode;
import io.ballerina.compiler.syntax.tree.QualifiedNameReferenceNode;
import io.ballerina.compiler.syntax.tree.ReturnStatementNode;
import io.ballerina.compiler.syntax.tree.ReturnTypeDescriptorNode;
import io.ballerina.compiler.syntax.tree.SeparatedNodeList;
import io.ballerina.compiler.syntax.tree.SimpleNameReferenceNode;
import io.ballerina.compiler.syntax.tree.StatementNode;
import io.ballerina.compiler.syntax.tree.SyntaxKind;
import io.ballerina.compiler.syntax.tree.SyntaxTree;
import io.ballerina.compiler.syntax.tree.Token;
import io.ballerina.compiler.syntax.tree.TypeDefinitionNode;
import io.ballerina.compiler.syntax.tree.*;
import io.ballerina.asyncapi.core.GeneratorConstants;
import io.ballerina.asyncapi.core.GeneratorUtils;
import io.ballerina.asyncapi.core.generators.client.model.AASClientConfig;
Expand All @@ -65,13 +35,10 @@
import io.ballerina.tools.text.TextDocuments;


import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -114,7 +81,6 @@
import static io.ballerina.compiler.syntax.tree.SyntaxKind.PUBLIC_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.QUESTION_MARK_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.REMOTE_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.RESOURCE_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.RETURNS_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.RETURN_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.SEMICOLON_TOKEN;
Expand Down Expand Up @@ -189,7 +155,8 @@ public BallerinaClientGenerator(AASClientConfig AASClientConfig) {
this.ballerinaUtilGenerator = new BallerinaUtilGenerator();
this.remoteFunctionNameList = new ArrayList<>();
this.serverURL = "/";
this.ballerinaAuthConfigGenerator = new BallerinaAuthConfigGenerator(false, false);
this.ballerinaAuthConfigGenerator = new BallerinaAuthConfigGenerator(false, false , asyncAPI,
ballerinaUtilGenerator);
this.resourceMode = AASClientConfig.isResourceMode();
}

Expand Down Expand Up @@ -260,7 +227,6 @@ private ClassDefinitionNode getClassDefinitionNode() throws BallerinaAsyncApiExc
String stringClassName= asyncAPI.getInfo().getTitle().trim()+ GeneratorUtils.
removeNonAlphanumeric(asyncAPI.getChannels().getItemNames().get(0).trim())+"Client";
IdentifierToken className= createIdentifierToken(stringClassName);
//TODO: Change this name as extracting it from the title of the asyncapi document
// IdentifierToken className = createIdentifierToken(GeneratorConstants.CLIENT_CLASS);
NodeList<Token> classTypeQualifiers = createNodeList(
createToken(ISOLATED_KEYWORD), createToken(CLIENT_KEYWORD));
Expand Down Expand Up @@ -323,12 +289,12 @@ private MetadataNode getClassMetadataNode() {
* @throws BallerinaAsyncApiException When invalid server URL is provided
*/
private FunctionDefinitionNode createInitFunction() throws BallerinaAsyncApiException {

FunctionSignatureNode functionSignatureNode = getInitFunctionSignatureNode();
ArrayList initMetaDataDoc=new ArrayList();
FunctionSignatureNode functionSignatureNode = getInitFunctionSignatureNode(initMetaDataDoc);
FunctionBodyNode functionBodyNode = getInitFunctionBodyNode();
NodeList<Token> qualifierList = createNodeList(createToken(PUBLIC_KEYWORD), createToken(ISOLATED_KEYWORD));
IdentifierToken functionName = createIdentifierToken("init");
return createFunctionDefinitionNode(null, getInitDocComment(), qualifierList, createToken(FUNCTION_KEYWORD),
return createFunctionDefinitionNode(null, getInitDocComment(initMetaDataDoc), qualifierList, createToken(FUNCTION_KEYWORD),
functionName, createEmptyNodeList(), functionSignatureNode, functionBodyNode);
}

Expand All @@ -341,8 +307,8 @@ private FunctionBodyNode getInitFunctionBodyNode() {

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

assignmentNodes.add(ballerinaAuthConfigGenerator.getWebsocketClientConfigVariableNode());
assignmentNodes.add(ballerinaAuthConfigGenerator.getClientConfigDoStatementNode());
// assignmentNodes.add(ballerinaAuthConfigGenerator.getWebsocketClientConfigVariableNode());
// assignmentNodes.add(ballerinaAuthConfigGenerator.getClientConfigDoStatementNode());

// If both apiKey and httpOrOAuth is supported
// todo : After revamping
Expand Down Expand Up @@ -385,11 +351,11 @@ private FunctionBodyNode getInitFunctionBodyNode() {
* @return {@link FunctionSignatureNode}
* @throws BallerinaAsyncApiException When invalid server URL is provided
*/
private FunctionSignatureNode getInitFunctionSignatureNode() throws BallerinaAsyncApiException {
private FunctionSignatureNode getInitFunctionSignatureNode(ArrayList initMetaDataNode) throws BallerinaAsyncApiException {

serverURL = getServerURL((AsyncApi25ServersImpl) asyncAPI.getServers());
SeparatedNodeList<ParameterNode> parameterList = createSeparatedNodeList(
ballerinaAuthConfigGenerator.getConfigParamForClassInit(serverURL));
ballerinaAuthConfigGenerator.getConfigParamForClassInit(serverURL,initMetaDataNode));
OptionalTypeDescriptorNode returnType = createOptionalTypeDescriptorNode(createToken(ERROR_KEYWORD),
createToken(QUESTION_MARK_TOKEN));
ReturnTypeDescriptorNode returnTypeDescriptorNode = createReturnTypeDescriptorNode(
Expand All @@ -403,9 +369,9 @@ private FunctionSignatureNode getInitFunctionSignatureNode() throws BallerinaAsy
*
* @return {@link MetadataNode} Metadata node containing entire function documentation comment.
*/
private MetadataNode getInitDocComment() {
private MetadataNode getInitDocComment(ArrayList docs) {

List<Node> docs = new ArrayList<>();
// List<Node> docs = new ArrayList<>();
String clientInitDocComment = "Gets invoked to initialize the `connector`.\n";
Map<String, JsonNode> extensions = ((AsyncApi25InfoImpl)asyncAPI.getInfo()).getExtensions();
if (extensions != null && !extensions.isEmpty()) {
Expand Down Expand Up @@ -484,7 +450,7 @@ private List<FunctionDefinitionNode> createRemoteFunctions(Map<String, AsyncApiM
if(extensions!=null && extensions.get(X_RESPONSE)!=null ){
// JsonNode ref=extensions.get(X_RESPONSE).get(PAYLOAD);
FunctionDefinitionNode functionDefinitionNode =
getClientMethodFunctionDefinitionNode( (AsyncApi25MessageImpl)messageItem.getValue(),extensions);
getClientMethodFunctionDefinitionNode( messageItem,extensions);
functionDefinitionNodeList.add(functionDefinitionNode);
//

Expand Down Expand Up @@ -544,18 +510,20 @@ private List<FunctionDefinitionNode> createRemoteFunctions(Map<String, AsyncApiM
// * }
// * </pre>
// */
private FunctionDefinitionNode getClientMethodFunctionDefinitionNode(AsyncApi25MessageImpl message,
private FunctionDefinitionNode getClientMethodFunctionDefinitionNode(Map.Entry<String, AsyncApiMessage> message,
Map<String, JsonNode> extensions)
throws BallerinaAsyncApiException {
// Create api doc for function
List<Node> remoteFunctionDocs = new ArrayList<>();
AsyncApi25MessageImpl messageValue= (AsyncApi25MessageImpl) message.getValue();
String messageName=message.getKey();

if (message.getSummary() != null) {
if (messageValue.getSummary() != null) {
remoteFunctionDocs.addAll(DocCommentsGenerator.createAPIDescriptionDoc(
message.getSummary(), true));
} else if (message.getDescription() != null && !message.getDescription().isBlank()) {
messageValue.getSummary(), true));
} else if (messageValue.getDescription() != null && !messageValue.getDescription().isBlank()) {
remoteFunctionDocs.addAll(DocCommentsGenerator.createAPIDescriptionDoc(
message.getDescription(), true));
messageValue.getDescription(), true));
} else {
MarkdownDocumentationLineNode newLine = createMarkdownDocumentationLineNode(null,
createToken(SyntaxKind.HASH_TOKEN), createEmptyNodeList());
Expand All @@ -567,14 +535,14 @@ private FunctionDefinitionNode getClientMethodFunctionDefinitionNode(AsyncApi25
REMOTE_KEYWORD),
createToken(ISOLATED_KEYWORD));
Token functionKeyWord = createToken(FUNCTION_KEYWORD);
IdentifierToken functionName = createIdentifierToken("do"+message.getName());
IdentifierToken functionName = createIdentifierToken(REMOTE_METHOD_NAME_PREFIX +messageName);

remoteFunctionNameList.add(message.getName());
remoteFunctionNameList.add(messageName);

FunctionSignatureGenerator functionSignatureGenerator = new FunctionSignatureGenerator(asyncAPI,
ballerinaSchemaGenerator, typeDefinitionNodeList, resourceMode);
FunctionSignatureNode functionSignatureNode =
functionSignatureGenerator.getFunctionSignatureNode(message.getPayload(),
functionSignatureGenerator.getFunctionSignatureNode(messageValue.getPayload(),
remoteFunctionDocs,extensions);
typeDefinitionNodeList = functionSignatureGenerator.getTypeDefinitionNodeList();
// // Create `Deprecated` annotation if an operation has mentioned as `deprecated:true`
Expand All @@ -583,14 +551,15 @@ private FunctionDefinitionNode getClientMethodFunctionDefinitionNode(AsyncApi25
// remoteFunctionDocs, annotationNodes);
// }
// Create metadataNode add documentation string

List<AnnotationNode> annotationNodes = new ArrayList<>();
MetadataNode metadataNode = createMetadataNode(createMarkdownDocumentationNode(
createNodeList(remoteFunctionDocs)), createNodeList(annotationNodes));

// Create Function Body
FunctionBodyGenerator functionBodyGenerator = new FunctionBodyGenerator(imports, typeDefinitionNodeList,
asyncAPI, ballerinaSchemaGenerator, ballerinaAuthConfigGenerator, ballerinaUtilGenerator, resourceMode);
FunctionBodyNode functionBodyNode = functionBodyGenerator.getFunctionBodyNode(extensions);
FunctionBodyNode functionBodyNode = functionBodyGenerator.getFunctionBodyNode(extensions, ((RequiredParameterNode)functionSignatureNode.parameters().get(0)).paramName().get().toString());
imports = functionBodyGenerator.getImports();

//Generate relative path
Expand Down
Loading

0 comments on commit 8d88455

Please sign in to comment.