Skip to content

Move existing integration to JADNC #1076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1c099e7
Copied integration and fixed compilation errors
maurei Sep 8, 2021
54a023f
Add models and adjusted models in OpenAPI document tests such that i …
maurei Sep 8, 2021
09ab5c0
Add FlightAttendantController to used controllers
maurei Sep 9, 2021
19cc14d
Added client library with tests to project; made adjustmets to the mo…
maurei Sep 9, 2021
ec2ccaf
Adjusted build script to create OpenApiClient artifact, added documen…
maurei Sep 9, 2021
c6d691d
Adjusted docs and minor refactors
maurei Sep 10, 2021
ef2753e
Replace resource name formatter with internal one
maurei Sep 10, 2021
b3cdc7a
Removed JsonApiInputFormatter workaround
maurei Sep 10, 2021
a955616
Merge branch 'openapi' into oa/existing-integration
maurei Sep 13, 2021
bad9aad
Moved full document test to ExistingOpenApiIntegration test folder, w…
maurei Sep 14, 2021
ea46194
Disabled OpenAPI for nonjson and operations controller, as these are …
maurei Sep 14, 2021
a0d5bff
review feedback on docs
maurei Sep 14, 2021
280e449
Add a very minimal example of a generated OpenAPI client for JsonApiD…
maurei Sep 14, 2021
7b3d1ef
Fixed inspect and cleanup code issues that do not appear when execute…
maurei Sep 15, 2021
ba42b77
rename existing to legacy in ExistingOpenApiIntegration folder and na…
maurei Sep 15, 2021
ecd971e
moved OpenApiClient to OpenApi.Client
maurei Sep 15, 2021
37a95b3
Configure NSwag to generate a client with access modifier set to inte…
maurei Sep 15, 2021
da1db37
Move OpenAPI client tests to dedicated test project, cleared up some …
maurei Sep 15, 2021
f71bbea
Some adjustments to stay closer to 1-on-1 model mapping from the priv…
maurei Sep 15, 2021
348dffa
review feedback
maurei Sep 15, 2021
e88c762
Process inspectcode issues that do not appear when run locally
maurei Sep 15, 2021
54ac898
Update package reference in build script
maurei Sep 16, 2021
83ac507
Changed models to stay closer 1-to-1 mapping. Changed 'OpenApiClient…
maurei Sep 16, 2021
aaf59a6
process review feedback
maurei Sep 16, 2021
1baed2d
rename ClassName in client test project
maurei Sep 16, 2021
65430a8
Updated example client project using Visual Studio defaults and updat…
Sep 16, 2021
520c618
Layout tweaks and fixes
Sep 16, 2021
b72d823
fixed failed automatic rename 'openApiOpenApi'
maurei Sep 17, 2021
43cec51
Merge branch 'oa/existing-integration' of https://github.com/json-api…
maurei Sep 17, 2021
605d2ce
reduced changed wrt private version of this test
maurei Sep 17, 2021
8c2a7b4
Changed ID type from to for Flight
maurei Sep 17, 2021
a83de89
ReserveCabinPersonnel -> Purser, CabinPersonnel -> CabinCrewMembers
maurei Sep 17, 2021
8ceff7b
fixed failed automatic rename 'openApiOpenApi'
maurei Sep 17, 2021
5a4a06f
Flight: replaced OperatingAirplane with Purser, added to many relatio…
maurei Sep 17, 2021
7a88a64
Review feedback
maurei Sep 17, 2021
4073c3f
Fix client lifetime test
maurei Sep 17, 2021
d3ebfaf
Feedback
maurei Sep 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ function CreateNuGetPackage {
if ([string]::IsNullOrWhitespace($versionSuffix)) {
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts
dotnet pack .\src\JsonApiDotNetCore.OpenApi -c Release -o .\artifacts
dotnet pack .\src\JsonApiDotNetCore.OpenApiClient -c Release -o .\artifacts
}
else {
dotnet pack .\src\JsonApiDotNetCore -c Release -o .\artifacts --version-suffix=$versionSuffix
dotnet pack .\src\JsonApiDotNetCore.OpenApi -c Release -o .\artifacts --version-suffix=$versionSuffix
dotnet pack .\src\JsonApiDotNetCore.OpenApiClient -c Release -o .\artifacts --version-suffix=$versionSuffix
}

CheckLastExitCode
Expand Down
30 changes: 30 additions & 0 deletions JsonApiDotNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCore.OpenApi",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenApiTests", "test\OpenApiTests\OpenApiTests.csproj", "{B693DE14-BB28-496F-AB39-B4E674ABCA80}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCore.OpenApiClient", "src\JsonApiDotNetCore.OpenApiClient\JsonApiDotNetCore.OpenApiClient.csproj", "{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonApiDotNetCoreExampleClient", "src\Examples\JsonApiDotNetCoreExampleClient\JsonApiDotNetCoreExampleClient.csproj", "{7FC5DFA3-6F66-4FD8-820D-81E93856F252}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -238,6 +242,30 @@ Global
{B693DE14-BB28-496F-AB39-B4E674ABCA80}.Release|x64.Build.0 = Release|Any CPU
{B693DE14-BB28-496F-AB39-B4E674ABCA80}.Release|x86.ActiveCfg = Release|Any CPU
{B693DE14-BB28-496F-AB39-B4E674ABCA80}.Release|x86.Build.0 = Release|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Debug|x64.ActiveCfg = Debug|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Debug|x64.Build.0 = Debug|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Debug|x86.ActiveCfg = Debug|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Debug|x86.Build.0 = Debug|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Release|Any CPU.Build.0 = Release|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Release|x64.ActiveCfg = Release|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Release|x64.Build.0 = Release|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Release|x86.ActiveCfg = Release|Any CPU
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}.Release|x86.Build.0 = Release|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Debug|x64.ActiveCfg = Debug|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Debug|x64.Build.0 = Debug|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Debug|x86.ActiveCfg = Debug|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Debug|x86.Build.0 = Debug|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Release|Any CPU.Build.0 = Release|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Release|x64.ActiveCfg = Release|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Release|x64.Build.0 = Release|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Release|x86.ActiveCfg = Release|Any CPU
{7FC5DFA3-6F66-4FD8-820D-81E93856F252}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -258,6 +286,8 @@ Global
{210FD61E-FF5D-4CEE-8E0D-C739ECCCBA21} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
{71287D6F-6C3B-44B4-9FCA-E78FE3F02289} = {7A2B7ADD-ECB5-4D00-AA6A-D45BD11C97CF}
{B693DE14-BB28-496F-AB39-B4E674ABCA80} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C} = {7A2B7ADD-ECB5-4D00-AA6A-D45BD11C97CF}
{7FC5DFA3-6F66-4FD8-820D-81E93856F252} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OpenAPI
# OpenAPI API Description

