Skip to content

Commit

Permalink
Merge pull request #2078 from ballerina-platform/openapi-example
Browse files Browse the repository at this point in the history
Remove the resource parameter annotation restriction
  • Loading branch information
TharmiganK authored Jul 22, 2024
2 parents 0a0b7ac + 3392b2e commit 883eb41
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 " +
Expand All @@ -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);
}

Expand Down Expand Up @@ -400,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ public static void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ct
continue;
}
if (!HTTP.equals(nameSymbolOptional.get())) {
reportInvalidParameterAnnotation(ctx, paramLocation, paramName);
continue;
}

Expand Down Expand Up @@ -452,7 +451,6 @@ public static void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ct
break;
}
default:
reportInvalidParameterAnnotation(ctx, paramLocation, paramName);
break;
}
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -188,7 +192,8 @@ void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ctx, FunctionDef
int index = 0;
for (ParameterSymbol param : parametersOptional.get()) {
List<AnnotationSymbol> 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++));
Expand Down Expand Up @@ -226,6 +231,15 @@ void extractInputParamTypeAndValidate(SyntaxNodeAnalysisContext ctx, FunctionDef
}
}

private boolean isHttpPackageAnnotationTypeDesc(TypeSymbol typeSymbol) {
Optional<ModuleSymbol> 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<AnnotationSymbol> annotations = parameterSymbol.annotations().stream()
.filter(annotationSymbol -> annotationSymbol.typeDescriptor().isPresent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -153,7 +154,10 @@ private NodeList<ModuleMemberDeclarationNode> updateMemberNodes(NodeList<ModuleM
RequiredParameterNode param = (RequiredParameterNode) parameterNode;
//add the annotation
AnnotationNode payloadAnnotation = getHttpPayloadAnnotation();
NodeList<AnnotationNode> annotations = NodeFactory.createNodeList();
NodeList<AnnotationNode> annotations = param.annotations();
if (Objects.isNull(annotations)) {
annotations = NodeFactory.createNodeList();
}
NodeList<AnnotationNode> updatedAnnotations = annotations.add(payloadAnnotation);
RequiredParameterNode.RequiredParameterNodeModifier paramModifier = param.modify();
paramModifier.withAnnotations(updatedAnnotations);
Expand Down

0 comments on commit 883eb41

Please sign in to comment.