FastEndpoints / FastEndpoints

A light-weight REST API development framework for ASP.NET 6 and newer.
https://fast-endpoints.com
MIT License
4.64k stars 277 forks source link

swagger ui not displaying default value for nullable enum properties #726

Closed aj-scram closed 4 months ago

aj-scram commented 4 months ago

so in the operation processor, you're assigning the ref directly to the param object via Schema; this causes invalid swagger which makes the default value not populate

https://github.com/FastEndpoints/FastEndpoints/blob/60efcbdbf7268af398c407b9d638e885807feffa/Src/Swagger/OperationProcessor.cs#L652

see generated swagger from NSwag below image

image

image

Fix is that the $ref needs to be moved to oneOf or allOf collection for the param

image

dj-nitehawk commented 4 months ago

i believe it's being done as a workaround for #699

https://github.com/FastEndpoints/FastEndpoints/blob/56be725f808fc25542d485e7355e3a66ef797cdb/Src/Swagger/OperationProcessor.cs#L651-L657

if we revert the workaround, then a nullable enum property is going to look like this in swagger ui:

image

any ideas what exact swagger spec is needed for swagger ui to render a dropdown as well as honor default values for a nullable enum property?

aj-scram commented 4 months ago

I believe the correction here is to add to allOf instead

I already have an operation processor to do so for MVC controller enums (middle of refactoring towards FE) based on this comment/issue:

https://github.com/RicoSuter/NSwag/issues/3645#issuecomment-1457994616

That was to fix enums not generating as dropdowns as well, but I find using allOf continues to allow defaults to work as expected

There’s also an open issue where someone is asking for it to be configurable between the two

https://github.com/RicoSuter/NJsonSchema/issues/1509#issue-1181080784

NOTE: this won’t fix defaults for array enum parameters, but I’m also not sure how to fix that currently

dj-nitehawk commented 4 months ago

thanks! that helped... check out v5.26.0.26-beta which should be available on nuget soon. i believe it's working as expected now.

aj-scram commented 4 months ago

Much appreciated; if figure out how to get the array defaults (with/ multi selection) working as expected, I’ll report back

Is there any interest in the configurable feature request? Could see about opening a PR for it in the future, if/when I find time