Nswag: ASP.NET Core 3 support

Created on 15 Feb 2019  Â·  52Comments  Â·  Source: RicoSuter/NSwag

Where is the ASP.NET Core 3 support?

NSwag.AspNetCore enhancement

Most helpful comment

I hope to find time to look into this this week...

All 52 comments

Usually this is added when the new library is RTM.

As a ref, here are the PRs for adding 2.2 support:
https://github.com/RSuter/NSwag/pulls?utf8=âś“&q=is%3Apr+is%3Aclosed+2.2

You may want to start looking into this now using the previews, as changes to remove Newtonsoft.Json as a core dependency of MVC in ASP.NET Core 3.0 will likely break compatibility and cause a blocker for people from upgrading due to usage of MvcJsonOptions such as here:

https://github.com/RSuter/NSwag/blob/66095db418f5e6b50b670d3145feb63ada34550f/src/NSwag.AspNetCore/Middlewares/WebApiToSwaggerMiddleware.cs#L25

Related issues:

as changes to remove Newtonsoft.Json as a core dependency

Yep, this will be a major change, mainly in the NJsonSchema repo. Until now, noone could tell me what the replacement of newtonsoft’s contract resolver is...

So if you want to keep a dependency on Newtonsoft.Json, you'd use AddNewtonsoftJson() for configuring MVC and then the MvcNewtonsoftJsonOptions class. I'm not entirely sure what the new types are for the native solution.

Ultimately, the problematic reference for 3.0 is Microsoft.AspNetCore.Mvc.Formatters.Json. If you removed that and then built up from there for netcoreapp3.0 you should get towards something compatible.

When using the newtonsoft.json serializer it should be no big issue - my problem is more with the new serializer and how to get the serialization contract there...

Having had a quick look at the code, a "fast" way to do this (though still a breaking change) would be to remove all usage of IOptions<MvcJsonOptions> and replace it with IOptions<JsonSerializerSettings>, given that its sole purpose is to act as a holder for the serializer settings, and then register that into the services if it isn't already there, plus maybe an explicit dependency on Newtonsoft.Json for 3.0 apps as it won't necessarily be in the application's default dependencies anymore.

Otherwise you're looking at having to come up with some sort of abstraction on to get the serializer settings, abstract away JSON serialization completely like MVC is doing, or start forking the code with things like hash defines to handle different code and serialization paths for different TFMs.

Have also opened https://github.com/aspnet/AspNetCore/issues/8254 suggesting MvcJsonOptions is restored via a type-forward in ASP.NET Core 3.0 preview 4 or later to reduce the impact of the changes.

I think we could just add the new target framework .net core 3 to nswag.aspnetcore and use the new MvcNewtonsoftJsonOptions with conditional compile... the downside is that you cannot use asp.net core 2.2 on .net core 3.0 - to allow that we’d need a new package - e.g. nswag.aspnetcore3 - i.e. decouple the aspnetcore version from the target framework version. But this means potentially a new package for every “major” aspnetcore version - a maintainability nightmare

That approach also has the downside of forcing the Newtonsoft.Json version of MVC JSON formatting to be active even if the user isn't using it, rather than the new native implementation if someone wants to go with that instead.

Hopefully the ASP.NET Core team can come up with something to make this less painful to manage.

Yeah, but we would only use these options if they are registered, ie the newtonsoft json serializer is active -otherwise we would use the new implementation. But i dont know at all how the contract reflection works there...

And of course the new serializer requires a lot of work because ATM everything is newtonsoft.json based - ie at the beginning nswag will only work with aspnetcore 3 WITH the newtonsoft serializer (or some [automatic] newtonsoft settings which more or less resemble the new serializer behavior)

Does anyone know a workaround/hack that i can use today while we wait for a proper fix?

Using preview 3.0, there is currently no workaround.

So the problem is only that MvcJsonOptions has been renamed to MvcNewtonsoftJsonOptions?

It’s not just been renamed, but also moved to a different assembly, so it’s a complete breaking change as the types this library are compiled against aren’t there at runtime, effectively making everything unusable.

