Skip to content

Commit

Permalink
Fix payload annotation modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
TharmiganK committed Jul 18, 2024
1 parent 7bf5e95 commit f524465
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
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 f524465

Please sign in to comment.