RicoSuter / NSwag

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

[C# client generation] Improper handling of 204 status code #4853

Open mdzieg opened 5 months ago

mdzieg commented 5 months ago

I have a problem with C# generator. Version 13.6.2 is the latest one what works. Code generated with that version looks like this:

                         if (status_ == "200") 
                        {
                            return;
                        }
                        else
                        if (status_ != "200" && status_ != "204")
                        {

As of version 13.7.0, generated code thwors exception instead of default value in case 204 status code is returned:

            int status_ = (int) response_.StatusCode;
            if (status_ == 200)
            {
              ...
            }
            else
            { 

I added attributes on controller method to inform generator that it can result with 204, and the code generated is also not okay:

                        var status_ = (int)response_.StatusCode;
                        if (status_ == 200)
                        {
                            var objectResponse_ = await ReadObjectResponseAsync<GetRealizationRepartitionAppDto>(response_, headers_, cancellationToken).ConfigureAwait(false);
                            return objectResponse_.Object;
                        }
                        else
                        if (status_ == 204)
                        {
                            string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
                            throw new ApiException("No Content", status_, responseText_, headers_, null);
                        }

swagger.json looks more or less like this (after applying attributes):

"responses": {
                    "200": {
                        "description": "Success",
                        "content": {
                            "text/plain": {
                                "schema": {
                                    "$ref": "#/components/schemas/AppDto"
                                }
                            },
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/AppDto"
                                }
                            },
                            "text/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/AppDto"
                                }
                            },
                            "application/xml": {
                                "schema": {
                                    "$ref": "#/components/schemas/AppDto"
                                }
                            },
                            "text/xml": {
                                "schema": {
                                    "$ref": "#/components/schemas/AppDto"
                                }
                            }
                        }
                    },
                    "204": {
                        "description": "No Content"
                    }
                }

@RicoSuter What should I do to get correct 204 status code handling.

icnocop commented 2 months ago

Related issues: https://github.com/RicoSuter/NSwag/issues/1259 https://github.com/RicoSuter/NSwag/issues/1602 https://github.com/RicoSuter/NSwag/issues/2995 https://github.com/RicoSuter/NSwag/issues/4064

You can try one of the work-arounds indicated in one of these comments: https://github.com/RicoSuter/NSwag/issues/1259#issuecomment-1889574322 https://github.com/RicoSuter/NSwag/issues/2995#issuecomment-705532765 https://github.com/RicoSuter/NSwag/issues/2995#issuecomment-732359530 https://github.com/RicoSuter/NSwag/issues/2995#issuecomment-1262311104 https://github.com/RicoSuter/NSwag/issues/2995#issuecomment-1809992937