Suppress null check in response object in latest version of NSwagStudio 13.7.0.0
See original GitHub issueWe have POST endpoints in ASP.NetCore project that return no response (null) when 200 status code. Recently when we regenerated clients using latest version of NSwagStudio, we see that it has added additional null checks on response object because of which our tests now fail.
Question is: Is there a way to configure to not generate this new null checks for specific (or all) endpoints in latest version of NSwagStudio?
Dependency versions: <Swashbuckle_AspNetCore_Version>5.5.1</Swashbuckle_AspNetCore_Version> Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0
Generated client
(before)
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.6.2.0 (NJsonSchema v10.1.23.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
var status_ = ((int)response_.StatusCode).ToString();
if (status_ == "200")
{
var objectResponse_ = await ReadObjectResponseAsync<IActionResult>(response_, headers_).ConfigureAwait(false);
return objectResponse_.Object;
}
(after)
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.7.0.0 (NJsonSchema v10.1.24.0 (Newtonsoft.Json v12.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<Void>(response_, headers_).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new Exception("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
}
return objectResponse_.Object;
}
We also tried to change our POST endpoint to return void instead of IActionResult but both ways seem to be producing above null check in clients in latest version of NSwagStudio.
Controller
(before) [HttpPost] [ODataRoute] [Produces(“application/json”)] [ProducesResponseType(typeof(IActionResult), Status200OK)] public Task PostXXX() => …;
(after)
[HttpPost] [ODataRoute] [Produces(“application/json”)] [ProducesResponseType(Status200OK)] public Task PostXXX() => …;
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5
You need to tell swashbuckle that the result can be null. The first thing you tried is NSwag-specific.
It’s in fact a duplicate of #3015 .
The explanation and workaround is given here : https://github.com/RicoSuter/NSwag/issues/2995#issuecomment-675746287
We did that to allow the implementation of proper NullableReferenceTypes and for that, we need to be able to trust the spec.
If the spec doesn’t declare the result as nullable, then we consider it to be not null. I don’t know how you could do that with Swashbuckle, but you need to add
nullable: true
in your OpenApi spec file where relevant