-
-
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
[BUG][Java/spring] useOptional:true is applied only if openApiNullable:true #20407
Comments
@wing328 If you allow me to tag you, your opinion is highly appreciated as well |
I see no reason why both options are related to each other. IMHO Optional is also valid without JsonNullable.
Yes would be great |
TL; DR. Ok, I will try to provide something. Before I start, let's confirm that we are at the same page. Assume there is a spec openapi: 3.0.0
components:
schemas:
Item:
type: object
properties:
foo:
type: string
bar:
type: string
nullable: true Both {
"foo": "someValue"
// "bar" is not mentioned
} ... and {
"foo": "someValue"
"bar": null
} ... have different meanings. Now we have two config parameters
It generates private @Nullable String foo;
private @Nullable String bar = null; Explanations:
It generates private @Nullable String foo;
private JsonNullable<String> bar = JsonNullable.<String>undefined(); Explanations:
It generates private Optional<String> foo = Optional.empty();
private JsonNullable<String> bar = JsonNullable.<String>undefined(); Explanations:
Currently it generates private String foo;
private String bar = null; Explanations:
I am going to change it to private Optional<String> foo = Optional.empty();
private Optional<String> bar = Optional.empty(); Explanations:
So, the algorithm is
Any thoughts or concerns? |
I think point 4 is not correct if you explicitly set to nullable true it should be not an Optional. Also Jackson will not handle that because it will generate a Optional#empty instead of null. IMHO if you look into the semantic in json is absent and set to null semantically the same meaning the value is not there. What is in a POST or GET the difference between both states? |
Frankly speaking, I am not a big fan of using
{
"foo" : "123"
// "bar" is not mentioned, meaning it should be set to its default value "xyz"
}
{
"foo" : "123",
"bar": null // this time I really want to set it to "null" explicitly
} As far as I see the only valid to support 3 states is So, how are we going to handle the nullable
|
No matter how you're going to do it. Someone will complain ;). First of all, JsonNullable is made for a merge patch ( Of course, you might find other use cases, although I don't know whether your example with Jackson would simply work. I think with absent/null Jackson uses the default value, but never tested. You don't have to do everything just because it's possible ;)
Since I don't know how people use it, everyone can decide for themselves whether an optional or a nullable field comes out. |
That's a holy truth, still I wouldn't like to spend time on the PR that will be rejected. So, I would prefer to get your sign off first. In order to keep the discussion focused, let's move forward with small "yes" questions as Socrates recommended. I got the purpose of There are no disagreements at the points 1-3, right? Then we're discussing just point 4 First, you implemented it that
then you seemed to agree that
For the spec openapi: 3.0.0
components:
schemas:
Item:
type: object
properties:
# foo is not mentioned at the required
foo:
type: string
# nullable is not set, assuming it is false by default it will generate private String foo; It is ok or not? I am going to change it to private Optional<String> foo = Optional.empty(); Are you fine with that? |
I thought this is current state, right? There will be no change?
Yes, why not? I also do not remember every detail from my PR. It's a year ago.
yes nullable: false --> Optional But again this is just my personal opinion (and in the end not my decision) and I will not use the feature (because I use optional and jsonnullable), so if you would like to go with Optional for both cases, go ahead. Also fine for me. |
I also use both optional and json nullable so I don't have a strong opinion here. I agree that the behaviour of not having optional parameters as If you wanted to preserve the current behaviour, you could add a third option.
Then the developer can decide what they want (which should also solve your use case).
|
@welshm the only question is what make the nullable true/false then in the spec? If the generated code is the same and also the json is the same. And there is no difference between nullable true/false then it looks redundant 😀 |
I'm not sure I understand - nullable true false in the spec would be based on The generated code is different depending on what options are set based on the discussion above. I feel like I'm not understanding the point of what you're trying to tell me 😅 |
Just want to share that in the openapi normalizer, there's a rule called
For example, one can enable this rule to get |
Correct. We're talking just about case 4
I think the same We seemed to come to conclusion that for the
The only thing to discuss is what to do with The options are:
I don't see good reason for it. Why not wrap it with
That's what I'm tending to do but we're still dicussing
Currently two feature toggles make 4 combinations, adding a new one will lead to handle 8 options. It will be a mess at the mustashe templates I am afraid. A developer should be aware of the new key, we need to discuss its reasonable default, cover all possible 8 cases with tests, update the documentation... to many efforts for me, sorry. So, I am tending to wrap both cases with Still, if you still strictly state that P.S. If we don't come to agreement, does it make sense to amend documentation a bit that currently |
How I said I don’t use it. Go for suggestion. |
Hope, I will do.
My way of thinking is (please, feel free to correct any statement as I am new to OpenAPI)
Does it make sense for you?
|
Yes totally. I can only pass on to you my experience that developers and users are very creative when it comes to using things. I really only use nullable in PATCH, but in some discussions here the argument has often come up that some people also want to use it in POST and GET etc. I can understand your thinking. My argument was rather that if And as I said, I understand your arguments. I won't use it, so you can decide based on the information what you think is better ;) |
Bug Report Checklist
OpenAPI declaration file content or url
Description
Assume we have a very minimal specification (see above) and use Java Spring Boot generator.
With
useOptional:true
everything is fine i.e.... but
Optional
wrappers are applied only ifopenApiNullable:true
.Luckily, it is a default behavior. Still, if I explicitly turn it off
openApiNullable:false
thenuseOptional:true
doesn't have any effectI guess it is a bug. The output should be the very same as above.
Or am I missing an idea behind it? Is it written somewhere in documentation that
useOptional
makes sense only together withopenApiNullable
?@MelleD as you seem to be an expert of using
Optional
, what are your thoughts of it?Should I provide a PR to fix?
openapi-generator version
7.10.0
The text was updated successfully, but these errors were encountered: