-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Adds @Nullable annotation to Spring Boot generator #20345
base: master
Are you sure you want to change the base?
Adds @Nullable annotation to Spring Boot generator #20345
Conversation
b7a676a
to
b84983a
Compare
@cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @banlevente @Zomzog @martin-mfg Folks, please, have a look |
b84983a
to
f1bb82a
Compare
Split that into 2 commits for an easier review. A bit confused why adding the import of |
It turned out that |
@@ -0,0 +1 @@ | |||
{{^required}}{{^useOptional}}{{#openApiNullable}}{{^isNullable}}@Nullable {{/isNullable}}{{/openApiNullable}}{{^openApiNullable}}@Nullable {{/openApiNullable}}{{/useOptional}}{{/required}} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think for JsonNullable and when you using using Collection which could no be null it make also no sense to set this annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And one more question do you think this make sense when you using bean validation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @MelleD,
Thank for your quick review, appreciate it.
for JsonNullable
Indeed, that was already taken into consideration. If the field is wrapped with either JsonNullable
or Optional
it will not be annotated with @Nullable
. Please, see the tests at the 1st commit.
using Collection
Actually, it is not about just collection but any property with default value. For collections it was more obvious. My fault, thank you for catching it. Fixed that at the 4th commit (and updated samples again at the 5th commit)
when you using bean validation
If I got you right, it is not intersected. See: any property which is not required might be null. Any validation (except @NotNull
of course) bypasses and should bypass any nulls, which is correct. So, the bean validation doesn't prevent NPEs
and it is worth to annotate such fields, I guess
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, that was already taken into consideration. If the field is either wrapped with
JsonNullable
orOptional
it is not annotated with@Nullable
. Please, see the tests at the 1st commit.
Ah I missed that, I just saw useOptional and thought maybe JsonNullable make also no sense :-)
If I got you right, it is not intersected. See: any property which is not required might be null. Any validation (except @NotNull of course) bypasses and should bypass any nulls, which is correct. So, the bean validation doesn't prevent NPEs and it is worth to annotate such fields, I guess
Ok sounds reasonable. For a while I don't use nulls (I use Optional/Enum instead).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some thoughts regarding Optional
but let's discuss them as a separate story. For the moment my colleagues use just regular objects, and I would like to make their life easier with handling NPEs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have some thoughts regarding Optional
It's fine there is no right or wrong everybody have own opinion about Optional.(https://medium.com/97-things/using-optional-nowhere-somewhere-or-everywhere-b1eb5645eab5)
I just say that I have not much experience with null handling ;)
ae17cc9
to
0b1d077
Compare
PR looks good. It is still an open point whether a property like useBeanValidation or useOptional makes sense to switch the annotation on and off. Maybe we should ask other users @wing328 @borsch @martin-mfg What do you think? |
0b1d077
to
ac351df
Compare
Sorry for jumping in again but
Any validator (except
|
ac351df
to
a095eb5
Compare
Yes true but what is when you would like to use nulls without this annotation? You think everybody would like to use it? |
That was a reason why I didn't introduce
If I got you right, you seem to be from the optional-everywhere camp. Well, if the Still,
Beg your pardon, I am talking too much. Let's wait for @wing328 @borsch @martin-mfg opinions. |
How I said Iam fine with the PR thanks 😊 . Just collect some other opinions is there somebody who would like to turn it off to prevent some new issues: “how can I remove this annotation” 😂 |
34809c9
to
a4a3eea
Compare
* issue-14427: [REQ][spring] Null-Safety annotations * issue-17382: [REQ] spring generator add Nullable annotations Motivations: * Have Spring Boot generator client properly annotated for nullability to be able to check code using them with tools like NullAway * As it is related to Spring then the `org.springframework.lang.Nullable` annotation was chosen to avoid discussion which `@Nullable` one is true one * `@NonNull` wasn't used as I didn't see much benefit of it. Anyhow, an empty constructor and/or setters allow to put a `null` value there Modifications: * Adds nullableAnnotation template to handle nullability annotation on vars * Adjust pojo templates to use the nullability template * Adapts tests Modifications: * Runs export_docs_generator.sh script to update samples
a4a3eea
to
f959312
Compare
Motivations:
org.springframework.lang.Nullable
annotation was chosen to avoid discussion which@Nullable
one is true one@NonNull
wasn't used as I didn't see much benefit of it. Anyhow, an empty constructor and/or setters allow to put anull
value thereModifications:
nullableAnnotation
template to handle nullability annotation on varsPR 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.0
minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)