domaindrivendev / Swashbuckle.AspNetCore

Swagger tools for documenting API's built on ASP.NET Core
MIT License
5.25k stars 1.31k forks source link

Changing <TargetFramework> to <TargetFrameowrks> in csproj makes code generator stop running #2448

Closed rwb196884 closed 6 months ago

rwb196884 commented 2 years ago

I have some old net40 code that I need to support, and I've found that I can generate the swagger proxy in net6.0 and then compile it to net40. I have this working using a net40 project that includes the generated .cs file, but of course it would be better to consolidate it to a single project.

Here's what the single project would look like. This project is generating the .cs file, but if I change<TargetFramework> to <TargetFrameworks> -- i.e., add two s characters and no other changes -- then the code generator stops working.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>disable</ImplicitUsings>
        <!--<Nullable>disable</Nullable>-->
        <IsPackable>false</IsPackable>
    </PropertyGroup>

    <!--
    The purpose of this project is to run swaggergen to obtain the http proxy code.
    This code is then edited by a build task defined below.
    The edited files are then included in Company.TheOldNet40Product.   
    -->

    <ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
        <OpenApiReference Include="swagger.json" Namespace="Company.Product.HttpApiProxy" ClassName="ProductHttpApiProxy" OutputPath="$(ProjectDir)ProductHttpApiProxy.cs">
            <Options>/AdditionalNamespaceUsages:Company.TheOldNet40Product,Company.TheOldNet40Product.Metadata,Company.Product.Database /GenerateDtoTypes:false</Options>

            <CodeGenerator>NSwagCSharp</CodeGenerator>
        <!--<Options>/GenerateDtoTypes:false</Options>-->
        </OpenApiReference>
    </ItemGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.SourceLink.AzureRepos.Git" Version="1.0.0" PrivateAssets="All" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    </ItemGroup>

    <ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
        <Reference Include="System.Net" />
        <PackageReference Include="Microsoft.Net.Http" Version="2.2.29" />
        <Reference Include="System.ComponentModel.DataAnnotations" />
        <PackageReference Include=" AsyncBridge" Version="0.3.1" />
    </ItemGroup>

    <ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
        <PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="3.0.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="NSwag.ApiDescription.Client" Version="13.0.5">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="System.Runtime" Version="4.3.1" />
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\Company.Product.Model\Company.Product.Model.csproj" />
      <ProjectReference Include="..\Company.TheOldNet40Product.Model\Company.TheOldNet40Product.Model.csproj" />
    </ItemGroup>

    <UsingTask TaskName="ReplaceFileText" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
        <ParameterGroup>
            <InputFilename ParameterType="System.String" Required="true" />
            <OutputFilename ParameterType="System.String" Required="true" />
            <MatchExpression ParameterType="System.String" Required="true" />
            <ReplacementText ParameterType="System.String" Required="true" />
        </ParameterGroup>
        <Task>
            <Using Namespace="System" />
            <Using Namespace="System.IO" />
            <Using Namespace="System.Text.RegularExpressions" />
            <Code Type="Fragment" Language="cs">
                <![CDATA[  
          File.WriteAllText(
            OutputFilename,
            Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
            );
        ]]>
            </Code>
        </Task>
    </UsingTask>

    <Target Name="BeforeBuildStep" BeforeTargets="Build">
        <ReplaceFileText InputFilename="$(ProjectDir)ProductHttpApiProxy.cs" OutputFilename="$(ProjectDir)ProductHttpApiProxy.cs" MatchExpression="IReadOnlyDictionary" ReplacementText="IDictionary" />
    </Target>
</Project>
github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 60 days with no activity. It will be automatically closed in 14 days if no further updates are made.