0
votes

I can provide more detail, but not sure what is useful, so I will respond with more specific information if requested.

I have a .NET Core 3.1 project which was using Swashbuckle to provide Swagger UI. It started using the Swashbuckle.AspNetCore NuGet package version 5.0.0-rc4, and everything worked.

I have a controller method that takes a Contract object:

public async Task<IActionResult> Import([FromBody] Contract contract) {

This is just an extremely basic class (I stripped everything down while troubleshooting), basically just:

public class Contract {
    /// <summary>
    /// Optional, summary
    /// </summary>
    public DateTime contractExpirationDate;
    public DateTime date;
}

It all works great, the Swagger UI shows Contract in the Schemas section with all it's details, and it shows up in the controllers section and tryout properly starts out with the example structure filled out, etc.

Everything is great... until I try to update Swashbuckle.AspNetCore any further... as soon as I hit 5.0.0-rc5 , or go to 5.0.0, or go anywhere all the way up to the latest (currently 5.4.1), I immediately run into the following problem.

Basically, what happens is, Swagger still runs and everything seems fine, but the Contract schema now shows as simply {}. It SEES Contract as a schema, but it's empty, and the controller Try it example doesn't have the sample (it just has {} as the request body), etc. If I just go and update Swashbuckle.AspNetCore back down to 5.0.0-rc4 and immediately re-run it, it works again.

I'm sure I'm missing some obvious change/update I need to make to advance beyond 5.0.0-rc4 , but I have spent quite a while trying to figure out what it is, and I'm coming up blank. My code seems to match all the examples on Microsoft's site, so I am not sure what I'm missing.

1
Low hanging fruit to rule out, but have you attempted to clear your browser cache after the upgrade? Lot of transitional code in the 5.x RC on the way to OAS 3.0Adam G
So a few things point to this not being a caching issue. First, I can update to and from 5.0.0-rc4 and above at will, and it will immediately work perfectly with 5.0.0-rc4 in place and will be blank at any of the higher versions, completely reproducibly. The swagger.json file matches up with this, showing the contract alternately with all the items, and as a blank contract (it has the 2 sections in it that essentially say it's blank): ` "components": { "schemas": { "Contract": { "type": "object", "additionalProperties": false },`JMnITup
Also, yes, of course I cleared the browser cache. :) I also rebooted. I did not reinstall Windows yet, but I AM quite sure my monitor is on and the power cable is properly plugged in. None of these helped.JMnITup
Had to ask! It's bitten me before with a Swagger issue (ask me how I know...) ;)Adam G

1 Answers

3
votes

The issue biting you after 5.0.0-rc4 is the switch from Newtonsoft as the JSON library over to System.Text.Json, which is behaving differently on your public fields.

Two ways to fix:

1) Add a getter and a setter on your class's fields and System.Text.Json will pick it up:

public class Contract {
    public DateTime contractExpirationDate { get; set; }
    public DateTime date { get; set; }
}

2) Install Swashbuckle.AspNetCore.Newtonsoft and revert the JSON library back to Newtonsoft by adding the following to your services registration:

services.AddSwaggerGenNewtonsoftSupport();