The only _current_ solution is to fork this repo, swap to the new type from 3.0, compile it yourself and use that.

Yes, but assuming they to this type forward thing, will it work even if NSwag targets the "old" ASP.NET Core 2.2 version?

If they come up with a solution then this issue can just be closed as it’ll no longer be an issue and it’ll “just work”, but looking at their reply it doesn’t look like a simple fix, but they seem to be treating it as high priority to resolve.

PR for supporting ASP.NET Core 3: https://github.com/RSuter/NSwag/pull/2041

Ok, the NSwag.AspNetCore package should now (v12.0.20) work with the ASP.NET Core 3 preview...

Keeping the PR open as this (conditionals & Core 3 TFM) is probably the way to go - also we need many more changes to support CLI etc...

For now probably only the ASP.NET Core middleware works. Please report any problems.

@pranavkm Does this dynamic fallback look good (for now)?
https://github.com/RicoSuter/NSwag/commit/19ecd169938a78fe33afabf43cb1fa2e7c959be1#diff-2698934139b3148923e9785185649f08R118
(I need this "func hack" in order to be able to catch the type load exception)

Yeah, that seems reasonable. If you have builds available with the change, I'd be happy to check the swagger gen works with the most recent preview4 bits of AspNetCore.

@pranavkm this is available in v12.0.20 - I tested it with preview3

NSwag.MSBuild needs to be updated to support .NET Core 3.0. Generic Host support, and and adding NSwagExe_Core30.

Yes, but i wanted to wait until core 3 is released :-) not an option?

Aah. I understand.
Not for the progress of my project 🙂

I don’t know how hard it is to add, but a preview would be nice.

I agree that it would be nice...

I saw the following problems:

  • As soon as i install core 3 preview i’m no longer able to build the solution even if there are no changes for core 3 (solvable)
  • On the ci server i need to build with vs 2019 msbuild but there is no chocolatey wix extension package for it yet (solvable)
  • My biggest concern: As soon as we add core 3 preview target framework to the projects, all people contributing to nswag need to also install it which will make it even harder for them to compile everything (it’s already hard with all the required sdks etc)

But ill see again if its worth the effort now...

I thought I had it working for my netcore3 client generation CLI project (by moving the controllers and models to a separate project to avoid type loading exceptions) but the response models are all missing in the generated clients.

It's possible that CLI does not work yet as the CLI is not yet distrubuted with .NET Core 3 binaries... they are completely missing, i.e. generated as any?

When I said CLI, I meant a custom console app with a SwaggerToTypeScriptClientGenerator in it.

The clients are generated like this:

get(id: string): Observable<SwaggerResponse<void>> { ... }

instead of:

get(id: string): Observable<SwaggerResponse<ProjectModel | null>> { ... }

UPDATE

It turns out that removing the [ProducesResponseType] attributes from my actions brings back the models.

But, this is not a perfect solution obviously, since some of my actions return 202 Accepted for example, Without a [ProducesResponseType] attribute, the generated client doesn't anticipate for that status code.

@sandort You have to use [ProducesResponseType(typeof(ProjectModel))] or [ProducesDefaultResponseType]

@sandort You have to use [ProducesResponseType(typeof(ProjectModel))] or [ProducesDefaultResponseType]

Thanks, that works!

When will the MSBuild package be available?

Any news on the MSBuild package for .NetCore 3.0?

What is the progress?

NSwag.AspNetCore (the middlewares) should work with ASP.NET Core 3.0, the CLI binaries (e.g. NSwag.MSbuild, NSwag.ConsoleCore) are currently not released with .NET Core 3.0 target framework and ASP.NET Core 3.0 dependencies because it would force all maintainers to install .NET Core 3.0 previews, etc. - waiting for a RC for now.

Hi @RicoSuter,

You have written, that the middlewares should work with ASP.NET Core 3.0.
I've just installed NSwag.AspNetCore and tried to use middlewares same way, as I have used in ASP.NET Core 2.2.

Unfortunatelly, I can't download the swagger document. I got error 404 (Not Found). I tried on preview7 and preview5.
I've generated project from the VS Web Api template and added swagger middleware.

Here is my Startup.cs

`public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
        services.AddOpenApiDocument();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseOpenApi();
        app.UseSwaggerUi3();


        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

`

Is there any chance to use NSwag in ASP.NET Core 3.0, or currently I have to move back to 2.2?
I'm starting new project and wanted to write it from the begining in ASP.NET Core 3 to avoid migrating it in near future. :-)

Have you tried to add it before UseRouting()?

@Oposow You probably need to add Newtonsoft Json support.
Include the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package in preview version and add .AddNewtonsoftJson() after services.AddControllers()

It is how I solved it on my project and I get it working with preview 7.

Source: https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-2.2&tabs=visual-studio#jsonnet-support

Hi, thank you very much for answers. I will test it at work on monday :-)

sob., 27 lip 2019, 01:53 uĹĽytkownik Valentin Fritz notifications@github.com
napisał:

@Oposow https://github.com/Oposow You probably need to add Newtonsoft
Json support.
Include the Microsoft.AspNetCore.Mvc.NewtonsoftJson nuget package in
preview version and add .AddNewtonsoftJson() after
services.AddControllers()

It is how I solved it and I get it working with preview 7.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/RicoSuter/NSwag/issues/1961?email_source=notifications&email_token=AFSNH3YNZ4JL575IWLKS6U3QBOE7ZA5CNFSM4GXYVUK2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD256SMY#issuecomment-515631411,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFSNH3ZJWZITMBXAMKSPWG3QBOE7ZANCNFSM4GXYVUKQ
.

@RicoSuter unfortunatelly changing the methods calling order didn't help.

The solution provided by @vfrz worked perfectly, thank you very much!

Word on the street is there it may go straight from preview 8 to release, no RC's version. September 23rd at .Net Conf.

As an aside for those switching their implementation to the new native JSON support, and not planning to use the old Newtonsoft JSON libraries, how will this work with Nswag?

Unless things have changed very recently, last I was aware, there was going to be a preview 9 before the final version ships during .NET Conf: https://github.com/dotnet/corefx/issues/39769#issuecomment-518766445

As an aside for those switching their implementation to the new native JSON support, and not planning to use the old Newtonsoft JSON libraries, how will this work with Nswag?

I think it will be possible to only use newtonsoft contract resolver internally and use the new json stuff for serialization... then obly the internal resolver needs to be aligned with the actual serializer

Added fallback to JsonSerializerSettings from OpenAPI document settings if settings are not provided by calling AddNewtonsoftJson().

v13.0.6

So we wont get any MSBuild task for 3.0 before the actual release?

Maybe you can already use this instead of CLI?
https://github.com/RicoSuter/NSwag/wiki/ServiceProjectReference

@RicoSuter

Severity | Code | Description | Project | File | Line | Suppression State
-- | -- | -- | -- | -- | -- | --
Error |   | ServiceProjectReference items are no longer supported. | BlazorApp2.ApiClient | C:\Users\rober\.nuget\packages\microsoft.extensions.apidescription.client\0.3.0-preview7.19365.7\build\Microsoft.Extensions.ApiDescription.Client.targets | 139 |  

Hi @RicoSuter
When will it be available Nswag.MsBuild for .net core 3.0? How can we test preview versions?

I hope to find time to look into this this week...

Release v13.1.0: https://github.com/RicoSuter/NSwag/pull/2427

Also supports NRT (provided by Namotion.Reflection/):

image

.NET Core 3.0 support docs: https://github.com/RicoSuter/NSwag/wiki/AspNetCoreOpenApiDocumentGenerator#net-core-3-and-c-8

Please report if something is not working as expected.

image

Works great, thank you. Implemented here;
https://github.com/JasonGT/NorthwindTraders/tree/master/Src/WebUI

Does not work for me

Was this page helpful?
0 / 5 - 0 ratings