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

Asp.net 6.0 nswag 13.15.5 generates empty cs clients. #3834

Open
svenvanoverbeke opened this issue Jan 17, 2022 · 14 comments
Open

Asp.net 6.0 nswag 13.15.5 generates empty cs clients. #3834

svenvanoverbeke opened this issue Jan 17, 2022 · 14 comments

Comments

@svenvanoverbeke
Copy link

Currently having the issue where the generated client.cs and contract.cs files are generated empty.

Underneath my setup:

  [ApiController]
   [Route("[controller]")]
   public class JobController : ControllerBase
   {
       private static readonly string[] Summaries = new[]
       {
       "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
       };

       private readonly ILogger<JobController> _logger;

       public JobController(ILogger<JobController> logger)
       {
           _logger = logger;
       }

       [HttpGet(Name = "GetForApplication")]
       public IEnumerable<WeatherForecast> Get(string application)
       {
           return Enumerable.Range(1, 5).Select(index => new WeatherForecast
           {
               Date = DateTime.Now.AddDays(index),
               TemperatureC = Random.Shared.Next(-20, 55),
               Summary = Summaries[Random.Shared.Next(Summaries.Length)]
           })
           .ToArray();
       }
   }
<PackageReference Include="NSwag.AspNetCore" Version="13.15.5" />
<PackageReference Include="NSwag.MSBuild" Version="13.15.5">

using the same nswag.json used in a .net 5.0 version, working correctly.
I only changed runtime

"runtime": "Net60",

Build target:


<Target Name="GenerateApiClientSourceCode" AfterTargets="Build">
		<Exec Command="$(NSwagExe_Net60) run Swagger\nswag.json /runtime:Net60 /variables:Configuration=$(Configuration) " />
	</Target>

Content of generated client.cs:

`//----------------------
//
// Generated using the NSwag toolchain v13.15.5.0 (NJsonSchema v10.6.6.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
//
//----------------------

#pragma warning disable 108 // Disable "CS0108 '{derivedDto}.ToJson()' hides inherited member '{dtoBase}.ToJson()'. Use the new keyword if hiding was intended."
#pragma warning disable 114 // Disable "CS0114 '{derivedDto}.RaisePropertyChanged(String)' hides inherited member 'dtoBase.RaisePropertyChanged(String)'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."
#pragma warning disable 472 // Disable "CS0472 The result of the expression is always 'false' since a value of type 'Int32' is never equal to 'null' of type 'Int32?'
#pragma warning disable 1573 // Disable "CS1573 Parameter '...' has no matching param tag in the XML comment for ...
#pragma warning disable 1591 // Disable "CS1591 Missing XML comment for publicly visible type or member ..."
#pragma warning disable 8073 // Disable "CS8073 The result of the expression is always 'false' since a value of type 'T' is never equal to 'null' of type 'T?'"
#pragma warning disable 3016 // Disable "CS3016 Arrays as attribute arguments is not CLS-compliant"

namespace MyNamespace
{
using System = global::System;

}

#pragma warning restore 1591
#pragma warning restore 1573
#pragma warning restore 472
#pragma warning restore 114
#pragma warning restore 108
#pragma warning restore 3016`

Any idea?

@bsurprised
Copy link
Contributor

Duplicate of #3794 issue.
Pull request #3814 will solve this.

@Apollo3zehn
Copy link

Apollo3zehn commented Jan 19, 2022

I have forked the project and applied the patch. The build output can be found here (https://github.com/Apollo3zehn/NSwag/actions/runs/1717561743, https://github.com/Apollo3zehn/NSwag/suites/4986122681/artifacts/145780363). The .zip file contains the updated NSwagStudio which now generates the correct output.

@bsurprised
Copy link
Contributor

Thank you. I don't think it would be so urgent a matter. @RicoSuter would hopefully merge the pull request soon in the next revision.

@Apollo3zehn
Copy link

For me it was urgent :-) just wanted to share my workaround.

@bsurprised
Copy link
Contributor

Glad to know it helped.

@ranouf
Copy link

ranouf commented Jan 28, 2022

@Apollo3zehn > I tried your version, I have an infinite loading when I try to generate the typescript file.

I have the last version available for Nswag Studio (13.15.5.0), and the generated typescript file is still empty, I thought the fix has been already merged, but I don't why, it seems not working.
PR: #3814

@ranouf
Copy link

ranouf commented Jan 28, 2022

I created a git so you can test the infinite loading:
https://github.com/ranouf/net60withnswag/tree/main

open sln file
set servers/ApiWithAuthentication.Servers.API as Startup project.

note: in ApiWithAuthentication.Servers.API.csproj, you can uncomment the last lines to directly execute nswag to generate the typescript file

@Apollo3zehn
Copy link

@ranouf thanks for your feedback. I have had merged #3814 and the .cs client generation works for me fine now. Only problem left is that app.Run(baseUrl) seems to cause another issue which I need to investigate, https://github.com/Nexusforge/nexus/blob/ea7106d29cdda21e752064c3528a99111abd2f38/src/Nexus/Program.cs#L65). I did not test the typescript client generation.

@ranouf
Copy link

ranouf commented Feb 1, 2022

@Apollo3zehn let me know when you ll have a new artifact to download.

If you want to try typescript generation, you can use my git repo. download the code and open the nswag file at the root, every thing is already configured, just need to push "Generate Files" in nswag studio

@Apollo3zehn
Copy link

@ranouf Sorry, I have unfortunately no time available to test the typescript client generation.

I just wanted to say that with the new version (13.15.7 and I guess 13.15.6 also), the C# client generation is working fine for me (except of the above mentioned app.Run(baseUrl) issue). I will remove my fork now.

@ranouf
Copy link

ranouf commented Feb 3, 2022

No pb, I succeeded to find the solution, I needed to add Development as environment on nswag Studio to make it work correctly.

@Entroper
Copy link

Entroper commented Mar 23, 2022

Hi, I'm using NSwag 13.15.10 and I'm still getting empty .cs and .ts clients if I use the new WebApplicationBuilder (and an empty swagger.json as well). Reverting to the old HostBuilder (with Startup.cs) fixed it for now.

For info, we're using the same controllers that we were using on .NET 5, I just switched from

Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(builder => builder.UseStartup<Startup>());
// etc.

to the new

var builder = WebApplication.CreateBuilder(args);
// etc.

I'm not passing a baseUrl to app.Run(), or using API versioning or VersionedApiExplorer. I set the environment to Local to make NLog happy.

I'm running the nswag CLI from a Docker container with the following Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata
RUN ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime
RUN dpkg-reconfigure --frontend noninteractive tzdata
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - && apt-get install -y nodejs
RUN npm install -g nswag@13.15.0

@MathiasZander
Copy link

I got my Problem with aspnetcore2openapi generating empty specifications fixed, by passing in the Program args.
WebApplication.CreateBuilder(args)
Version="13.16.1"

@baxter-ben
Copy link

I got my Problem with aspnetcore2openapi generating empty specifications fixed, by passing in the Program args. WebApplication.CreateBuilder(args) Version="13.16.1"

This worked for me, but only after removing the bin and obj folders and rebuilding the project. I am using the CLI (dotnet nswag run) rather than the build target, in case that makes a difference.

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

7 participants