A Spring handler parameter marked as not required has a primitive type, so a request without the value fails
What it finds
A handler parameter bound with required = false is null when the request lacks the value. A primitive such as int or boolean cannot hold null, so Spring rejects such a request with an error instead of calling the handler, which is exactly the request the parameter was made optional for. A wrapper type such as Integer, an Optional, or a defaultValue keeps it working.
Reported is a primitive parameter (other than boolean, which Spring turns into false) annotated @RequestParam, @RequestHeader, @PathVariable, @CookieValue or @RequestAttribute with required = false and no defaultValue.
From the rule's source, optional_primitive_parameter.rs.
import java.util.Optional;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestHeader;import org.springframework.web.bind.annotation.RequestParam; class Pages { @GetMapping("/items") String list( @RequestParam(required = false) int page,tx0 reports: int cannot be null, so a request without this optional @RequestParam value fails @RequestParam(value = "size", required = false, defaultValue = "20") int size, @RequestParam(required = false) Integer offset, @RequestParam(required = false) boolean all, @RequestHeader(name = "X-Count", required = false) long count,tx0 reports: long cannot be null @RequestParam(required = false) Optional<Integer> limit, @RequestParam int depth) { return ""; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:optional-primitive-parameter" = "warning"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:optional-primitive-parameter] reason="..."tx0 explain tx0/java:optional-primitive-parameter shows the same in the terminal. Found a false positive? Report it.