Skip to content

Commit

Permalink
Merge pull request #1753 from lnash94/regression_issues
Browse files Browse the repository at this point in the history
[master]Fix some regression issues in RC1 pack
  • Loading branch information
lnash94 authored Aug 13, 2024
2 parents 0233bdb + d279381 commit 83bdd3d
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static io.ballerina.openapi.service.mapper.Constants.EXAMPLES;
import static io.ballerina.openapi.service.mapper.Constants.FILE_PATH;
Expand Down Expand Up @@ -178,7 +180,7 @@ private static void handleExamples(ResourceMetaInfoAnnotation.Builder resMetaInf
continue;
}
ExpressionNode expressValue = optExamplesValue.get();
String sourceCode = expressValue.toSourceCode();
String sourceCode = removeCommentLines(expressValue.toSourceCode());
ObjectMapper objectMapper = new ObjectMapper();
try {
Map<?, ?> objectMap = objectMapper.readValue(sourceCode, Map.class);
Expand All @@ -194,7 +196,7 @@ private static void handleExamples(ResourceMetaInfoAnnotation.Builder resMetaInf
continue;
}
ExpressionNode expressValue = optExamplesValue.get();
String mediaType = expressValue.toSourceCode();
String mediaType = removeCommentLines(expressValue.toSourceCode());
ObjectMapper objectMapper = new ObjectMapper();
try {
Map<?, ?> objectMap = objectMapper.readValue(mediaType, Map.class);
Expand Down Expand Up @@ -507,4 +509,22 @@ private static List<String> extractListItems(ListConstructorExpressionNode list)
}
return values;
}

