Skip to content

Commit

Permalink
Merge pull request #1552 from lnash94/constraint_support
Browse files Browse the repository at this point in the history
[constraint_support]Sync with master
  • Loading branch information
lnash94 authored Oct 10, 2023
2 parents 5708d09 + 27259f2 commit bbd3925
Show file tree
Hide file tree
Showing 26 changed files with 857 additions and 130 deletions.
50 changes: 25 additions & 25 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
org.gradle.caching=true
group=io.ballerina
version=1.8.0-SNAPSHOT
version=1.8.1-SNAPSHOT

#dependency
ballerinaLangVersion=2201.8.0-20230816-121900-c1174ddd
ballerinaLangVersion=2201.8.0
testngVersion=7.6.1
slf4jVersion=1.7.30
org.gradle.jvmargs=-Xmx4096M -Dfile.encoding=UTF-8
Expand All @@ -15,41 +15,41 @@ swaggerParserVersion=2.1.16
puppycrawlCheckstyleVersion = 10.12.1

# Stdlib Level 01
stdlibIoVersion=1.5.0
stdlibIoVersion=1.6.0
stdlibRegexVersion=1.4.3
stdlibTimeVersion=2.3.0
stdlibUrlVersion=2.3.0
stdlibXmldataVersion=2.6.0
stdlibTimeVersion=2.4.0
stdlibUrlVersion=2.4.0
stdlibXmldataVersion=2.7.0

# Stdlib Level 02
stdlibConstraintVersion=1.3.0
stdlibCryptoVersion=2.4.0
stdlibLogVersion=2.8.1-20230718-085900-36c385c
stdlibOsVersion=1.7.0
stdlibTaskVersion=2.4.0
stdlibConstraintVersion=1.4.0
stdlibCryptoVersion=2.5.0
stdlibLogVersion=2.9.0
stdlibOsVersion=1.8.0
stdlibTaskVersion=2.5.0

# Stdlib Level 03
stdlibCacheVersion=3.6.0
stdlibFileVersion=1.8.0
stdlibMimeVersion=2.8.0
stdlibUuidVersion=1.6.0
stdlibCacheVersion=3.7.0
stdlibFileVersion=1.9.0
stdlibMimeVersion=2.9.0
stdlibUuidVersion=1.7.0

# Stdlib Level 04
stdlibAuthVersion=2.9.0
stdlibJwtVersion=2.9.0
stdlibOAuth2Version=2.9.0
stdlibAuthVersion=2.10.0
stdlibJwtVersion=2.10.0
stdlibOAuth2Version=2.10.0

# Stdlib Level 05
stdlibHttpVersion=2.10.0-20230809-150400-0c9cad9
stdlibHttpVersion=2.10.0

# Stdlib Level 06
stdlibGrpcVersion=1.9.1-20230809-211700-feffbef
stdlibWebsocketVersion=2.9.1-20230809-174700-6942521
stdlibWebsubVersion=2.9.1-20230809-211400-c6c75d2
stdlibGrpcVersion=1.10.0
stdlibWebsocketVersion=2.10.0
stdlibWebsubVersion=2.10.0

# Stdlib Level 07
stdlibGraphqlVersion=1.10.0-20230809-214800-d775b0d
stdlibGraphqlVersion=1.10.0