You can describe your API with an OpenAPI specification using the [Swashbuckle](https://github.com/domaindrivendev/Swashbuckle.AspNetCore) integration for JsonApiDotNetCore.

Expand Down Expand Up @@ -39,8 +39,8 @@ public class Startup
IMvcCoreBuilder mvcBuilder = services.AddMvcCore();
services.AddJsonApi<AppDbContext>(mvcBuilder: mvcBuilder);

// Adds the Swashbuckle integration.
services.AddOpenApi(mvcBuilder);
// Adds the Swashbuckle integration.
services.AddOpenApi(mvcBuilder);
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
Expand Down
137 changes: 137 additions & 0 deletions docs/usage/openapi/client-library.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# OpenAPI Client Library

You can generate a client library in various programming languages from the OpenAPI specification file that JsonApiDotNetCore APIs provide. For C# .NET clients generated using NSwag, we provide an additional package that introduces support for sending partial write requests.

## Installation

You are required to install the following NuGet packages:

- `JsonApiDotNetCore.OpenApiClient`
- `NSwag.ApiDescription.Client`
- `Microsoft.Extensions.ApiDescription.Cient`
- `NSwag.ApiDescription.Client`

The following examples demonstrate how to install the `JsonApiDotNetCore.OpenApiClient` package.

### CLI

```
dotnet add package JsonApiDotNetCore.OpenApiClient
```

### Visual Studio

```powershell
Install-Package JsonApiDotNetCore.OpenApiClient
```

### *.csproj

```xml
<ItemGroup>
<!-- Be sure to check NuGet for the latest version # -->
<PackageReference Include="JsonApiDotNetCore.OpenApiClient" Version="4.0.0" />
</ItemGroup>
```


## Adding an OpenApiReference

Add a reference to your OpenAPI specification in your project file as demonstrated below.

```xml
<ItemGroup>
<OpenApiReference Include="swagger.json">
<Namespace>JsonApiDotNetCoreExampleClient.GeneratedCode</Namespace>
<ClassName>ExampleApiClient</ClassName>
<CodeGenerator>NSwagCSharp</CodeGenerator>
<Options>/UseBaseUrl:false /GenerateClientInterfaces:true</Options>
</OpenApiReference>
</ItemGroup>
```


## Usage

The NSwag tooling generates the OpenAPI client during a prebuild step. Once your application is built,
you can instantiate it using the class name as indicated in the project file.

```c#
namespace JsonApiDotNetCoreExampleClient
{
class Program
{
static void Main(string[] args)
{
using (HttpClient httpClient = new HttpClient())
{
ExampleApiClient exampleApiClient = new ExampleApiClient(httpClient);

// IntelliSense is now available on `exampleApiClient`!
}
}
}
}
```

Support for partial write requests can be enabled by leveraging the extensibility points of the generated client.

```c#
namespace JsonApiDotNetCoreExampleClient.GeneratedCode
{
// Note that this class should be in the same namespace as the ExampleApiClient generated by NSwag.
public partial class ExampleApiClient : JsonApiClient
{
partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
{
SetSerializerSettingsForJsonApi(settings);
}
}
}
```

You can now perform a write request by calling the `RegisterAttributesForRequest` method. Calling this method treats all attributes that contain their default value (<c>null</c> for reference types, <c>0</c> for integers, <c>false</c> for booleans, etc) as omitted unless explicitly listed to include them using the `alwaysIncludedAttributeSelectors` parameter.

```c#
// Program.cs
static void Main(string[] args)
{
using (HttpClient httpClient = new HttpClient())
{
ExampleApiClient exampleApiClient = new ExampleApiClient(httpClient);

var requestDocument = new PersonPatchRequestDocument
{
Data = new PersonDataInPatchRequest
{
Id = "546",
Type = PersonResourceType.People,
Attributes = new PersonAttributesInPatchRequest
{
FirstName = "Jack"
}
}
};

using (apiClient.RegisterAttributesForRequestDocument<PersonPatchRequestDocument, PersonDataInPatchRequest>(requestDocument, person => person.LastName)
{
await exampleApiClient.PatchPersonAsync(543, requestDocument));

// The request will look like this:
//
// {
// "data": {
// "type": "people",
// "id": "543",
// "attributes": {
// "firstName": "Jack",
// "lastName": null,
// }
// }
// }
}

}
}
```

5 changes: 4 additions & 1 deletion docs/usage/toc.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
# [Errors](errors.md)
# [Metadata](meta.md)
# [Caching](caching.md)
# [OpenAPI](openapi.md)

# OpenAPI
## [API Description](openapi/api-description.md))
## [Client Library](openapi/client-library.md))

# Extensibility
## [Layer Overview](extensibility/layer-overview.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using JsonApiDotNetCore.OpenApiClient;
using Newtonsoft.Json;

namespace JsonApiDotNetCoreExampleClient.GeneratedCode
{
// ReSharper disable once MemberCanBeInternal
public partial class ExampleApiClient : JsonApiClient
{
partial void UpdateJsonSerializerSettings(JsonSerializerSettings settings)
{
SetSerializerSettingsForJsonApi(settings);

settings.Formatting = Formatting.Indented;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using JsonApiDotNetCore.OpenApiClient;

namespace JsonApiDotNetCoreExampleClient.GeneratedCode
{
// ReSharper disable once MemberCanBeInternal
partial interface IExampleApiClient : IJsonApiClient
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(NetCoreAppVersion)</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.ApiDescription.Client" Version="5.0.9">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NSwag.ApiDescription.Client" Version="13.13.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<OpenApiReference Include="swagger.json">
<ClassName>ExampleApiClient</ClassName>
<CodeGenerator>NSwagCSharp</CodeGenerator>
<Namespace>JsonApiDotNetCoreExampleClient.GeneratedCode</Namespace>
<Options>/UseBaseUrl:false /GenerateClientInterfaces:true</Options>
</OpenApiReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\JsonApiDotNetCore.OpenApiClient\JsonApiDotNetCore.OpenApiClient.csproj" />
</ItemGroup>
</Project>
36 changes: 36 additions & 0 deletions src/Examples/JsonApiDotNetCoreExampleClient/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using JsonApiDotNetCoreExampleClient.GeneratedCode;

#pragma warning disable AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException

namespace JsonApiDotNetCoreExampleClient
{
// ReSharper disable once ClassNeverInstantiated.Global
internal class Program
{
private static async Task Main()
{
using var httpClient = new HttpClient
{
BaseAddress = new Uri("http://localhost:14140")
};

IExampleApiClient exampleApiClient = new ExampleApiClient(httpClient);

try
{
const int nonExistingId = int.MaxValue;
await exampleApiClient.Delete_personAsync(nonExistingId);
}
catch (ApiException exception)
{
Console.WriteLine(exception.Response);
}

Console.WriteLine("Press any key to close.");
Console.ReadKey();
}
}
}
Loading