RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

NSwag.MSBuild v12.3.1 swagger2csclient client dependencies #2205

Closed stevenvolckaert closed 5 years ago

stevenvolckaert commented 5 years ago

I'm using NSwag.MSBuild v12.3.1 swagger2csclient to generate C# clients from a Swagger v2 document, then I'm packaging the output as a NuGet package so I can use it across all projects that need to use the service the client wraps.

As explained on the SwaggerToCSharpClientGenerator wiki page, libs targeting .NET Standard 1.4+ require:

But which versions of those NuGet packages should the client project / NuGet package depend on? I'm looking specifically for the lowest supported versions; as I need to use the client package in legacy projects and upgrading the dependencies there is difficult.

swagger2csclient produces these lines of comments at the top of its output:

//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v12.3.1.0 (NJsonSchema v9.14.1.0 (Newtonsoft.Json v11.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------

Does this mean my package should require a package dependency to Newtonsoft.Json v11.0.0.0? Or is e.g. Version="11.*" OK? Or perhaps I can go even lower? The oldest legacy apps my client package needs to support depend on Newtonsoft.Json v10.0.2.

Below my current project file of the client project. Before building the source files, it first downloads a Swagger / Open API spec from the Internet, then uses swagger2csclient to generate auto-generated.cs in the project root. Note: The Kdc.* packages are on a private NuGet feed

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

  <Import Project="..\common.props" />

  <PropertyGroup>
    <Description>$(AssemblyName) provides types that allow access to the Connective eSignature REST API endpoints in a type-safe manner.</Description>
    <TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
    <RootNamespace>Kdc.DocumentSigningService.Connective</RootNamespace>
    <OpenApiClientClassName>ESignaturesClient</OpenApiClientClassName>
    <OpenApiDocumentUrl>https://www.esignatures.eu/swagger/docs/v3</OpenApiDocumentUrl>
  </PropertyGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
    <!-- Docs @ https://docs.microsoft.com/en-us/dotnet/standard/frameworks#how-to-specify-target-frameworks -->
    <Reference Include="System.Net.Http" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Kdc.AspNetCore" Version="1.1.0-alpha8" />
    <PackageReference Include="Kdc.Core" Version="1.1.0-alpha8" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="NSwag.MSBuild" Version="12.3.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
    <PackageReference Include="System.ComponentModel.Annotations" Version="4.5.0" />
  </ItemGroup>

  <Target Name="DownloadOpenApiDocument" BeforeTargets="BeforeBuild">
    <DownloadFile DestinationFileName="swagger.json" DestinationFolder="$(MSBuildProjectDirectory)" Retries="3" SourceUrl="$(OpenApiDocumentUrl)">
    </DownloadFile>
  </Target>

  <Target Name="GenerateServiceClientFromOpenApiDocument" BeforeTargets="BeforeBuild" DependsOnTargets="DownloadOpenApiDocument">
    <!-- Docs @ https://github.com/RicoSuter/NSwag/wiki/CommandLine#client-generators -->
    <Exec Command="$(NSwagExe_Core22) swagger2csclient /input:$(MSBuildProjectDirectory)\swagger.json /classname:$(OpenApiClientClassName) /namespace:$(RootNamespace) /usebaseurl:false /output:auto-generated.cs" />
    <Delete Files="$(MSBuildProjectDirectory)\swagger.json" />
  </Target>

</Project>
RicoSuter commented 5 years ago

Does this mean my package should require a package dependency to Newtonsoft.Json v11.0.0.0? Or is e.g. Version="11.*" OK? Or perhaps I can go even lower?

This is just the version which was used in the generator process, not what you actually need in your client library... should also work with newtonsoft.json v9

stevenvolckaert commented 5 years ago

Thanks a lot @RicoSuter !