-
Notifications
You must be signed in to change notification settings - Fork 80
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
Support hiding empty fields #78
Comments
Hi Effy, This is by design - the ExamplesOperationFilter will use whatever JSON serializer settings your app is configured with. This feature was introduced recently by request - it fixed #72 . If you roll back to 4.5.1 it will revert to the previous behaviour, which is to ignore nulls. Or you could change the behaviour of your app, by setting the global serializer settings (recommended): public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
} Or, you could possibly override IContractResolver and pass that into the |
Thank you @mattfrear Alternatively, I believe it would make much sense to allow modifying the SerializerSettings for both Request/Response (Requests should render nulls, yet Responses should render nulls on our use case). Effy |
Hi Effy
Unfortunately IContractResolver wouldn't work, my suggestion was incorrect. Sorry about that.
Can you clarify what makes sense for your use case? Requests should render nulls - yes or no? Responses should render nulls - yes or no? I guess I could extend the |
Thank you for the response @mattfrear On our use case, requests shouldn't render nulls, as they represent basic sample requests. On the responses we don't have a major preference, and we'll probably prefer them to not render nulls as well. I'll try to get into it and publish the PR you recommended. Thanks, |
Cool. If you do a PR then please add tests too, and update the ReadMe. |
Really? In my experience if the clients are .NET, then model binding treats a null the same as a missing property. This follows Postel's law. I'm don't think I want to add more code (i.e. a I'm going to close this because I'm not going to implement it. |
I have the following set @mattfrear Can we get this to work with System.Text.Json? |
@davidhenley which version of my library are you using? It used to work... |
@mattfrear we just tried @davidhenley solution, and sadly it didn't work as well. |
Latest version here, it doesn't work. Maybe I got this wrong, but isn't this forcing the use of the default
I think this implementation from |
@diegosps thanks for the investigation. Yes, this library has been around a long time, when the only Json serializer in town was the Newtonsoft one.
Yeah, it only works if you're using the Newtonsoft serializer. I'll see if diego's suggestion works. It might be a lot of work to untangle the Newtonsoft serializer from this project. |
I'm going to copy/paste this advisory to any open issues that are for ExamplesOperationFilter. Swashbuckle.AspNetCore supports adding examples from XML documentation, which might already cover your use case, in which case you can stop using this package and close your issue. If examples from XML documentation doesn't work for you, then feel free to leave your issue open. |
If you're using Newtonsoft's serializer, this works with the current version: .NET Core 3.1 services
.AddControllers()
.AddNewtonsoftJson(opt => opt.SerializerSettings.NullValueHandling = NullValueHandling.Ignore) .NET Core 2.1 (as described already) services
.AddMvc()
.AddJsonOptions(opt => opt.SerializerSettings.NullValueHandling = NullValueHandling.Ignore) As discussed above, this doesn't yet work if you're using System.Text.Json. Support for that might be added later. |
Hi,
When we create a example with only some properties, the SwaggerUI still generates all the fields as null.
Perhaps a new feature could be added for hiding those fields?
Thanks,
Effy
The text was updated successfully, but these errors were encountered: