Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARJAVA-5324 @PathVariable must have path template placeholder #5016

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

erwan-serandour
Copy link
Contributor

@erwan-serandour erwan-serandour commented Feb 10, 2025

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also reformat the file before pushing? I think there are a couple of lines non-formatted

.withCheck(new MissingPathVariableAnnotationCheck())
.verifyIssues();
}

@Test
void test_without_semantic(){

Choose a reason for hiding this comment

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

Since we are adding a new method, it might be the good time to extract the filename and the check instance into 2 constants

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

.collect(Collectors.toSet());
pathVariables.stream()
.filter(v -> !allUriParameters.contains(v.value()))
.filter(v -> !v.parameter().symbol().type().is(MAP))

Choose a reason for hiding this comment

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

Maybe this is material for another rule, but my observation is that whatever type of Map you provide as a @PathVariable (Map<Long, Long>, Map<Double,Exception>), spring will always convert it to a Map<String, String> and map all the path variables inside. Maybe this rule is not the right place for this, but I wanted to mention this (we could have a dedicated rule about specifying Map<String, String>, as it's confusing otherwise)

pathVariables.stream()
.filter(v -> !allUriParameters.contains(v.value()))
.filter(v -> !v.parameter().symbol().type().is(MAP))
.forEach(v -> reportIssue(v.parameter(), String.format("Bind path variable \"%s\" to a path parameter.", v.value())));

Choose a reason for hiding this comment

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

I think the messages we are attaching to issues are getting a little bit confusing because of the 2-ways relation between method parameters and URI template variables.
From the spring docs it appears to me that we should refer to something like the id in /path/{id} as a URI template variable, maybe template variable for simplicity, and to the corresponding @PathVariable String id simply as the method parameter, or maybe the PathVariable argument?

So I suggest we refactor the issue messages following this naming convention, because I see for instance:
Bind path variable "id" to a method parameter. and in the other case:
Bind path variable "aVar" to a path parameter. and I think it can be confusing.

Copy link

Quality Gate failed Quality Gate failed

Failed conditions
4 New issues
1 New Critical Issues (required ≤ 0)

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube IDE SonarQube IDE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants