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

Bug: Generated c# client methods return voids #3996

Open
fairking opened this issue May 5, 2022 · 6 comments
Open

Bug: Generated c# client methods return voids #3996

fairking opened this issue May 5, 2022 · 6 comments

Comments

@fairking
Copy link

fairking commented May 5, 2022

NSwag v13.15.10.0 (NJsonSchema v10.6.10.0 (Newtonsoft.Json v11.0.0.0))
Windows 10
.Net6
Visual Studio 2022 Preview 6.0

I was trying to do that via VS (Add Service Reference -> OpenAPI) but it wasn't working. So I simply run the command bellow in powershell (C:\Users\Admin\.nuget\packages\nswag.msbuild\13.15.10\tools\Win\NSwag.exe).

The command:

NSwag.exe openapi2csclient /className:API /namespace:AccessPay /input:{path}swagger.json /output:{path}swaggerClient.cs

The swagger scheme: https://eazycollectservices.github.io/EazyCollectAPIv3/swagger.json

The above example generates not compilable client code:

if (status_ == 200)
{
    return default(void); // Compile error
}

image

@fairking fairking changed the title Generated client methods return voids Bug: Generated c# client methods return voids May 5, 2022
@fairking
Copy link
Author

fairking commented May 5, 2022

Could be related to #3912

@crackalak
Copy link
Contributor

@fairking yes I think this is related. This happens because your swagger json doesn't define the content for the response types, it only seems to have examples (there should be a content property under the 200) e.g:

"responses": {
          "200": {
            "description": "OK",
            "examples": {
              "application/json": {
                "schema": {
                  "example": {
                    "Message": "Callback URL cleared."
                  },
                  "type": "string"
                }
              },
              "application/xml": "<GeneralAcknowledgement\n  xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n    <Message>Callback URL cleared.</Message>\n</GeneralAcknowledgement>"
            }
          }
        },

See spec for more info https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#responses-object-example

@fairking
Copy link
Author

@crackalak Thank you for the response. So the issue is related to the missing api docs. Closing the issue.

@tmatous
Copy link

tmatous commented Oct 14, 2023

The OpenAPI specs do not require a content definition. And even if it did, NSwag should not generate invalid C# code. This issue should be reopened.

@pavlexander
Copy link

agreed. There is no excuse for returning the broken code. If it comes down to errors in the specification (which is not the case according to @tmatous in this particular scenario), then the nswag should at least show some warnings or even an error during the code generation process..

Just stumbled upon the same issue..

@fairking fairking reopened this Jan 20, 2024
@fairking
Copy link
Author

fairking commented Jan 20, 2024

I understand there could be an issue with api docs, or with nswag, but can we at least explain users what is wrong? Currently people spend hours to understand what is going on. Or show some warning or error, or fix the way how the code is being generated.

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

No branches or pull requests

4 participants