Skip to content

Commit 51480c1

Browse files
committed
Force-downgrade Microsoft.OpenApi at runtime to workaround microsoft/OpenAPI.NET#2967 and domaindrivendev/Swashbuckle.AspNetCore#4065
1 parent 59a9de5 commit 51480c1

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

Directory.Build.props

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,14 @@
7070
<ItemGroup>
7171
<PackageReference Include="JetBrains.Annotations" Version="2026.2.*" PrivateAssets="All" />
7272
</ItemGroup>
73+
74+
<ItemGroup>
75+
<!--
76+
Workaround: Swashbuckle 11.x (used on net10.0/net11.0) was compiled against Microsoft.OpenApi 3.9.0.
77+
CS1705 prevents compiling against a lower version, so we compile against 3.9.0 and overwrite
78+
the output DLL with 3.7.0 at runtime. PackageDownload ensures 3.7.0 is in the NuGet cache.
79+
See the UseOpenApi370AtRuntime target in Directory.Build.targets.
80+
-->
81+
<PackageDownload Include="Microsoft.OpenApi" Version="[3.7.0]" />
82+
</ItemGroup>
7383
</Project>

Directory.Build.targets

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
<Project>
2+
<!--
3+
Workaround: Swashbuckle 11.x (used on net10.0/net11.0) was compiled against Microsoft.OpenApi 3.9.0, so
4+
CS1705 prevents us from downgrading it at compile time. We compile against 3.9.0 and then overwrite
5+
the output DLL with 3.7.0 so the runtime uses the desired version. The Exists() condition limits this
6+
to executable/test projects (they copy transitive package DLLs to output; library projects don't).
7+
PackageDownload in Directory.Build.props ensures the 3.7.0 DLL is present in the NuGet cache.
8+
-->
9+
<Target Name="UseOpenApi370AtRuntime" AfterTargets="Build"
10+
Condition="('$(TargetFramework)' == 'net11.0' OR '$(TargetFramework)' == 'net10.0') AND Exists('$(OutputPath)Microsoft.OpenApi.dll')">
11+
<PropertyGroup>
12+
<_NuGetPackagesRoot Condition="'$(NUGET_PACKAGES)' != ''">$(NUGET_PACKAGES)</_NuGetPackagesRoot>
13+
<_NuGetPackagesRoot Condition="'$(_NuGetPackagesRoot)' == ''">$(UserProfile)\.nuget\packages</_NuGetPackagesRoot>
14+
<_OpenApi370Dll>$(_NuGetPackagesRoot)\microsoft.openapi\3.7.0\lib\net8.0\Microsoft.OpenApi.dll</_OpenApi370Dll>
15+
</PropertyGroup>
16+
<Copy SourceFiles="$(_OpenApi370Dll)" DestinationFolder="$(OutputPath)" OverwriteReadOnlyFiles="true"
17+
Condition="Exists('$(_OpenApi370Dll)')" />
18+
</Target>
19+
220
<Target Name="DisableCompileTimeOpenApiXmlGenerator" BeforeTargets="CoreCompile">
321
<ItemGroup>
422
<Analyzer Remove="@(Analyzer)" Condition="'%(Filename)' == 'Microsoft.AspNetCore.OpenApi.SourceGenerators'" />

0 commit comments

Comments
 (0)