public static String removeCommentLines(String content) {
// Use regex to match lines that start with "//" or "#" or contain inline "//" comments
String regex = "(?m)^\\s*(//|#).*|(?m)\\s*//.*$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);

// Remove lines that start with "//" or "#"
content = matcher.replaceAll("");

// Use regex to match and remove inline "//" comments
regex = "(?m)^(.*?)(//|#).*?$";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(content);

// Replace inline comments with the content before them
return matcher.replaceAll("$1");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.ballerina.openapi.generators.openapi;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* This test class for the covering the unit tests for example mapping scenarios.
*/
public class ExampleTests {
private static final Path RES_DIR = Paths.get("src/test/resources/ballerina-to-openapi").toAbsolutePath();
private Path tempDir;

@BeforeMethod
public void setup() throws IOException {
this.tempDir = Files.createTempDirectory("bal-to-openapi-test-out-" + System.nanoTime());
}

//TODO: Enable this test once the openapi package is published with new changes
@Test(description = "Resource function api doc mapped to OAS operation summary", enabled = false)
public void testsForResourceFunction() throws IOException {
Path ballerinaFilePath = RES_DIR.resolve("metainfo/examples/resource_annotation_examples.bal");
TestUtils.compareWithGeneratedFile(ballerinaFilePath, "metainfo/examples/" +
"resource_annotation_examples.yaml");
}

@AfterMethod
public void cleanUp() {
TestUtils.deleteDirectory(this.tempDir);
}

@AfterTest
public void clean() {
System.setErr(null);
System.setOut(null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"fromCurrency": "Same-EUR",
"toCurrency": "LKR",
"fromAmount": 200,
"toAmount": 60000,
"timestamp": "2024-07-14"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import ballerina/http;
import ballerina/openapi;

listener http:Listener httpListener = check new (9000);

service /convert on httpListener {

@openapi:ResourceInfo {
examples: {
"response": {
"201": {
"examples": {
"application/json": {
"json01": {
"filePath": "example.json"
},
"json02": {
"value": {
"fromCurrency": "EUR",
"toCurrency": "LKR",
"fromAmount": 200,
"toAmount": 60000,
"timestamp": "2024-07-14"
}
}
// "json03": {
// "filePath": "example.json"
// },
},
"application/xml": {
"xml1": {
"filePath": "example.json"
},
"xml2": {
"value": {
"fromCurrency": "EUR",
"toCurrency": "LKR",
"fromAmount": 200,
"toAmount": 60000,
"timestamp": "2024-07-14"
}
}
}
}
}
},
"requestBody": {
"application/json": {
"requestExample01": {
"filePath": "example.json"
},
"requestExample02": {
"value": {
"fromCurrancy": "LKR",
"toCurrancy": "USD" //comment
}
}
// "requestExample03": {
// "filePath": "example.json"
// },
}
}

}
}
resource function post rate(record {} payload) returns record {}|xml? {
return {};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
openapi: 3.0.1
info:
title: Convert
version: 0.1.0
servers:
- url: "{server}:{port}/convert"
variables:
server:
default: http://localhost
port:
default: "9000"
paths:
/rate:
post:
operationId: postRate
requestBody:
content:
application/json:
schema:
type: object
properties: {}
examples:
requestExample01:
value:
toAmount: 60000
fromCurrency: Same-EUR
toCurrency: LKR
fromAmount: 200
timestamp: 2024-07-14
requestExample02:
value:
fromCurrancy: LKR
toCurrancy: USD
required: true
responses:
"201":
description: Created
content:
application/xml:
schema:
type: object
examples:
xml1:
value:
toAmount: 60000
fromCurrency: Same-EUR
toCurrency: LKR
fromAmount: 200
timestamp: 2024-07-14
xml2:
value:
fromCurrency: EUR
toCurrency: LKR
fromAmount: 200
toAmount: 60000
timestamp: 2024-07-14
application/json:
schema:
type: object
properties: {}
examples:
json02:
value:
fromCurrency: EUR
toCurrency: LKR
fromAmount: 200
toAmount: 60000
timestamp: 2024-07-14
json01:
value:
toAmount: 60000
fromCurrency: Same-EUR
toCurrency: LKR
fromAmount: 200
timestamp: 2024-07-14
"202":
description: Accepted
"400":
description: BadRequest
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorPayload'
components:
schemas:
ErrorPayload:
required:
- message
- method
- path
- reason
- status
- timestamp
type: object
properties:
timestamp:
type: string
status:
type: integer
format: int64
reason:
type: string
message:
type: string
path:
type: string
method:
type: string
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public isolated client class Client {
final readonly & ApiKeysConfig apiKeyConfig;
final readonly & ApiKeysConfig apiKeyConfig = {apikey: ""};
# Gets invoked to initialize the `connector`.
#
# + apiKeyConfig - API keys for authorization
Expand Down
1 change: 1 addition & 0 deletions openapi-cli/src/test/resources/testng.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ under the License.
<class name="io.ballerina.openapi.generators.common.DocCommenTests"/>
<class name="io.ballerina.openapi.generators.client.MockClientGenerationTests"/>
<class name="io.ballerina.openapi.generators.common.OASModifierTests"/>
<class name="io.ballerina.openapi.generators.openapi.ExampleTests"/>
</classes>
</test>
</suite>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.ballerina.compiler.syntax.tree.FunctionBodyNode;
import io.ballerina.compiler.syntax.tree.NodeList;
import io.ballerina.compiler.syntax.tree.NodeParser;
import io.ballerina.compiler.syntax.tree.ObjectFieldNode;
import io.ballerina.compiler.syntax.tree.ReturnStatementNode;
import io.ballerina.compiler.syntax.tree.StatementNode;
Expand All @@ -42,6 +43,7 @@
import static io.ballerina.compiler.syntax.tree.NodeFactory.createFunctionBodyBlockNode;
import static io.ballerina.compiler.syntax.tree.NodeFactory.createReturnStatementNode;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.CLOSE_BRACE_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.EQUAL_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.OPEN_BRACE_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.RETURN_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.SEMICOLON_TOKEN;
Expand Down Expand Up @@ -85,6 +87,14 @@ public List<ObjectFieldNode> createClassInstanceVariables() {
// add apiKey instance variable when API key security schema is given
ObjectFieldNode apiKeyFieldNode = authConfigGeneratorImp.getApiKeyMapClassVariable();
if (apiKeyFieldNode != null) {
apiKeyFieldNode = apiKeyFieldNode.modify(apiKeyFieldNode.metadata().orElse(null),
apiKeyFieldNode.visibilityQualifier().orElse(null),
apiKeyFieldNode.qualifierList(),
apiKeyFieldNode.typeName(),
apiKeyFieldNode.fieldName(),
createToken(EQUAL_TOKEN),
NodeParser.parseExpression("{ apikey: \"\"}"), apiKeyFieldNode.semicolonToken());

fieldNodeList.add(apiKeyFieldNode);
}
return fieldNodeList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.ballerina.compiler.syntax.tree.MetadataNode;
import io.ballerina.compiler.syntax.tree.Node;
import io.ballerina.compiler.syntax.tree.NodeList;
import io.ballerina.compiler.syntax.tree.NodeParser;
import io.ballerina.compiler.syntax.tree.ObjectFieldNode;
import io.ballerina.compiler.syntax.tree.ReturnStatementNode;
import io.ballerina.compiler.syntax.tree.StatementNode;
Expand Down Expand Up @@ -54,6 +55,7 @@
import static io.ballerina.compiler.syntax.tree.SyntaxKind.CLASS_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.CLIENT_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.CLOSE_BRACE_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.EQUAL_TOKEN;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.FUNCTION_DEFINITION;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.FUNCTION_KEYWORD;
import static io.ballerina.compiler.syntax.tree.SyntaxKind.ISOLATED_KEYWORD;
Expand Down Expand Up @@ -127,6 +129,13 @@ public List<ObjectFieldNode> createClassInstanceVariables() {
// add apiKey instance variable when API key security schema is given
ObjectFieldNode apiKeyFieldNode = authConfigGeneratorImp.getApiKeyMapClassVariable();
if (apiKeyFieldNode != null) {
apiKeyFieldNode = apiKeyFieldNode.modify(apiKeyFieldNode.metadata().orElse(null),
apiKeyFieldNode.visibilityQualifier().orElse(null),
apiKeyFieldNode.qualifierList(),
apiKeyFieldNode.typeName(),
apiKeyFieldNode.fieldName(),
createToken(EQUAL_TOKEN),
NodeParser.parseExpression("{ apikey: \"\"}"), apiKeyFieldNode.semicolonToken());
fieldNodeList.add(apiKeyFieldNode);
}
return fieldNodeList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getValue() {

public static final String OAS_PATH_SEPARATOR = "/";
public static final String ESCAPE_PATTERN = "([\\[\\]\\\\?!<>@#&~'`*\\-=^+();:\\/{}\\s|.$])";
public static final String ESCAPE_PATTERN_FOR_MODIFIER = "([\\[\\]\\\\?!<>@#&~'`*\\-=^+();:\\/{}\\s|.$]_)";
public static final String ESCAPE_PATTERN_FOR_MODIFIER = "([\\[\\]\\\\?!<>@#&~'`*\\_\\-=^+();:\\/{}\\s|.$])";
public static final String REGEX_WITHOUT_SPECIAL_CHARACTERS = "\\b[_a-zA-Z][_a-zA-Z0-9]*\\b";
public static final String REGEX_WORDS_STARTING_WITH_NUMBERS = "^[0-9].*";
public static final String REGEX_ONLY_NUMBERS_OR_NUMBERS_WITH_SPECIAL_CHARACTERS = "\\b[0-9([\\[\\]\\\\?!<>@#&~'`" +
Expand Down
Loading

0 comments on commit 83bdd3d

Please sign in to comment.