RicoSuter / NSwag

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

NSwag generates invalid C# client code for twitter's OpenAPI spec (missing class definitions) #3738

Open Trolldemorted opened 2 years ago

Trolldemorted commented 2 years ago

This code generates invalid (not compiling) C# code:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="NSwag.CodeGeneration.CSharp" Version="13.14.4" />
  </ItemGroup>
</Project>
namespace NSwag
{
    using System;
    using System.Threading.Tasks;
    using NSwag.CodeGeneration.CSharp;

    internal class Program
    {
        internal static async Task Main(string[] args)
        {
            System.Net.WebClient wclient = new();

            var document = await OpenApiDocument.FromJsonAsync(wclient.DownloadString("https://api.twitter.com/2/openapi.json"));

            wclient.Dispose();

            var settings = new CSharpClientGeneratorSettings
            {
                ClassName = "TwitterAPIv2",
                CSharpGeneratorSettings =
                {
                    Namespace = "SpartaModels.Twitter",
                },
            };

            var generator = new CSharpClientGenerator(document, settings);
            var code = generator.GenerateFile();
            Console.WriteLine(code);
        }
    }
}

Trying to build the output yields 37 compiler errors which are being caused by 3 problems:

Switching to System.Text.Json output does not help, I guess the usage multiple fields with the same names confused NSwag. Are there any workarounds I can employ? Being able to generate a C# twitter API would be super awesome.

jjhayter commented 1 year ago

I am running into this exact same issue.

Ironically RulesRequestSummary exists, but the openapi name is just "summary" so you get the Summary? property when generated hence the error.

I am currently looking into generating a client operation as a perfile and namespace as an option unless there is an easier way (please advise!)