question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suppress null check in response object in latest version of NSwagStudio 13.7.0.0

See original GitHub issue

We 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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
jeremyVignellescommented, Sep 11, 2020

You need to tell swashbuckle that the result can be null. The first thing you tried is NSwag-specific.

1reaction
jeremyVignellescommented, Aug 18, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to declare Nullable response types in SwashBuckle ...
Recently there has been changes in latest version of NSwagStudio that generates clients with null checks added to response objects.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found