# Ballerinax Observer
observeVersion=1.1.0
observeInternalVersion=1.1.0
observeVersion=1.2.0
observeInternalVersion=1.2.0
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public String toString() {
httpCodeMap.put("NotImplemented", "501");
httpCodeMap.put("BadGateway", "502");
httpCodeMap.put("ServiceUnavailable", "503");
httpCodeMap.put("GatewayTimeOut", "504");
httpCodeMap.put("GatewayTimeout", "504");
httpCodeMap.put("HttpVersionNotSupported", "505");
httpCodeMap.put("VariantAlsoNegotiates", "506");
httpCodeMap.put("InsufficientStorage", "507");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public enum DiagnosticMessages {
OAS_CONVERTOR_115("OAS_CONVERTOR_115", "Given Ballerina file does not contain any HTTP service.",
DiagnosticSeverity.ERROR),
OAS_CONVERTOR_116("OAS_CONVERTOR_116", "Failed to parser the Number value due to: %s ",
DiagnosticSeverity.ERROR);
DiagnosticSeverity.ERROR),
OAS_CONVERTOR_117("OAS_CONVERTOR_117", "Generated OpenAPI definition does not contain `%s` request" +
" body information, as it's not supported by the OpenAPI tool.",
DiagnosticSeverity.WARNING);

private final String code;
private final String description;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.ballerina.openapi.converter.diagnostic.IncompatibleResourceDiagnostic;
import io.ballerina.openapi.converter.diagnostic.OpenAPIConverterDiagnostic;
import io.ballerina.openapi.converter.utils.ConverterCommonUtils;
import io.ballerina.tools.diagnostics.DiagnosticSeverity;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand Down Expand Up @@ -217,8 +218,12 @@ private Optional<OperationAdaptor> convertResourceToOperation(FunctionDefinition
openAPIParameterMapper.getResourceInputs(components, semanticModel);
if (openAPIParameterMapper.getErrors().size() > 1 || (openAPIParameterMapper.getErrors().size() == 1 &&
!openAPIParameterMapper.getErrors().get(0).getCode().equals("OAS_CONVERTOR_113"))) {
errors.addAll(openAPIParameterMapper.getErrors());
return Optional.empty();
boolean isErrorIncluded = openAPIParameterMapper.getErrors().stream().anyMatch(d ->
DiagnosticSeverity.ERROR.equals(d.getDiagnosticSeverity()));
if (isErrorIncluded) {
errors.addAll(openAPIParameterMapper.getErrors());
return Optional.empty();
}
}
errors.addAll(openAPIParameterMapper.getErrors());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static io.ballerina.openapi.cmd.CmdConstants.GenType.GEN_CLIENT;
import static io.ballerina.openapi.cmd.CmdConstants.GenType.GEN_SERVICE;
import static io.ballerina.openapi.cmd.CmdConstants.OAS_PATH_SEPARATOR;
import static io.ballerina.openapi.cmd.CmdConstants.SUPPORTED_OPENAPI_VERSIONS;
import static io.ballerina.openapi.cmd.CmdConstants.TEST_DIR;
import static io.ballerina.openapi.cmd.CmdConstants.TEST_FILE_NAME;
import static io.ballerina.openapi.cmd.CmdConstants.TYPE_FILE_NAME;
Expand Down Expand Up @@ -103,7 +104,7 @@ public void generateClientAndService(String definitionPath, String serviceName,
// absence of the operationId in operation. Therefor we enable client flag true as default code generation.
// if resource is enabled, we avoid checking operationId.
OpenAPI openAPIDef = GeneratorUtils.normalizeOpenAPI(openAPIPath, !isResource);

checkOpenAPIVersion(openAPIDef);
// Generate service
String concatTitle = serviceName.toLowerCase(Locale.ENGLISH);
String srcFile = concatTitle + "_service.bal";
Expand Down Expand Up @@ -338,6 +339,7 @@ private List<GenSrcFile> generateClientFiles(Path openAPI, Filter filter, boolea
List<GenSrcFile> sourceFiles = new ArrayList<>();
// Normalize OpenAPI definition
OpenAPI openAPIDef = GeneratorUtils.normalizeOpenAPI(openAPI, !isResource);
checkOpenAPIVersion(openAPIDef);
// Generate ballerina service and resources.
OASClientConfig.Builder clientMetaDataBuilder = new OASClientConfig.Builder();
OASClientConfig oasClientConfig = clientMetaDataBuilder
Expand Down Expand Up @@ -409,6 +411,8 @@ public List<GenSrcFile> generateBallerinaService(Path openAPI, String serviceNam
openAPI);
}

checkOpenAPIVersion(openAPIDef);

if (openAPIDef.getInfo().getTitle().isBlank() && (serviceName == null || serviceName.isBlank())) {
openAPIDef.getInfo().setTitle(UNTITLED_SERVICE);
} else {
Expand Down Expand Up @@ -469,4 +473,11 @@ public void setLicenseHeader(String licenseHeader) {
public void setIncludeTestFiles(boolean includeTestFiles) {
this.includeTestFiles = includeTestFiles;
}

private void checkOpenAPIVersion(OpenAPI openAPIDef) {
if (!SUPPORTED_OPENAPI_VERSIONS.contains(openAPIDef.getOpenapi())) {
outStream.printf("WARNING: The tool has not been tested with OpenAPI version %s. " +
"The generated code may potentially contain errors.%n", openAPIDef.getOpenapi());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -169,7 +170,7 @@ public String getValue() {
httpCodeMap.put("501", "NotImplemented");
httpCodeMap.put("502", "BadGateway");
httpCodeMap.put("503", "ServiceUnavailable");
httpCodeMap.put("504", "GatewayTimeOut");
httpCodeMap.put("504", "GatewayTimeout");
httpCodeMap.put("505", "HttpVersionNotSupported");
HTTP_CODES_DES = Collections.unmodifiableMap(httpCodeMap);
}
Expand Down Expand Up @@ -201,5 +202,6 @@ public String getValue() {
// OS specific line separator
public static final String LINE_SEPARATOR = System.lineSeparator();
public static final String DOUBLE_LINE_SEPARATOR = LINE_SEPARATOR + LINE_SEPARATOR;

public static final List<String> SUPPORTED_OPENAPI_VERSIONS =
List.of("2.0", "3.0.0", "3.0.1", "3.0.2", "3.0.3", "3.1.0");
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public Object[][] singleFileProviderForDiagnosticCheck() {
{"duplicated_response.yaml"},
{"complex_oneOf_schema.yaml"},
{"request_body_ref.yaml"},
{"single_allOf.yaml"},
{"vendor_specific_mime_types.yaml"},
{"requestBody_reference_has_inline_object_content_type.yaml"},
{"ballerinax_connector_tests/ably.yaml"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ public void unionPathParameter() throws IOException, BallerinaOpenApiException {

@Test(description = "When path parameter has given allOf data type in ballerina",
expectedExceptions = BallerinaOpenApiException.class,
expectedExceptionsMessageRegExp = "Ballerina does not support object type path .*")
expectedExceptionsMessageRegExp = "Path parameter: 'id' is invalid. " +
"Ballerina does not support object type path parameters.")
public void allOfPathParameter() throws IOException, BallerinaOpenApiException {
Path definitionPath = RESDIR.resolve("swagger/allOf_path_parameter.yaml");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,6 @@ public static List<Diagnostic> getDiagnostics(SyntaxTree syntaxTree) throws Form
return semanticModel.diagnostics();
}

public static List<Diagnostic> getDiagnosticsForGenericService(SyntaxTree serviceSyntaxTree)
throws FormatterException, IOException {
writeFile(servicePath, Formatter.format(serviceSyntaxTree).toSourceCode());
SemanticModel semanticModel = getSemanticModel(servicePath);
return semanticModel.diagnostics();
}

public static List<Diagnostic> getDiagnosticsForService(SyntaxTree serviceSyntaxTree, OpenAPI openAPI,
BallerinaServiceGenerator ballerinaServiceGenerator)
throws FormatterException, IOException, BallerinaOpenApiException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,12 @@ public void testForServiceConfigOnlyWithCors() {
compareWithGeneratedFile(ballerinaFilePath, "service_config_with_cors.yaml");
}

@Test(description = "Generate OpenAPI spec for request body with union type")
public void testForUnionTypeRequestBody() {
Path ballerinaFilePath = RES_DIR.resolve("request_body/union_type.bal");
compareWithGeneratedFile(ballerinaFilePath, "union_type.yaml");
}

@AfterMethod
public void cleanUp() {
deleteDirectory(this.tempDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ public void generateNestedAllOfSchema() throws IOException, BallerinaOpenApiExce
TestUtils.compareGeneratedSyntaxTreewithExpectedSyntaxTree("schema/ballerina/nested_all_of.bal", syntaxTree);
}

@Test(description = "Generate type definition from allOf schema with valid single item")
public void generateAllOfwithValidSingleItem() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/single_item_allOf.yaml");
OpenAPI openAPI = GeneratorUtils.normalizeOpenAPI(definitionPath, true);
BallerinaTypesGenerator ballerinaSchemaGenerator = new BallerinaTypesGenerator(openAPI);
syntaxTree = ballerinaSchemaGenerator.generateSyntaxTree();
TestUtils.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"schema/ballerina/single_item_allOf.bal", syntaxTree);
}

@Test(description = "Tests record generation for nested OneOf schema inside AllOf schema",
expectedExceptions = BallerinaOpenApiException.class,
expectedExceptionsMessageRegExp =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,18 @@ public void testsRefParamsInOpenAPIV31() throws IOException, BallerinaOpenApiExc
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"parameter_with_ref_v31.bal", syntaxTree);
}
}

@Test(description = "Tests for path segments has parameters with extension")
public void testsForPathSegmentHasExtensionType() throws IOException, BallerinaOpenApiException {
Path definitionPath = RES_DIR.resolve("swagger/multiPathParamWithExtensionType.yaml");
OpenAPI openAPI = GeneratorUtils.getOpenAPIFromOpenAPIV3Parser(definitionPath);
OASServiceMetadata oasServiceMetadata = new OASServiceMetadata.Builder()
.withOpenAPI(openAPI)
.withFilters(filter)
.build();
BallerinaServiceGenerator ballerinaServiceGenerator = new BallerinaServiceGenerator(oasServiceMetadata);
syntaxTree = ballerinaServiceGenerator.generateSyntaxTree();
CommonTestFunctions.compareGeneratedSyntaxTreewithExpectedSyntaxTree(
"multiPathParamWithExtensionType.bal", syntaxTree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.ArrayList;
import java.util.List;

import static io.ballerina.openapi.generators.common.TestUtils.getDiagnosticsForGenericService;
import static io.ballerina.openapi.generators.common.TestUtils.getDiagnosticsForService;
import static io.ballerina.openapi.generators.common.TestUtils.normalizeOpenAPI;

Expand Down Expand Up @@ -92,7 +91,7 @@ public void checkDiagnosticIssuesInGenericServiceGen(String yamlFile) throws IOE
.build();
BallerinaServiceGenerator ballerinaServiceGenerator = new BallerinaServiceGenerator(oasServiceMetadata);
syntaxTree = ballerinaServiceGenerator.generateSyntaxTree();
List<Diagnostic> diagnostics = getDiagnosticsForGenericService(syntaxTree);
List<Diagnostic> diagnostics = getDiagnosticsForService(syntaxTree, openAPI, ballerinaServiceGenerator);
boolean hasErrors = diagnostics.stream()
.anyMatch(d -> DiagnosticSeverity.ERROR.equals(d.diagnosticInfo().severity()));
Assert.assertFalse(hasErrors);
Expand Down Expand Up @@ -137,6 +136,7 @@ public Object[][] singleFileProviderForDiagnosticCheck() {
{"complex_oneOf_schema.yaml"},
{"request_body_ref.yaml"},
{"vendor_specific_mime_types.yaml"},
{"single_allOf.yaml"},
// TODO: Uncomment when fixed https://github.com/ballerina-platform/openapi-tools/issues/1415
// {"ballerinax_connector_tests/ably.yaml"},
{"ballerinax_connector_tests/azure.iot.yaml"},
Expand Down
Loading

0 comments on commit bbd3925

Please sign in to comment.