How to declare Nullable response types in SwashBuckle (generating clients in NSwagStudio)

1.9k Views Asked by At

We use SwashBuckle to configure our AspNet Core (Service fabric) project to generate Swagger json and UI. Then we use NSwagStudio to generate typescript and C# clients out of nswag.json template with swagger generated by Swashbuckle.

Recently there has been changes in latest version of NSwagStudio that generates clients with null checks added to response objects. We have existing Controllers returning null responses so the clients have stopped working.

Our sample controller endpoint declaration:

[HttpPost]
[ODataRoute]
[Produces("application/json")]
[ProducesResponseType(typeof(IActionResult), Status200OK)]
public Task PostXXX() => ...;

Our 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;
}

Our generated client (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;
} 

The question is how do we tell SwashBuckle to mark that this controller endpoint can return nullable response? In other words, am looking for something similar to what's available in NSwag here in Swashbuckle world: https://github.com/RicoSuter/NSwag/wiki/AspNetCoreOpenApiDocumentGenerator#response-nullability

More details in this github issue https://github.com/RicoSuter/NSwag/issues/3011

Please let me know if you need more information.

0

There are 0 best solutions below