[BUG] [Java] [Spring] Fix #16561 by marking a requestBody as @NotNull if it is required#22291
Conversation
c7d2fcc to
1daa7fc
Compare
d89bb0f to
1316aee
Compare
|
RequestBody is by default mandatory in spring See |
It is, yes. All this PR does is add the @NotNull annotation if the body is required, to aid with static code analysis. |
|
But make no sense when spring is already handle the required field. I see no benefit to add @NotNull here. |
|
But that is not a good reason to introduce unnecessary code, what is not needed |
|
We may have to disagree on that one; I think static analysis tools are a very useful thing to have in your toolbox, and would encourage their use. |
|
Of course, I use such tools too, but you're treating a symptom, not the cause. In my view, it's bad practice to introduce code that offers no added value simply to satisfy a tool analysis. I'd say the same for test code. You should only add code if it provides added value. For example, the code confused me massively because I thought Spring had changed its API and RequestBody no longer had the required flag. From my perspective, the code is now even harder to read and suggests an incorrect process to the developer. In this case, the introduced annotation adds no value whatsoever and doesn't change the fact that null RequestBody is already protected against null. If your tool can't handle it, the problem lies with the tool itself, not the code. |
|
If you consider the matrix of Java frameworks on one side, and the various static analysis tools along the other, in practical terms settling on a common, already widely used annotation - NotNull - is going to be far more practical than completing that matrix. |
|
Is it intentional that the formatting has two spaces after the annotation?
|
|
Yes. The same mustache template is used in two places; one ( |
|
Could we not handle that issue by aligning the two E.g., we change Then your change would just be something akin to |
Yeah, a fair point. I'll get to that shortly. |
de1d62d to
963dafe
Compare
|
OK, all updated. Thanks for the feedback! |
…if it is required.
963dafe to
c8acffc
Compare
|
@wing328 spring-projects/spring-framework#18068 „
Furthermore, it may be a breaking change for many in error handling if bean validation takes effect first instead of Spring validation. |
That's where we disagree. Does it change the way Spring checks for bodies that are present? No. Does it change the way ...
Additionally, I'll highlight that the generator already
However, I'll let the maintainers decide which way to go on this one, and keep this MR up to date until they do. |
|
Well, that's a strange comparison, because nullable is not a bean validation annotation and has a completely different meaning. Secondly, my Sonar understands the Spring check and doesn't complain about missing NotNulls. And as you can see from NotNull in pathvariable, it causes problems and is also unnecessary. So the error is more likely due to the fact that you shouldn't overwrite the spring defaults. |
|
great discussion/debate will add an option to make both sides happy after merging this PR |
This fixes #16561:
A required request body is now marked as
@NotNull.@cachescrubber (2022/02)
@welshm (2022/02)
@MelleD (2022/02)
@atextor (2022/02)
@manedev79 (2022/02)
@javisst (2022/02)
@borsch (2022/02)
@banlevente (2022/02)
@Zomzog (2022/09)
@martin-mfg (2023/08)
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)