Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ruleKey": "S6856",
"hasTruePositives": false,
"falseNegatives": 45,
"falseNegatives": 46,
"falsePositives": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@

public class MissingPathVariableAnnotationCheckSample {

class ParentController {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the parent is in a different file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At line 30 there is a case marked as FP where the MissingParentChildController extends MissingPathVariableParentInDifferentSample which is a different file. In those cases we do not have access to the parent class declaration and so we cannot see its methods, hence the FP

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you 👍, I missed it

@ModelAttribute("viewCfg")
public String getView(@PathVariable("view") final String view){
return "";
}
}
class ChildController extends ParentController {
@GetMapping("/model/{view}") //Compliant, parent class defines 'view' path var in the model attribute
public String list(@ModelAttribute("viewCfg") final String viewConfig){
return "";
}
}
class MissingParentChildController extends MissingPathVariableParentInDifferentSample {
@GetMapping("/model/{view}") // Noncompliant
// FP: parent class in different file, cannot collect the model attribute
public String list(@ModelAttribute("parentView") final String viewConfig){
return "";
}
}

@GetMapping("/{name:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{ext:\\.[a-z]+}") // Noncompliant
public void handleWithoutExt(@PathVariable String name, @PathVariable String version) {}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package checks.spring;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;

public class MissingPathVariableParentInDifferentSample {
@ModelAttribute("parentView")
public String getView(@PathVariable("view") final String view){
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ public void visitNode(Tree tree) {
}

Set<String> modelAttributeMethodParameter = extractModelAttributeMethodParameter(methods);
modelAttributeMethodParameter.addAll(addInheritedModelAttributeMethodParameter(modelAttributeMethodParameter, clazzTree));

checkParametersAndPathTemplate(methods, modelAttributeMethodParameter, requestMappingTemplateVariables);
}

private static Set<String> extractModelAttributeMethodParameter(List<MethodTree> methods){
Set<java.lang.String> modelAttributeMethodParameter = new HashSet<>();
Set<String> modelAttributeMethodParameter = new HashSet<>();
for (var method : methods) {
if (!method.symbol().metadata().isAnnotatedWith(MODEL_ATTRIBUTE_ANNOTATION)) {
continue;
Expand All @@ -98,6 +99,23 @@ private static Set<String> extractModelAttributeMethodParameter(List<MethodTree>
return modelAttributeMethodParameter;
}

private static Set<String> addInheritedModelAttributeMethodParameter(Set<String> modelAttributeMethodParameters, ClassTree clazz){
if(clazz.superClass() == null){
return modelAttributeMethodParameters;
}
Type superClass = clazz.superClass().symbolType();
ClassTree declaration = superClass.symbol().declaration();
if(declaration != null){
List<MethodTree> methods = declaration.members().stream()
.filter(member -> member.is(Tree.Kind.METHOD))
.map(MethodTree.class::cast)
.toList();
modelAttributeMethodParameters.addAll(extractModelAttributeMethodParameter(methods));
modelAttributeMethodParameters.addAll(addInheritedModelAttributeMethodParameter(modelAttributeMethodParameters, declaration));
}
return modelAttributeMethodParameters;
}

private void checkParametersAndPathTemplate(List<MethodTree> methods, Set<String> modelAttributeMethodParameter, Set<String> requestMappingTemplateVariables) {
for (var method : methods) {
if (!method.symbol().metadata().isAnnotatedWith(MODEL_ATTRIBUTE_ANNOTATION)) {
Expand Down