-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Response types using Nullable/oneOf are invisible in swagger-ui (OpenApi3) #2071
Comments
Just nice, how are we supposed to express a nullable Pet otherwise? |
See: https://stackoverflow.com/a/48114924/876814 (they use allOf - we could use that too) But maybe we need to raise an issue in Swagger UI to fix that? |
Ref: OAI/OpenAPI-Specification#1368 @handrews is this:
the correct syntax for referencing a nullable Pet - or must we use "allOf"? |
The other question besides this is whether it makes sense that a response will be indicated as nullable when it's a reference type (C# class) and non-nullable when it's a value type. That inference doesn't make a lot of sense to me because whether the API may return an empty response or not is not a matter of value-type vs. reference-type. |
@RicoSuter when there is only one entry, @softworkz I prefer to leave out irrelevant fields rather than sending literal |
@handrews - I wasn't referring to nullable fields inside a schema. In this case, it's just about the root response object itself. |
@softworkz Oh, I see. When you say that the root response could be nullable do you man that the response would be an |
That is correct - but ASP.NET Core will send a So in theory this nullable + oneOf should be displayed correctly in Swagger UI - @softworkz can you open an issue in the Swagger UI repo (and reference this issue)? |
@handrews thanks for your fast answer! |
Would this be fixed by using allOf? I haven't tried yet... |
LOL. I hate strongly typed languages with inflexible frameworks 😝 |
I love strong typing.. Thanks a lot for your advise, though! |
Yeah me too :-) BTW: With C# 8's Nullable Reference Types the default will be non nullable for this return - and only if you add ? to the type it will produce this null + oneOf, for more information, see https://blog.rsuter.com/the-output-of-nullable-reference-types-and-how-to-reflect-it/ |
I've read about it. But this behavior can't be introduced to existing projects. At least I can't imagine how this could work.. |
As soon as you enable the feature the whole JSON Schema/OpenAPI generator would behave as if
were set... and to make something nullable you'd need to add |
But even in the current state, I wonder whether it's a good idea to declare responses as nullable by default. |
What I mean is that it's not typically the intent of somebody who is expicitly declaring a response type that it would be nullable. |
Maybe it would make sense to add a setting like that which only applies to response types. But the way it is now is correct - you really can return null and it would send |
...right...and really in most cases not reflecting the actual behavior of the API. |
We would need to add a new setting and use it here to initialize isNullable: |
But I think we still need to create an issue in Swagger UI - as this will be probably a problem for all these nullable + oneOfs (also between DTO schemas, etc.) - can you do that? |
I've worked through their stuff half a year ago to fix some authentication issue but I'm a bit short of time right now. I've already spent a too much on doc generation.. But I'll send you another PR with something different that might be of interest. |
I just ran into this too. Is there any progress? |
I'll add a new setting DefaultResponseReferenceTypeNullHandling which is Null by default (avoid breaking changes) and can be changed to NotNull, ok? |
Sounds great to me! |
v12.2.5 |
@acmajia and @softworkz |
@RicoSuter |
Between this one and OAI/OpenAPI-Specification#1368 , really having trouble finding the final decision on this. Is the "correct" syntax A:
or B:
? |
Can anyone shed light on my question ^ ? @RicoSuter ? @acmajia ? |
I’d say b) but i think recently (oai 3.1) they also introduced the type “null” (Same as Json schema) and both are valid.. nswag/njsonschema should support both |
This can be seen for example in NSwag.Sample.NETCore22 (unmodified):
Operation /pet/{petId}:
Definition:
The schemas for the other responses are visible because they are array types. But the problem exists for all single object return types.
The only workaround I found is
config.DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull;
But that doesn't seem to be a good solution...
The text was updated successfully, but these errors were encountered: