Swashbuckle.aspnetcore: Incompatibility with ASP.NET Core 3.0

Created on 3 Feb 2019  路  9Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

I've been testing an API that targets ASP.NET Core 2.2 in production with ASP.NET Core 3.0 using preview 2, and I've found an incompatibility between Swashbuckle.AspNetCore and ASP.NET Core MVC 3.0 due to the changes to move Newtonsoft.Json out of MVC itself as a core dependency.

If an application uses the Microsoft.AspNetCore.Mvc.JsonResult class anywhere in its code when both ASP.NET Core 3.0.0-preview-19075-0444 and Swashbuckle.AspNetCore 4.0.1 are referenced, a CS0433 compiler error is generated:

The type 'JsonResult' exists in both 'Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' and 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=2.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'

This incompatibility arises because Swashbuckle.AspNetCore.Swagger references Microsoft.AspNetCore.Mvc.Formatters.Json >= 2.0.0 which does not exist in ASP.NET Core 3.0 and ASP.NET Core 3.0 has moved the type in that assembly to Microsoft.AspNetCore.Mvc.Core.

Because there's no type forward, both types exist in scope creating an unresolvable conflict meaning the application cannot be compiled.

The issue can be worked around by adding the following to the .csproj file of the project as referenced by https://github.com/NuGet/Home/issues/4989#issuecomment-426666530:

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.0.0" Alias="LegacyAspNetCoreJsonFormatter" />
</ItemGroup>
<Target Name="AddPackageAliases" BeforeTargets="ResolveReferences" Outputs="%(PackageReference.Identity)">
  <PropertyGroup>
    <AliasPackageReference>@(PackageReference->'%(Identity)')</AliasPackageReference>
    <AliasName>@(PackageReference->'%(Alias)')</AliasName>
  </PropertyGroup>
  <ItemGroup>
    <ReferencePath Condition="'%(FileName)'=='$(AliasPackageReference)' and '$(AliasName)' != ''">
      <Aliases>$(AliasName)</Aliases>
    </ReferencePath>
  </ItemGroup>
</Target>

The possible options to fix this seem to be either:

  1. Microsoft creates a type forward for a later version of ASP.NET Core 3.0 that resolves the conflict.
  2. Swashbuckle.AspNetCore adds a target framework for netcoreapp3.0 and adjusts its dependencies accordingly.

Most helpful comment

Thanks Martin!

All 9 comments

Have also logged an issue in the ASP.NET Core repo: aspnet/AspNetCore#7220

Looks like this will be resolved by https://github.com/aspnet/AspNetCore/pull/7278 for a future preview of ASP.NET Core 3.0.

While updated builds of ASP.NET Core 3.0 with the fix mentioned above fix the compilation error, there's now a runtime error caused by this line of code because this type no longer exists in 3.0:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/c8d8edbf1a04ccf5662ad961fd373adaf0d12e32/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SchemaRegistryFactory.cs#L13

cc @pranavkm @rynowak @glennc For awareness

Have opened #1061 with a _possible_ suggested fix for 5.0.

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.

Fixed by #1061

Thanks Martin!

+1

Was this page helpful?
0 / 5 - 0 ratings