RicoSuter / NSwag

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

Incorrect c# syntax generated #4682

Open GStoynev opened 10 months ago

GStoynev commented 10 months ago

Hi, while I suspect this is potentially a bad (or one that can be worked around/improved) openapi json service definition, this segment:

  ...
  "responses": {  
    "201": {
        "description": "ID of the newly created container"
    },

generates the following "bad" c#:

//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v14.0.0.0 (NJsonSchema v11.0.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

...
public virtual async System.Threading.Tasks.Task MyMethodAsync(Sometype body, System.Threading.CancellationToken cancellationToken) 
{
  ...
  if (status_ == 201)
  {
    return default(void); // compile error here: Error  CS1547  Keyword 'void' cannot be used in this context
  }
  ...

I don't have control over the service json - it's delivered to us by an external entity over an http call. It might be automatically generated by Azure. Mentioning this to make the point that right or wrong, it's a real world scenario.

Note that we - the consumer - are on Windows, Visual Studio 2022 Pro (up-to-date), targeting latest stable nuget-s (see pic). Project settings:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <LangVersion>default</LangVersion>
    ...

image

And I want to use the opportunity to thank you for your service - your work has been outstanding! Thank you!

GStoynev commented 10 months ago

If someone points me to the project/class that's responsible, I'd be able to debug and potentially issue a PR

jeremiahmercier-3136 commented 3 months ago

This looks like the same issue as #3912.