Skip to content

Commit

Permalink
Merge pull request #3748 from RicoSuter/master
Browse files Browse the repository at this point in the history
Release v13.14.5
  • Loading branch information
RicoSuter authored Nov 23, 2021
2 parents 7e78409 + 482cc6a commit a0aa1d8
Show file tree
Hide file tree
Showing 68 changed files with 462 additions and 1,036 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![npm](https://img.shields.io/npm/v/nswag.svg)](https://www.npmjs.com/package/nswag)
[![build](https://github.com/RicoSuter/NSwag/actions/workflows/build.yml/badge.svg)](https://github.com/RicoSuter/NSwag/actions/workflows/build.yml)
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-1dce73.svg)](https://gitter.im/NSwag/NSwag)
[![Discord](https://img.shields.io/badge/Discord-join%20chat-1dce73.svg)](https://discord.gg/4x48JjUT)
[![StackOverflow](https://img.shields.io/badge/questions-on%20StackOverflow-orange.svg?style=flat)](http://stackoverflow.com/questions/tagged/nswag)
[![Wiki](https://img.shields.io/badge/docs-in%20wiki-orange.svg?style=flat)](https://github.com/RicoSuter/nswag/wiki)
[![Backers on Open Collective](https://opencollective.com/NSwag/backers/badge.svg)](#backers)
Expand Down
14 changes: 3 additions & 11 deletions build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO.Compression;
using System.Linq;
using Nuke.Common;
using Nuke.Common.CI.AppVeyor;
using Nuke.Common.Execution;
using Nuke.Common.Git;
using Nuke.Common.IO;
Expand All @@ -25,7 +24,6 @@
using static Nuke.Common.Tools.Npm.NpmTasks;
using static Nuke.Common.Tools.NuGet.NuGetTasks;
using static Nuke.Common.Tools.VSTest.VSTestTasks;
using Enumerable = System.Linq.Enumerable;

[CheckBuildProjectConfigurations]
partial class Build : NukeBuild
Expand Down Expand Up @@ -185,16 +183,9 @@ partial class Build : NukeBuild
.After(Compile)
.Executes(() =>
{
var logger = "";
if (AppVeyor.Instance is not null)
{
// https://github.com/appveyor/ci/issues/1601
// logger = "Appveyor";
}

var webApiTest = Solution.GetProject("NSwag.Generation.WebApi.Tests");
VSTest(x => x
.SetLogger(logger)
.SetTestAssemblies(SourceDirectory / "NSwag.Generation.WebApi.Tests" / "bin" / Configuration / "NSwag.Generation.WebApi.Tests.dll")
.SetTestAssemblies(webApiTest.Directory / "bin" / Configuration / "NSwag.Generation.WebApi.Tests.dll")
);

/*
Expand Down Expand Up @@ -267,6 +258,7 @@ partial class Build : NukeBuild
var project = Solution.GetProject(projectName);
MSBuild(x => x
.SetProcessWorkingDirectory(project.Directory)
.SetNodeReuse(IsLocalBuild)
);
var process = StartProcess(nswagCommand, $"run /runtime:{runtime}", workingDirectory: project.Directory);
process.WaitForExit();
Expand Down
137 changes: 137 additions & 0 deletions docs/tutorials/GenerateProxyClientWithCLI/generate-proxy-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# A How-To: Generating the Service Client Proxy code

## The Sample Problem

You've recently joined a large project, with a distributed team, collaborating on building a Client Application - which could be a Mobile, Desktop, or Web App - and the application being built needs to integrate with a 3rd-Party Service that you don't have much visibility into, or little ability to change.

The 3rd-Party Service does have well defined contracts, deployed to integration-test endpoints, and the Specs are shared with you via an [OpenAPI Spec](https://swagger.io/specification/).

You are asked to find a repeatable approach to generate interfaces and DTOs from the spec, based on the above constraints.

Also, since you've come to this repo, we'll assume you want to use NSwag as part of your solution.

## Prerequisites

- `NSwag` - This process we'll cover requires the `NSwag` commandline tool to help automate generation of a service client, an interface definition and DTOs. Follow [install instructions](https://github.com/RicoSuter/NSwag/wiki/CommandLine) to install the command line version of nswag.
- [OpenAPI Swagger Editor VS Code Extension](https://marketplace.visualstudio.com/items?itemName=42Crunch.vscode-openapi) *(optional)* - This Visual Studio Code (VS Code) extension adds rich support for the OpenAPI Specification (OAS) (formerly known as Swagger Specification) in JSON or YAML format. The features include, for example, SwaggerUI and ReDoc preview, IntelliSense, linting, schema enforcement, code navigation, definition links, snippets, static security analysis, and more!
- If in later steps you choose to download the 3rd-Party Service's Open API Spec, this plugin makes it easy visualize

**Notes**:

- You may need to specify runtime version `nswag version /runtime:Net50` to run nswag on your local machine, since the sample [nswag config](https://github.com/RicoSuter/NSwag/wiki/NSwag-Configuration-Document) we'll use specifies `runtime` as `Net50`.
- If you chose to download NSwag as a ZIP Archive, you may see dotnet version errors when trying to execute commands. If you are not able to resolve the issues, you may opt to install via Chocolatey or the MSI from the install instructions page provided above.
- For this sample, we'll use the public [Swagger Petstore](https://petstore.swagger.io/) as the sample 3rd-Party Service, later referred to as `[YourRemoteService]`.

## Creating Client Interface, DTOs and Proxy classes

### **OPTION 1**: The sample process for (re)creating the service client, interface definition and DTOs, using an existing nswag configuration file, is as follows

- Use an existing [nswag config document](https://github.com/RicoSuter/NSwag/wiki/NSwag-Configuration-Document), similar to [sample.nswag](./sample.nswag) from this sample folder.
- In your App codebase, check for folders similar to `MainApp > Services > [YourRemoteService]`, and `MainApp > Contracts > [YourRemoteService]`, if they are missing, create them
- If `[YourRemoteService]` has a public unauthenticated Open API Spec endpoint, one that gets appropriately versioned before changes are published, you can use it directly.
- Otherwise, I recommend downloading the desired version (typically current version) of your [Service Swagger](https://petstore.swagger.io/v2/swagger.json) from the Cloud and place the file in the same directory as the [sample.nswag](./sample.nswag).
- If you downloaded a copy of the OpenAPI-Spec, you may want to include it along side with the nswag generated files, for reference and ad-hoc version tracking.
- Generate the service client, interface definitions and DTOs by running the `nswag` CLI:

```
nswag run sample.nswag /runtime:Net50
```

- Take the output from the above command, an update files, if needed, in your `MainApp > Services > [YourRemoteService]`, and `MainApp > Contracts > [YourRemoteService]` folders
- Check if any updates are needed to service instances (or mock instances) that you may have registered with a Dependency Injection container used in your App, if any.

### **OPTION 2**: To customize the outputs, follow these additional steps

- Create your own `sample.nswag` configuration based on the starter sample below.
- Check for folders similar to `MainApp > Services > [YourRemoteService]`, and `MainApp > Contracts > [YourRemoteService]`, if missing, create them
- Update the location of the OpenAPI spec to the `documentGenerator.fromDocument.url` parameter in the `nswag` config file. The parameter can point to the local Yaml or Json file you downloaded, or an http address.
- Update the class name `codeGenerators.openApiToCSharpClient.className` parameter in the `nswag` config file, to something other than *`SampleService`*
- Update the namespace `codeGenerators.openApiToCSharpClient.namespace` parameter in the `nswag` config file, to something other than *`MainApp.Services.SampleService`*
- Update the location of the generated C# code file `codeGenerators.openApiToCSharpClient.output` parameter in the `nswag` config file.
- If you want the interface definition and DTOs as a separate file, ensure `codeGenerators.openApiToCSharpClient.generateContractsOutput` is set to **`true`**, and
- Update the contractsNamespace `codeGenerators.openApiToCSharpClient.contractsNamespace` parameter in the `nswag` config file, to something other than *`MainApp.Services.SampleService.Contracts`*
- Update the location of the generated C# contracts code file `codeGenerators.openApiToCSharpClient.contractsOutputFilePath` parameter in the `nswag` config file.
- If you do not use a BaseClass beyond the generated interface, then set `codeGenerators.openApiToCSharpClient.clientBaseClass` to **`null`**, and `codeGenerators.openApiToCSharpClient.useHttpRequestMessageCreationMethod` to **`false`**
- Generate the service client, interface definitions and DTOs by running the `nswag` CLI:

```
nswag run sample.nswag /runtime:Net50
```

- Update files, if needed, in your `MainApp > Services > [YourRemoteService]`, and `MainApp > Contracts > [YourRemoteService]` folders
- Check if any updates are needed to service instances (or mock instances) registered with a Dependency Injection container in your App, if any.

## Sample NSwag config

```
{
"runtime": "Net50",
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "YOUR_OPENAPI_SPEC_LOCATION_HERE",
"output": null,
"newLineBehavior": "Auto"
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"generateClientClasses": true,
"generateClientInterfaces": true,
"generateDtoTypes": true,
"injectHttpClient": true,
"disposeHttpClient": true,
"generateExceptionClasses": true,
"exceptionClass": "ServiceException",
"wrapDtoExceptions": false,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": true,
"useBaseUrl": true,
"generateBaseUrlProperty": true,
"generateSyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "public",
"clientBaseClass": "MainApp.Services.BaseService",
"typeAccessModifier": "public",
"generateContractsOutput": true,
"contractsNamespace": "MainApp.Services.SampleService.Contracts",
"contractsOutputFilePath": "GENERATEDCONTRACTS.cs",
"parameterDateTimeFormat": "s",
"generateUpdateJsonSerializerSettingsMethod": true,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "SampleService",
"operationGenerationMode": "MultipleClientsFromOperationId",
"generateOptionalParameters": false,
"generateJsonMethods": true,
"parameterArrayType": "System.Collections.Generic.IEnumerable",
"parameterDictionaryType": "System.Collections.Generic.IDictionary",
"responseArrayType": "System.Collections.ObjectModel.ObservableCollection",
"responseDictionaryType": "System.Collections.Generic.Dictionary",
"wrapResponses": false,
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "MainApp.Services.SampleService",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTime",
"dateTimeType": "System.DateTime",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.ObjectModel.ObservableCollection",
"arrayInstanceType": "System.Collections.ObjectModel.ObservableCollection",
"dictionaryType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.ObservableCollection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "poco",
"generateDefaultValues": true,
"generateDataAnnotations": false,
"excludedTypeNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"output": "GENERATEDCODE.cs"
}
}
}
```
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<ItemGroup>
<!-- ## NSwag -->
<PackageReference Include="NSwag.AspNetCore" Version="13.14.4" />
<PackageReference Include="NSwag.MSBuild" Version="13.14.4">
<PackageReference Include="NSwag.AspNetCore" Version="13.14.5" />
<PackageReference Include="NSwag.MSBuild" Version="13.14.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@
<Reference Include="NJsonSchema, Version=10.5.2.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\..\packages\NJsonSchema.10.5.2\lib\net45\NJsonSchema.dll</HintPath>
</Reference>
<Reference Include="NSwag.AspNet.Owin, Version=13.14.4.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.AspNet.Owin.13.14.4\lib\net45\NSwag.AspNet.Owin.dll</HintPath>
<Reference Include="NSwag.AspNet.Owin, Version=13.14.5.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.AspNet.Owin.13.14.5\lib\net45\NSwag.AspNet.Owin.dll</HintPath>
</Reference>
<Reference Include="NSwag.Core, Version=13.14.4.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.Core.13.14.4\lib\net45\NSwag.Core.dll</HintPath>
<Reference Include="NSwag.Core, Version=13.14.5.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.Core.13.14.5\lib\net45\NSwag.Core.dll</HintPath>
</Reference>
<Reference Include="NSwag.Generation, Version=13.14.4.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.Generation.13.14.4\lib\net45\NSwag.Generation.dll</HintPath>
<Reference Include="NSwag.Generation, Version=13.14.5.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.Generation.13.14.5\lib\net45\NSwag.Generation.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NSwag.Generation.WebApi, Version=13.14.4.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.Generation.WebApi.13.14.4\lib\net45\NSwag.Generation.WebApi.dll</HintPath>
<Reference Include="NSwag.Generation.WebApi, Version=13.14.5.0, Culture=neutral, PublicKeyToken=c2d88086e098d109, processorArchitecture=MSIL">
<HintPath>..\..\packages\NSwag.Generation.WebApi.13.14.5\lib\net45\NSwag.Generation.WebApi.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
Expand Down
7 changes: 2 additions & 5 deletions src/NSwag.Annotations/NSwag.Annotations.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.0;net45;netstandard2.0</TargetFrameworks>
<Description>NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript</Description>
<Version>13.14.4</Version>
<Version>13.14.5</Version>
<PackageTags>OpenAPI Swagger AspNetCore Documentation CodeGen TypeScript WebApi AspNet</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand All @@ -19,13 +19,10 @@
<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>TRACE;DEBUG</DefineConstants>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\assets\NuGetIcon.png" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>NSwag.ApiDescription.Client</id>
<version>13.14.4</version>
<version>13.14.5</version>
<authors>Rico Suter</authors>
<owners>Rico Suter</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand All @@ -16,7 +16,7 @@
<developmentDependency>true</developmentDependency>
<dependencies>
<dependency id="Microsoft.Extensions.ApiDescription.Client" version="3.0.0" />
<dependency id="NSwag.MSBuild" version="13.14.4" />
<dependency id="NSwag.MSBuild" version="13.14.5" />
</dependencies>
<references />
</metadata>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
<_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>
</PropertyGroup>

<!-- OpenApiReference support for C# -->
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.AspNet.Owin/NSwag.AspNet.Owin.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45</TargetFrameworks>
<TargetFramework>net45</TargetFramework>
<Description>NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript</Description>
<Version>13.14.4</Version>
<Version>13.14.5</Version>
<PackageTags>OpenAPI Swagger AspNetCore Documentation CodeGen TypeScript WebApi AspNet</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.AspNet.WebApi/NSwag.AspNet.WebApi.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45</TargetFrameworks>
<TargetFramework>net45</TargetFramework>
<Description>NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript</Description>
<Version>13.14.4</Version>
<Version>13.14.5</Version>
<PackageTags>OpenAPI Swagger AspNetCore Documentation CodeGen TypeScript WebApi AspNet</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net461</TargetFrameworks>
<TargetFramework>net461</TargetFramework>
<PlatformTarget>x86</PlatformTarget>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<Version>13.14.4</Version>
<Version>13.14.5</Version>
<Authors>Rico Suter</Authors>
<Company>Rico Suter</Company>
<Description>NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript</Description>
Expand All @@ -15,7 +15,6 @@
<PackageTags>OpenAPI Swagger AspNetCore Documentation CodeGen TypeScript WebApi AspNet</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\NSwag.AspNetCore.Launcher\Program.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PlatformTarget Condition="'$(TargetFramework)'=='net461'">x64</PlatformTarget>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<Version>13.14.4</Version>
<Version>13.14.5</Version>
<Authors>Rico Suter</Authors>
<Company>Rico Suter</Company>
<Description>NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AspNetCore/NSwag.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>net451;netstandard1.6;netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<Description>NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript</Description>
<Version>13.14.4</Version>
<Version>13.14.5</Version>
<PackageTags>Swagger Documentation AspNetCore NetCore TypeScript CodeGen</PackageTags>
<Copyright>Copyright © Rico Suter, 2020</Copyright>
<PackageLicenseUrl>https://github.com/RicoSuter/NSwag/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Loading

0 comments on commit a0aa1d8

Please sign in to comment.