Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle.AspNetCore (5.0.0-rc5) ignoring public fields

First off, this worked fine in Swashbuckle.AspNetCore v4. So given this model & endpoint;

public class TestRequest
{
    public string test1 { get; set; }
    public string test2;
}

[HttpPost]
public void Post([FromBody] TestRequest value)
{
}

Swagger only shows;

{
  "test1": "string"
}

as the example TestRequest value.

Is this behaviour intended now? Is there anyway to change this via setup or filters? I have a ton of imported services that only use fields. (generated by dotnet-svcutil, which is pretty common)

like image 819
bmiller Avatar asked Oct 23 '25 01:10

bmiller


1 Answers

ok, taking @Flydog57 suggestion to figure it out myself, I found that the new Swashbuckle.AspNetCore is using the MS json.net over Newtonsoft.

So reading over the git readme a tenth time at https://github.com/domaindrivendev/Swashbuckle.AspNetCore

I found that there is a way to revert the json serializer back to Newtonsoft. First (as of the 5.0.0-rc5 release) install this package;

Install-Package Swashbuckle.AspNetCore.Newtonsoft -Version 5.0.0-rc5

Then in Start.ConfigureServices tell Swashbuckle to use by;

services.AddSwaggerGenNewtonsoftSupport();

Now you'll find all those public "Fields" have reappeared in your swagger page.

Update: .Net 5+ has an "IncludeFields" option in JsonSerializerOptions class.

https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.includefields?view=net-5.0

So easy peasy now.

like image 56
bmiller Avatar answered Oct 24 '25 16:10

bmiller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!