RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.76k stars 1.29k forks source link

A SwaggerException is generated in the C# client when upgrading from 13.6.2 to 13.7.0 #3005

Open bgraham1962 opened 4 years ago

bgraham1962 commented 4 years ago

For a number of our API endpoints we return 200 null if that is the most sensible result. For example:

        [Route("guest/{userId}/image")]
        [HttpGet]
        [SwaggerResponse(HttpStatusCode.OK, "Guest Image", typeof(File))]
        public File GetPhoto(string userId)
        {
            if (GuestService.GetValidGuest(userId).Select(g => g.Image).FirstOrDefault() is GuestImage image)
            {
                switch (image.Type)
                {
                    case TypeBmp:
                        return new File { Data = image.Image, MediaType = MimeBmp, FileName = "photo.bmp" };
                    case TypeJpeg:
                        return new File { Data = image.Image, MediaType = MimeJpeg, FileName = "photo.jpg" };
                }                
            }
            return null;
        }

Since the upgrade to 13.7 the generated C# client now throws an error in this situation.

if (objectResponse_.Object == null)
  {
     throw new SwaggerException("Response was null which was not expected.", status_, objectResponse_.Text,                            headers_, null);
  }

How do we annotate the method to tell it that File (in this case) is a nullable return type?

Regards Brian

jeremyVignelles commented 4 years ago

I think that you can decorate your method with [return: CanBeNull] and that it would generate the nullable:true in the OpenApi definition that would then be understood by the client generator.

If you can use C#9, it would be easier to annotate your controller with nullable reference types

bgraham1962 commented 4 years ago

Thanks for the reply Jérémy,

I cannot find that attribute within the normal C# attributes. Is it a JetBrains one?

Cheers

Brian

From: Jérémy VIGNELLES notifications@github.com Sent: Friday, 14 August 2020 5:17 pm To: RicoSuter/NSwag NSwag@noreply.github.com Cc: Brian Graham brian.graham@slingshot.co.nz; Author author@noreply.github.com Subject: Re: [RicoSuter/NSwag] A SwaggerException is generated in the C# client when upgrading from 13.6.2 to 13.7.0 (#3005)

I think that you can decorate your method with [return: CanBeNull] and that it would generate the nullable:true in the OpenApi definition that would then be understood by the client generator.

If you can use C#9, it would be easier to annotate your controller with nullable reference types

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/RicoSuter/NSwag/issues/3005#issuecomment-673888760 , or unsubscribe https://github.com/notifications/unsubscribe-auth/ABMOMP2XDVLPX3RIPE52553SATCELANCNFSM4P63FZPA . https://github.com/notifications/beacon/ABMOMPYPKTQLXE2GLLVWFK3SATCELA5CNFSM4P63FZPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOFAVLT6A.gif

jeremyVignelles commented 4 years ago

It could be the one from jetbrains, but IIRC, any attribute with that name (including attibute you declare in your project) would work by convention.

jeremyVignelles commented 4 years ago

Could also be the one from the NJsonSchema NuGet package : https://github.com/RicoSuter/NJsonSchema/blob/958afa39a8386a6f02bf6604aca742e4d18acb61/src/NJsonSchema/Annotations/CanBeNullAttribute.cs

bgraham1962 commented 4 years ago

Thanks Jérémy

I will check them out.

Regards Brian

From: Jérémy VIGNELLES notifications@github.com Sent: Monday, 17 August 2020 8:15 am To: RicoSuter/NSwag NSwag@noreply.github.com Cc: Brian Graham brian.graham@slingshot.co.nz; Author author@noreply.github.com Subject: Re: [RicoSuter/NSwag] A SwaggerException is generated in the C# client when upgrading from 13.6.2 to 13.7.0 (#3005)

Could also be the one from the NJsonSchema NuGet package : https://github.com/RicoSuter/NJsonSchema/blob/958afa39a8386a6f02bf6604aca742e4d18acb61/src/NJsonSchema/Annotations/CanBeNullAttribute.cs

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/RicoSuter/NSwag/issues/3005#issuecomment-674572424, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABMOMP3AARO4PEXEC6HJX7TSBA43VANCNFSM4P63FZPA.