dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

VS2019-integrated OpenAPI client support fails for multitargeted assemblies #31996

Open lscorcia opened 3 years ago

lscorcia commented 3 years ago

Minimal repro project here: https://github.com/lscorcia/aspnetcore-issue31996-repro

I have a library which is using multitargeting - in the .csproj it has:

    <TargetFrameworks>netstandard2.1;net472</TargetFrameworks>

I added to it an OpenAPI connected service via VS2019 GUI (16.9.4):

immagine

It added some nuget packages to the project, and the OpenApiReference element:

  <ItemGroup>
    <OpenApiReference Include="OpenAPIs\dss.json">
      <CodeGenerator>NSwagCSharp</CodeGenerator>
    </OpenApiReference>
  </ItemGroup>
...
 <PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="5.0.5">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="NSwag.ApiDescription.Client" Version="13.10.9">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

The build completes fine, no error or warnings, however it does not generate the corresponding source code in the obj folder. If I replace the multitargeting with a single target however it works correctly:

    <TargetFramework>netstandard2.1</TargetFramework>

Looking at the build logs, it seems like the following condition fails and does not run the OpenAPI code generation:

https://github.com/dotnet/aspnetcore/blob/ec69111cd568dbc7b06f62851bea3b452e404972/src/Tools/Extensions.ApiDescription.Client/src/build/Microsoft.Extensions.ApiDescription.Client.targets#L161-L164

Relevant msbuild.log entry (in Italian, but should be easily readable):

Destinazione"_TieInGenerateOpenApiCode" ignorata. Condizione false. ( '$(OpenApiGenerateCodeOnBuild)' == 'true' AND ('$(TargetFramework)' == '' OR '$(TargetFrameworks)' == '') ) ha restituito ( 'true' == 'true' AND ('netstandard2.1' == '' OR 'netstandard2.1;net472' == '') ).
...
Destinazione"_TieInGenerateOpenApiCode" ignorata. Condizione false. ( '$(OpenApiGenerateCodeOnBuild)' == 'true' AND ('$(TargetFramework)' == '' OR '$(TargetFrameworks)' == '') ) ha restituito ( 'true' == 'true' AND ('net472' == '' OR 'netstandard2.1;net472' == '') ).

I can see why one wouldn't want to run the generation multiple times, but it does not run at all.

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

captainsafia commented 2 years ago

Yep, this is a bug in the MSBuild condition here. It's valid to avoid multiple invocations for multiple targets but this disables it all together. I think a condition like:

Condition="'$(TargetFrameworks)' == '' OR $(TargetFrameworks.EndsWith($(TargetFramework)))" 

Get us a little closer to doing the invocation for only the TFM That appears first. We might have to tweak it a bit to account for projects that target more than 2 TFMs.

ghost commented 1 year ago

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

Sinhk commented 1 year ago

Not sure if it's related to this issue, but it appears to be a typo here: https://github.com/dotnet/aspnetcore/blob/e7a85fb5de16cb888783537f2374d16bcb9678dc/src/Tools/Extensions.ApiDescription.Client/src/buildMultiTargeting/Microsoft.Extensions.ApiDescription.Client.targets#L18 with two apostrophes before $(OpenApiGenerateCodeOnBuild)

AdelRefaat commented 1 year ago

I had to workaround it by setting TargetFrameworks to single/current value before running GenerateOpenApiCode during compilation Sample here: https://gist.github.com/AdelRefaat/dc3258c86e2d6e8a06fbff921c0baf96