A path variable parameter names a variable that the path template does not have

tx0/java:unknown-path-variabledefaulterrorbug

What it finds

@GetMapping("/users/{id}") with a parameter @PathVariable("userId") long id fails on every request: Spring looks for a variable userId in the path template, finds none and answers with an error. The name of a path variable is the one given to @PathVariable, or else the parameter's own name, and it must appear in braces in the template of the method or its class, as the Spring MVC documentation describes.

Reported is a @PathVariable parameter whose name is in none of the templates of the method's mapping annotation and of a @RequestMapping on its class. Templates must be string literals; a parameter of type Map takes all variables and is left alone.

From the rule's source, unknown_path_variable.rs.

Example from the rule's tests
import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import java.util.Map; @RequestMapping("/orgs/{org}")class Users {  @GetMapping("/users/{id}")  String one(@PathVariable("userId") long id) {tx0 reports: has no variable {userId}    return "";  }   @GetMapping("/users/{id}/items/{item:[0-9]+}")  String item(@PathVariable long id, @PathVariable("item") String item, @PathVariable String org) {    return "";  }   @GetMapping(path = "/teams/{team}")  String team(@PathVariable String name) {tx0 reports: has no variable {name}    return "";  }   @GetMapping("/all/{a}/{b}")  String all(@PathVariable Map<String, String> vars) {    return "";  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:unknown-path-variable" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:unknown-path-variable] reason="..."

tx0 explain tx0/java:unknown-path-variable shows the same in the terminal. Found a false positive? Report it.