From 6d0637bc4edc5dbf0740a80ce28cc0920604ad2c Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 4 May 2021 20:59:22 +0530 Subject: [PATCH 1/3] Fix validation for listener init argument as included record params --- .../stdlib/websubhub/CompilerPluginTest.java | 8 ++ .../ballerina_sources/sample_14/.gitignore | 1 + .../sample_14/Ballerina.toml | 7 ++ .../ballerina_sources/sample_14/service.bal | 97 +++++++++++++++++++ .../ServiceDeclarationValidator.java | 36 +++++-- 5 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore create mode 100644 websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml create mode 100644 websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal diff --git a/websubhub-compiler-plugin-test/src/test/java/io/ballerina/stdlib/websubhub/CompilerPluginTest.java b/websubhub-compiler-plugin-test/src/test/java/io/ballerina/stdlib/websubhub/CompilerPluginTest.java index 5f75a3f3..ff34c599 100644 --- a/websubhub-compiler-plugin-test/src/test/java/io/ballerina/stdlib/websubhub/CompilerPluginTest.java +++ b/websubhub-compiler-plugin-test/src/test/java/io/ballerina/stdlib/websubhub/CompilerPluginTest.java @@ -235,6 +235,14 @@ public void testCompilerPluginForParamOrder() { Assert.assertEquals(diagnostic.message(), expectedMsg); } + @Test + public void testValidServiceDeclarationWithIncludedRecordParams() { + Package currentPackage = loadPackage("sample_14"); + PackageCompilation compilation = currentPackage.getCompilation(); + DiagnosticResult diagnosticResult = compilation.diagnosticResult(); + Assert.assertEquals(diagnosticResult.diagnostics().size(), 0); + } + private Package loadPackage(String path) { Path projectDirPath = RESOURCE_DIRECTORY.resolve(path); BuildProject project = BuildProject.load(getEnvironmentBuilder(), projectDirPath); diff --git a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore new file mode 100644 index 00000000..1de56593 --- /dev/null +++ b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore @@ -0,0 +1 @@ +target \ No newline at end of file diff --git a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml new file mode 100644 index 00000000..9c105fcf --- /dev/null +++ b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml @@ -0,0 +1,7 @@ +[package] +org = "websubhub_test" +name = "sample_14" +version = "0.1.0" + +[build-options] +observabilityIncluded = true \ No newline at end of file diff --git a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal new file mode 100644 index 00000000..fb9265f9 --- /dev/null +++ b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal @@ -0,0 +1,97 @@ +import ballerina/websubhub as foo; +import ballerina/http; +import ballerina/io; + +listener foo:Listener securedHub = new(9090, + secureSocket = { + key: { + certFile: "../resources/public.crt", + keyFile: "../resources/private.key" + } + } +); +service /websubhub on securedHub { + + isolated remote function onRegisterTopic(foo:TopicRegistration message) + returns foo:TopicRegistrationSuccess|foo:TopicRegistrationError { + if (message.topic == "test") { + return foo:TOPIC_REGISTRATION_SUCCESS; + } else { + return foo:TOPIC_REGISTRATION_ERROR; + } + } + + isolated remote function onDeregisterTopic(foo:TopicDeregistration message, http:Request baseRequest) + returns foo:TopicDeregistrationSuccess|foo:TopicDeregistrationError { + + map body = { isDeregisterSuccess: "true" }; + foo:TopicDeregistrationSuccess deregisterResult = { + body + }; + if (message.topic == "test") { + return deregisterResult; + } else { + return error foo:TopicDeregistrationError("Topic Deregistration Failed!"); + } + } + + isolated remote function onUpdateMessage(foo:UpdateMessage message) + returns foo:Acknowledgement|foo:UpdateMessageError { + return foo:ACKNOWLEDGEMENT; + } + + isolated remote function onSubscription(foo:Subscription msg) + returns foo:SubscriptionAccepted|foo:SubscriptionPermanentRedirect|foo:SubscriptionTemporaryRedirect + |foo:BadSubscriptionError|foo:InternalSubscriptionError { + foo:SubscriptionAccepted successResult = { + body: >{ + isSuccess: "true" + } + }; + if (msg.hubTopic == "test") { + return successResult; + } else if (msg.hubTopic == "test1") { + return successResult; + } else { + return error foo:BadSubscriptionError("Bad subscription"); + } + } + + isolated remote function onSubscriptionValidation(foo:Subscription msg) + returns foo:SubscriptionDeniedError? { + if (msg.hubTopic == "test1") { + return error foo:SubscriptionDeniedError("Denied subscription for topic 'test1'"); + } + return (); + } + + isolated remote function onSubscriptionIntentVerified(foo:VerifiedSubscription msg) { + io:println("Subscription Intent verified invoked!"); + } + + isolated remote function onUnsubscription(foo:Unsubscription msg) + returns foo:UnsubscriptionAccepted|foo:BadUnsubscriptionError|foo:InternalUnsubscriptionError { + if (msg.hubTopic == "test" || msg.hubTopic == "test1" ) { + foo:UnsubscriptionAccepted successResult = { + body: >{ + isSuccess: "true" + } + }; + return successResult; + } else { + return error foo:BadUnsubscriptionError("Denied unsubscription for topic '" + msg.hubTopic + "'"); + } + } + + isolated remote function onUnsubscriptionValidation(foo:Unsubscription msg) + returns foo:UnsubscriptionDeniedError? { + if (msg.hubTopic == "test1") { + return error foo:UnsubscriptionDeniedError("Denied subscription for topic 'test1'"); + } + return (); + } + + isolated remote function onUnsubscriptionIntentVerified(foo:VerifiedUnsubscription msg){ + io:println("Unsubscription Intent verified invoked!"); + } +} diff --git a/websubhub-compiler-plugin/src/main/java/io/ballerina/stdlib/websubhub/task/validator/ServiceDeclarationValidator.java b/websubhub-compiler-plugin/src/main/java/io/ballerina/stdlib/websubhub/task/validator/ServiceDeclarationValidator.java index 0a0f35c6..5f22a34e 100644 --- a/websubhub-compiler-plugin/src/main/java/io/ballerina/stdlib/websubhub/task/validator/ServiceDeclarationValidator.java +++ b/websubhub-compiler-plugin/src/main/java/io/ballerina/stdlib/websubhub/task/validator/ServiceDeclarationValidator.java @@ -11,9 +11,11 @@ import io.ballerina.compiler.api.symbols.UnionTypeSymbol; import io.ballerina.compiler.syntax.tree.FunctionArgumentNode; import io.ballerina.compiler.syntax.tree.FunctionDefinitionNode; +import io.ballerina.compiler.syntax.tree.NamedArgumentNode; import io.ballerina.compiler.syntax.tree.NodeLocation; import io.ballerina.compiler.syntax.tree.ParenthesizedArgList; import io.ballerina.compiler.syntax.tree.PositionalArgumentNode; +import io.ballerina.compiler.syntax.tree.RestArgumentNode; import io.ballerina.compiler.syntax.tree.SeparatedNodeList; import io.ballerina.compiler.syntax.tree.ServiceDeclarationNode; import io.ballerina.compiler.syntax.tree.SyntaxKind; @@ -151,20 +153,34 @@ private void validateListenerArguments(SyntaxNodeAnalysisContext context, private void verifyListenerArgType(SyntaxNodeAnalysisContext context, NodeLocation location, SeparatedNodeList functionArgs) { if (functionArgs.size() >= 2) { - PositionalArgumentNode firstArg = (PositionalArgumentNode) functionArgs.get(0); - PositionalArgumentNode secondArg = (PositionalArgumentNode) functionArgs.get(1); - SyntaxKind firstArgSyntaxKind = firstArg.expression().kind(); - SyntaxKind secondArgSyntaxKind = secondArg.expression().kind(); - if ((firstArgSyntaxKind == SyntaxKind.SIMPLE_NAME_REFERENCE - || firstArgSyntaxKind == SyntaxKind.MAPPING_CONSTRUCTOR) - && (secondArgSyntaxKind == SyntaxKind.SIMPLE_NAME_REFERENCE - || secondArgSyntaxKind == SyntaxKind.MAPPING_CONSTRUCTOR)) { - WebSubHubDiagnosticCodes errorCode = WebSubHubDiagnosticCodes.WEBSUBHUB_101; - updateContext(context, errorCode, location); + Optional firstArgSyntaxKindOpt = getArgSyntaxKind(functionArgs.get(0)); + Optional secondArgSyntaxKindOpt = getArgSyntaxKind(functionArgs.get(1)); + if (firstArgSyntaxKindOpt.isPresent() && secondArgSyntaxKindOpt.isPresent()) { + SyntaxKind firstArgSyntaxKind = firstArgSyntaxKindOpt.get(); + SyntaxKind secondArgSyntaxKind = secondArgSyntaxKindOpt.get(); + if ((firstArgSyntaxKind == SyntaxKind.SIMPLE_NAME_REFERENCE + || firstArgSyntaxKind == SyntaxKind.MAPPING_CONSTRUCTOR) + && (secondArgSyntaxKind == SyntaxKind.SIMPLE_NAME_REFERENCE + || secondArgSyntaxKind == SyntaxKind.MAPPING_CONSTRUCTOR)) { + WebSubHubDiagnosticCodes errorCode = WebSubHubDiagnosticCodes.WEBSUBHUB_101; + updateContext(context, errorCode, location); + } } } } + private Optional getArgSyntaxKind(FunctionArgumentNode argument) { + SyntaxKind syntaxKind = null; + if (argument instanceof PositionalArgumentNode) { + syntaxKind = ((PositionalArgumentNode) argument).expression().kind(); + } else if (argument instanceof NamedArgumentNode) { + syntaxKind = ((NamedArgumentNode) argument).expression().kind(); + } else if (argument instanceof RestArgumentNode) { + syntaxKind = ((RestArgumentNode) argument).expression().kind(); + } + return Optional.ofNullable(syntaxKind); + } + private void validateRequiredMethodsImplemented(SyntaxNodeAnalysisContext context, List availableFunctionDeclarations, NodeLocation location) { From 412435c5809a3c5ed3e155e17fce7e0260b163d1 Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 4 May 2021 21:39:52 +0530 Subject: [PATCH 2/3] Add new line to end of files --- .../src/test/resources/ballerina_sources/sample_14/.gitignore | 2 +- .../test/resources/ballerina_sources/sample_14/Ballerina.toml | 2 +- .../src/test/resources/ballerina_sources/sample_14/service.bal | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore index 1de56593..eb5a316c 100644 --- a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore +++ b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/.gitignore @@ -1 +1 @@ -target \ No newline at end of file +target diff --git a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml index 9c105fcf..754f6880 100644 --- a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml +++ b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/Ballerina.toml @@ -4,4 +4,4 @@ name = "sample_14" version = "0.1.0" [build-options] -observabilityIncluded = true \ No newline at end of file +observabilityIncluded = true diff --git a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal index fb9265f9..ada200c2 100644 --- a/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal +++ b/websubhub-compiler-plugin-test/src/test/resources/ballerina_sources/sample_14/service.bal @@ -10,6 +10,7 @@ listener foo:Listener securedHub = new(9090, } } ); + service /websubhub on securedHub { isolated remote function onRegisterTopic(foo:TopicRegistration message) From ade7b547bea07b78dff92b64d4206d8d80df076c Mon Sep 17 00:00:00 2001 From: ayeshLK Date: Tue, 4 May 2021 22:01:55 +0530 Subject: [PATCH 3/3] Update change log --- changelog.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/changelog.md b/changelog.md index 27969aea..d1653f02 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Multipart content delivery using websubhub-client. - Subscription Lease seconds expired event. +## [0.2.0-beta.1] - 2021-05-06 + +### Fixed +- [Fix the listener initialization with inline configs compiler plugin error](https://github.com/ballerina-platform/ballerina-standard-library/issues/1304) + ## [0.2.0-alpha8] - 2021-04-22 ### Added - [Add compiler plugin to validate websubhub:Service](https://github.com/ballerina-platform/ballerina-standard-library/issues/1099)