-
Beta Was this translation helpful? Give feedback.
Answered by
mikezw
Aug 1, 2025
Replies: 2 comments 1 reply
-
Based on the Worth noting is that there is a plan to deprecate this package in .NET 10: aspnet/Announcements#518 |
Beta Was this translation helpful? Give feedback.
1 reply
-
My use is simple, so I copy some property and target in Microsoft.Extensions.ApiDescription.Client, and Multi targeting <Project>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<!--Well-known metadata of the code generator item groups.This all copy from Mircosoft.Extensions.ApiDescription.Client -->
<ItemGroup>
<AvailableItemName Include="MyOpenApiReference" />
</ItemGroup>
<!--Well-known metadata of the code generator item groups.-->
<ItemDefinitionGroup>
<MyOpenApiReference>
<!-- Name of the class to generate. Defaults to match filename in %(OutputPath). -->
<ClassName />
<!--Code generator to use. Required and must end with "CSharp" or "TypeScript" (the currently-supported targetlanguages) unless %(OutputPath) is set. Builds will invoke a target named "Generate%(CodeGenerator)" to doactual code generation.-->
<CodeGenerator>NSwagCSharp</CodeGenerator>
<!-- Namespace to contain generated class. Default is $(RootNamespace). -->
<Namespace />
<!--Options to pass to the code generator target then (likely) added to a tool's command line. Value is passedalong to the code generator but otherwise unused in this package.-->
<Options>$(OpenApiGenerateCodeOptions)</Options>
<!--Path to place generated code. Code generator may interpret path as a filename or directory. Default filename orfolder name is %(Filename)Client.[cs|ts]. Filenames and relative paths (if explicitly set) are combined with$(OpenApiCodeDirectory). Final value (depending on $(OpenApiCodeDirectory)) is likely to be a path relative tothe client project.-->
<OutputPath />
</MyOpenApiReference>
</ItemDefinitionGroup>
<!-- See https://aka.ms/dotnet/msbuild/customize for more details on customizing your build -->
<!--This copy from NSwag.ApiDescription.Client And the nswagExe property will add after directory.build.props import-->
<PropertyGroup>
<_NSwagCommand>$(NSwagExe)</_NSwagCommand>
<_NSwagCommand
Condition="'$(MSBuildRuntimeType)' == 'Core'">dotnet --roll-forward-on-no-candidate-fx 2 "$(NSwagDir_Core31)/dotnet-nswag.dll"</_NSwagCommand>
<_NSwagCommand
Condition="'$(TargetFramework)' == 'net5.0'">dotnet --roll-forward-on-no-candidate-fx 2 "$(NSwagDir_Net50)/dotnet-nswag.dll"</_NSwagCommand>
<_NSwagCommand
Condition="'$(TargetFramework)' == 'net6.0'">dotnet --roll-forward-on-no-candidate-fx 2 "$(NSwagDir_Net60)/dotnet-nswag.dll"</_NSwagCommand>
<_NSwagCommand
Condition="'$(TargetFramework)' == 'net7.0'">dotnet --roll-forward-on-no-candidate-fx 2 "$(NSwagDir_Net70)/dotnet-nswag.dll"</_NSwagCommand>
</PropertyGroup>
<!--This copy from NSwag.ApiDescription.Client with some change-->
<Target Name="MyGenerateNSwagCSharp" Condition="@(MyOpenApiReference)!=''">
<ItemGroup>
<!-- @(CurrentOpenApiReference) item group will never contain more than one item. -->
<MyOpenApiReference>
<Command>$(_NSwagCommand) openapi2csclient /className:%(ClassName) /namespace:%(Namespace)</Command>
</MyOpenApiReference>
<MyOpenApiReference>
<Command Condition="! %(FirstForGenerator)">%(Command) /GenerateExceptionClasses:false</Command>
</MyOpenApiReference>
<MyOpenApiReference>
<Command>%(Command) /input:"%(FullPath)" /output:"%(OutputPath)" %(Options)</Command>
</MyOpenApiReference>
</ItemGroup>
<Message Importance="high" Text="%0AGenerateNSwagCSharp:" />
<Message Importance="high" Text=" %(MyOpenApiReference.Command)" />
<Delete Files="@(MyOpenApiReference.OutputPath)"></Delete>
<Exec Command="%(MyOpenApiReference.Command)" LogStandardErrorAsError="true" />
<Error Condition="!Exists('%(MyOpenApiReference.OutputPath)')" Text="Generate openapi failed(%(MyOpenApiReference.ClassName))" />
</Target>
<!--This is from https://learn.microsoft.com/en-us/visualstudio/msbuild/run-target-exactly-once?view=vs-2022 -->
<Target Name="MainGeneratorTarget" DependsOnTargets="MyGenerateNSwagCSharp">
<Message Importance="high" Text="MainGeneratorTarget run" />
</Target>
<Target Name="MainGeneratorTargetBuildTargetBeforeOuterBuild" DependsOnTargets="MainGeneratorTarget" BeforeTargets="DispatchToInnerBuilds"/>
<!--MainGeneratorTarget will not run when inner builds because it finish before DispatchToInnerBuilds-->
<Target Name="MainGeneratorTargetInnerBuild" BeforeTargets="CoreCompile">
<!--RemoveProperties="TargetFramework"-->
<MSBuild Projects="$(MSBuildProjectFullPath)" Targets="MainGeneratorTarget" />
<!--Add to compile-->
<ItemGroup>
<Compile Include="%(MyOpenApiReference.OutputPath)"></Compile>
</ItemGroup>
</Target>
</Project>
In project <ItemGroup>
<MyOpenApiReference Include="OpenAPIs\swagger.json" ClassName="OpenAPI" OutputPath="obj\%(ClassName).g.cs" Namespace="OpenAPIGenerate">
<SourceUri>https://generator.swagger.io/api/swagger.json</SourceUri>
<FirstForGenerator>true</FirstForGenerator>
</MyOpenApiReference>
</ItemGroup> But these will not download the file automatically |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
mikezw
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My use is simple, so I copy some property and target in Microsoft.Extensions.ApiDescription.Client, and Multi targeting