From 7bf5e95e3f4a261debc58664d36cf2544ee42ac7 Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Wed, 17 Jul 2024 16:12:16 +0530 Subject: [PATCH 1/3] Remove parameter annotation restriction --- .../stdlib/http/compiler/CompilerPluginTest.java | 10 ++++------ .../http/compiler/CompilerPluginTestConstants.java | 1 - .../stdlib/http/compiler/HttpResourceValidator.java | 7 ------- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java index e2f57d478f..07ec5b9700 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java @@ -138,7 +138,7 @@ public void testInValidInputPayloadArgs() { Package currentPackage = loadPackage("sample_package_4"); PackageCompilation compilation = currentPackage.getCompilation(); DiagnosticResult diagnosticResult = compilation.diagnosticResult(); - Assert.assertEquals(diagnosticResult.errorCount(), 8); + Assert.assertEquals(diagnosticResult.errorCount(), 7); assertError(diagnosticResult, 0, "invalid multiple resource parameter annotations for 'abc': expected one of " + "the following types: 'http:Payload', 'http:CallerInfo', 'http:Header', 'http:Query'", HTTP_108); assertError(diagnosticResult, 1, "invalid usage of payload annotation for a non entity body " + @@ -147,13 +147,11 @@ public void testInValidInputPayloadArgs() { "resource : 'head'. Use an accessor that supports entity body", HTTP_129); assertError(diagnosticResult, 3, "invalid usage of payload annotation for a non entity body resource" + " : 'options'. Use an accessor that supports entity body", HTTP_129); - assertError(diagnosticResult, 4, "invalid annotation type on param 'a': expected one of the following types: " + - "'http:Payload', 'http:CallerInfo', 'http:Header', 'http:Query'", CompilerPluginTestConstants.HTTP_104); - assertTrue(diagnosticResult, 5, "invalid payload parameter type: 'string|ballerina/http:", + assertTrue(diagnosticResult, 4, "invalid payload parameter type: 'string|ballerina/http:", CompilerPluginTestConstants.HTTP_107); - assertTrue(diagnosticResult, 6, "invalid payload parameter type:", + assertTrue(diagnosticResult, 5, "invalid payload parameter type:", CompilerPluginTestConstants.HTTP_107); - assertTrue(diagnosticResult, 7, "invalid payload parameter type:", + assertTrue(diagnosticResult, 6, "invalid payload parameter type:", CompilerPluginTestConstants.HTTP_107); } diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTestConstants.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTestConstants.java index 555781b659..f7559fd76e 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTestConstants.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTestConstants.java @@ -26,7 +26,6 @@ private CompilerPluginTestConstants() {} public static final String HTTP_101 = "HTTP_101"; public static final String HTTP_102 = "HTTP_102"; - public static final String HTTP_104 = "HTTP_104"; public static final String HTTP_105 = "HTTP_105"; public static final String HTTP_106 = "HTTP_106"; public static final String HTTP_107 = "HTTP_107"; diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpResourceValidator.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpResourceValidator.java index e591774203..77d40558c7 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpResourceValidator.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/HttpResourceValidator.java @@ -382,7 +382,6 @@ public static void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ct continue; } if (!HTTP.equals(nameSymbolOptional.get())) { - reportInvalidParameterAnnotation(ctx, paramLocation, paramName); continue; } @@ -452,7 +451,6 @@ public static void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ct break; } default: - reportInvalidParameterAnnotation(ctx, paramLocation, paramName); break; } } @@ -921,11 +919,6 @@ private static boolean isValidReturnTypeWithCaller(TypeSymbol returnTypeDescript } } - private static void reportInvalidParameterAnnotation(SyntaxNodeAnalysisContext ctx, Location location, - String paramName) { - updateDiagnostic(ctx, location, HttpDiagnosticCodes.HTTP_104, paramName); - } - private static void reportInvalidParameter(SyntaxNodeAnalysisContext ctx, Location location, String paramName) { updateDiagnostic(ctx, location, HttpDiagnosticCodes.HTTP_105, paramName); From f524465c57907c3ef08a9dc201162f7c17c9e14a Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 18 Jul 2024 11:17:22 +0530 Subject: [PATCH 2/3] Fix payload annotation modifier --- .../codemodifier/HttpPayloadParamIdentifier.java | 16 +++++++++++++++- .../PayloadAnnotationModifierTask.java | 6 +++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/HttpPayloadParamIdentifier.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/HttpPayloadParamIdentifier.java index 7bf304e6a6..902560d9ab 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/HttpPayloadParamIdentifier.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/HttpPayloadParamIdentifier.java @@ -18,7 +18,9 @@ package io.ballerina.stdlib.http.compiler.codemodifier; +import io.ballerina.compiler.api.ModuleID; import io.ballerina.compiler.api.symbols.AnnotationSymbol; +import io.ballerina.compiler.api.symbols.ModuleSymbol; import io.ballerina.compiler.api.symbols.ParameterSymbol; import io.ballerina.compiler.api.symbols.ResourceMethodSymbol; import io.ballerina.compiler.api.symbols.Symbol; @@ -52,9 +54,11 @@ import java.util.stream.Collectors; import static io.ballerina.stdlib.http.compiler.Constants.ANYDATA; +import static io.ballerina.stdlib.http.compiler.Constants.BALLERINA; import static io.ballerina.stdlib.http.compiler.Constants.BYTE_ARRAY; import static io.ballerina.stdlib.http.compiler.Constants.GET; import static io.ballerina.stdlib.http.compiler.Constants.HEAD; +import static io.ballerina.stdlib.http.compiler.Constants.HTTP; import static io.ballerina.stdlib.http.compiler.Constants.MAP_OF_ANYDATA; import static io.ballerina.stdlib.http.compiler.Constants.NIL; import static io.ballerina.stdlib.http.compiler.Constants.OPTIONS; @@ -188,7 +192,8 @@ void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ctx, FunctionDef int index = 0; for (ParameterSymbol param : parametersOptional.get()) { List annotations = param.annotations().stream() - .filter(annotationSymbol -> annotationSymbol.typeDescriptor().isPresent()) + .filter(annotationSymbol -> annotationSymbol.typeDescriptor().isPresent() && + isHttpPackageAnnotationTypeDesc(annotationSymbol.typeDescriptor().get())) .collect(Collectors.toList()); if (annotations.isEmpty()) { nonAnnotatedParams.add(new ParamData(param, index++)); @@ -226,6 +231,15 @@ void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ctx, FunctionDef } } + private boolean isHttpPackageAnnotationTypeDesc(TypeSymbol typeSymbol) { + Optional module = typeSymbol.getModule(); + if (module.isEmpty()) { + return false; + } + ModuleID id = module.get().id(); + return id.orgName().equals(BALLERINA) && id.moduleName().startsWith(HTTP); + } + public static void validateAnnotatedParams(ParameterSymbol parameterSymbol, ParamAvailability paramAvailability) { List annotations = parameterSymbol.annotations().stream() .filter(annotationSymbol -> annotationSymbol.typeDescriptor().isPresent()) diff --git a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/PayloadAnnotationModifierTask.java b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/PayloadAnnotationModifierTask.java index 86f7d9242b..e43542777c 100644 --- a/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/PayloadAnnotationModifierTask.java +++ b/compiler-plugin/src/main/java/io/ballerina/stdlib/http/compiler/codemodifier/PayloadAnnotationModifierTask.java @@ -53,6 +53,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; /** * {@code HttpPayloadParamIdentifier} injects the @http:Payload annotation to the Payload param which found during the @@ -153,7 +154,10 @@ private NodeList updateMemberNodes(NodeList annotations = NodeFactory.createNodeList(); + NodeList annotations = param.annotations(); + if (Objects.isNull(annotations)) { + annotations = NodeFactory.createNodeList(); + } NodeList updatedAnnotations = annotations.add(payloadAnnotation); RequiredParameterNode.RequiredParameterNodeModifier paramModifier = param.modify(); paramModifier.withAnnotations(updatedAnnotations); From 206cb621a35c41664b361ab0938d3a95f4b109ba Mon Sep 17 00:00:00 2001 From: TharmiganK Date: Thu, 18 Jul 2024 13:13:31 +0530 Subject: [PATCH 3/3] Fix compiler test failure --- .../http/compiler/CompilerPluginTest.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java index 07ec5b9700..4aec08b487 100644 --- a/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java +++ b/compiler-plugin-tests/src/test/java/io/ballerina/stdlib/http/compiler/CompilerPluginTest.java @@ -398,21 +398,20 @@ public void testResourceErrorPositions() { Package currentPackage = loadPackage("sample_package_15"); PackageCompilation compilation = currentPackage.getCompilation(); DiagnosticResult diagnosticResult = compilation.diagnosticResult(); - Assert.assertEquals(diagnosticResult.errorCount(), 13); + Assert.assertEquals(diagnosticResult.errorCount(), 12); // only testing the error locations assertErrorPosition(diagnosticResult, 0, "(29:44,29:60)"); - assertErrorPosition(diagnosticResult, 1, "(42:86,42:87)"); - assertErrorPosition(diagnosticResult, 2, "(46:57,46:60)"); - assertErrorPosition(diagnosticResult, 3, "(50:63,50:66)"); - assertErrorPosition(diagnosticResult, 4, "(54:66,54:69)"); - assertErrorPosition(diagnosticResult, 5, "(58:77,58:80)"); - assertErrorPosition(diagnosticResult, 6, "(62:76,62:79)"); - assertErrorPosition(diagnosticResult, 7, "(66:76,66:82)"); - assertErrorPosition(diagnosticResult, 8, "(73:45,73:46)"); - assertErrorPosition(diagnosticResult, 9, "(81:43,81:46)"); - assertErrorPosition(diagnosticResult, 10, "(81:61,81:64)"); - assertErrorPosition(diagnosticResult, 11, "(81:79,81:82)"); - assertErrorPosition(diagnosticResult, 12, "(85:77,85:93)"); + assertErrorPosition(diagnosticResult, 1, "(46:57,46:60)"); + assertErrorPosition(diagnosticResult, 2, "(50:63,50:66)"); + assertErrorPosition(diagnosticResult, 3, "(54:66,54:69)"); + assertErrorPosition(diagnosticResult, 4, "(58:77,58:80)"); + assertErrorPosition(diagnosticResult, 5, "(62:76,62:79)"); + assertErrorPosition(diagnosticResult, 6, "(66:76,66:82)"); + assertErrorPosition(diagnosticResult, 7, "(73:45,73:46)"); + assertErrorPosition(diagnosticResult, 8, "(81:43,81:46)"); + assertErrorPosition(diagnosticResult, 9, "(81:61,81:64)"); + assertErrorPosition(diagnosticResult, 10, "(81:79,81:82)"); + assertErrorPosition(diagnosticResult, 11, "(85:77,85:93)"); } @Test