Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
harshan007 opened this issue Aug 18, 2020 · 5 comments

Comments

@harshan007
Copy link

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() => ...;

@jeremyVignelles
Copy link
Collaborator

Duplicate of #2995

@jeremyVignelles
Copy link
Collaborator

It's in fact a duplicate of #3015 .

The explanation and workaround is given here : #2995 (comment)

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

@harshan007
Copy link
Author

harshan007 commented Sep 10, 2020

Thanks for reply.

@jeremyVignelles and @RicoSuter

I tried couple of workarounds mentioned in linked issue and also the XML document of setting nullable = true but couldn't get it to work as we are using SwakshBuckle.

We don't even have reference to this extension at the moment.
services.AddOpenApiDocument(doc =>{ doc.DefaultResponseReferenceTypeNullHandling = NJsonSchema.Generation.ReferenceTypeNullHandling.Null; });

Also

-		/// <response code="200">The successfully retrieved data</response>
+		/// <response code="200" nullable="true">The successfully retrieved data</response>

What are the options available to us and how do i do this:

but you need to add nullable: true in your OpenApi spec file where relevant

Any help to workaround this would be great. We only have to decorate few POST endpoints to make them return nullable.

Our workflow is: Opening nswag.json in NSwagStudio and then generate files (with service successfully launching swagger endpoint in background)

@jeremyVignelles
Copy link
Collaborator

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

@dzmitry-lahoda
Copy link

So I cannot override generator telling that all responses can be nullable during client generation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants