-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
[BUG] [Java] [Spring] Fix #16561 by marking a requestBody as @NotNull if it is required #22291
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
base: master
Are you sure you want to change the base?
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. |
0ee0ba9 to
de1d62d
Compare
…if it is required.
de1d62d to
963dafe
Compare
|
OK, all updated. Thanks for the feedback! |
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)