Skip to content

Commit 520c618

Browse files
author
Bart Koelman
committed
Layout tweaks and fixes
1 parent 65430a8 commit 520c618

File tree

3 files changed

+56
-56
lines changed

3 files changed

+56
-56
lines changed

docs/usage/openapi-client.md

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# OpenAPI Client
22

3-
You can generate a JSON:API client 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 partial PATCH/POST requests. The issue here is that a property on a generated C# class being `null` could mean "set the value to `null` in the request" or "this is `null` because I never touched it".
3+
You can generate a JSON:API client in various programming languages from the [OpenAPI specification](https://swagger.io/specification/) file that JsonApiDotNetCore APIs provide.
4+
5+
For C# .NET clients generated using [NSwag](https://github.com/RicoSuter/NSwag), we provide an additional package that introduces support for partial PATCH/POST requests. The issue here is that a property on a generated C# class being `null` could mean "set the value to `null` in the request" or "this is `null` because I never touched it".
46

57
## Getting started
68

@@ -9,28 +11,38 @@ You can generate a JSON:API client in various programming languages from the Ope
911
The easiest way to get started is by using the built-in capabilities of Visual Studio. The next steps describe how to generate a JSON:API client library and use our package.
1012

1113
1. In **Solution Explorer**, right-click your client project, select **Add** > **Service Reference** and choose **OpenAPI**.
14+
1215
2. On the next page, specify the OpenAPI URL to your JSON:API server, for example: `http://localhost:14140/swagger/v1/swagger.json`.
1316
Optionally provide a class name and namespace and click **Finish**.
1417
Visual Studio now downloads your swagger.json and updates your project file. This results in a pre-build step that generates the client code.
18+
1519
Tip: To later re-download swagger.json and regenerate the client code, right-click **Dependencies** > **Manage Connected Services** and click the **Refresh** icon.
1620
3. Although not strictly required, we recommend to run package update now, which fixes some issues and removes the `Stream` parameter from generated calls.
21+
1722
4. Add some demo code that calls one of your JSON:API endpoints. For example:
23+
1824
```c#
1925
using var httpClient = new HttpClient();
2026
var apiClient = new ExampleApiClient("http://localhost:14140", httpClient);
2127

22-
PersonCollectionResponseDocument getResponse = await apiClient.GetPersonCollectionAsync();
28+
PersonCollectionResponseDocument getResponse =
29+
await apiClient.GetPersonCollectionAsync();
2330

2431
foreach (PersonDataInResponse person in getResponse.Data)
2532
{
26-
Console.WriteLine($"Found user {person.Id} named '{person.Attributes.FirstName} {person.Attributes.LastName}'.");
33+
Console.WriteLine($"Found user {person.Id} named " +
34+
$"'{person.Attributes.FirstName} {person.Attributes.LastName}'.");
2735
}
2836
```
37+
2938
5. Add our client package to your project:
39+
3040
```
3141
dotnet add package JsonApiDotNetCore.OpenApi.Client
3242
```
43+
3344
6. Add the following glue code to connect our package with your generated code. The code below assumes you specified `ExampleApiClient` as class name in step 2.
45+
3446
```c#
3547
using JsonApiDotNetCore.OpenApi.Client;
3648
using Newtonsoft.Json;
@@ -43,7 +55,9 @@ The easiest way to get started is by using the built-in capabilities of Visual S
4355
}
4456
}
4557
```
58+
4659
7. Extend your demo code to send a partial PATCH request with the help of our package:
60+
4761
```c#
4862
var patchRequest = new PersonPatchRequestDocument
4963
{
@@ -58,10 +72,11 @@ The easiest way to get started is by using the built-in capabilities of Visual S
5872
};
5973
6074
// This line results in sending "lastName: null" instead of omitting it.
61-
using (apiClient.RegisterAttributesForRequestDocument<PersonPatchRequestDocument, PersonAttributesInPatchRequest>(
62-
patchRequest, person => person.LastName))
75+
using (apiClient.RegisterAttributesForRequestDocument<PersonPatchRequestDocument,
76+
PersonAttributesInPatchRequest>(patchRequest, person => person.LastName))
6377
{
64-
PersonPrimaryResponseDocument patchResponse = await apiClient.PatchPersonAsync("1", patchRequest);
78+
PersonPrimaryResponseDocument patchResponse =
79+
await apiClient.PatchPersonAsync("1", patchRequest);
6580
6681
// The sent request looks like this:
6782
// {
@@ -105,17 +120,18 @@ Alternatively, the next section shows what to add to your client project file di
105120

106121
From here, continue from step 3 in the list of steps for Visual Studio.
107122

108-
## Customization
123+
## Configuration
109124

110-
### NSwga
125+
### NSwag
111126

112127
The `OpenApiReference` element in the project file accepts an `Options` element to pass additional settings to the client generator,
113-
which are listed at https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs.
128+
which are listed [here](https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs).
114129

115130
For example, the next section puts the generated code in a namespace, removes the `baseUrl` parameter and generates an interface (which is handy for dependency injection):
131+
116132
```xml
117133
<OpenApiReference Include="swagger.json">
118-
<Namespace>ExampleProject.ServiceAccess.GeneratedCode</Namespace>
134+
<Namespace>ExampleProject.GeneratedCode</Namespace>
119135
<ClassName>SalesApiClient</ClassName>
120136
<CodeGenerator>NSwagCSharp</CodeGenerator>
121137
<Options>/UseBaseUrl:false /GenerateClientInterfaces:true</Options>

docs/usage/openapi.md

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,63 +3,47 @@
33
JsonApiDotNetCore provides an extension package that enables you to produce an [OpenAPI specification](https://swagger.io/specification/) for your JSON:API endpoints. This can be used to generate a [documentation website](https://swagger.io/tools/swagger-ui/) or to generate [client libraries](https://openapi-generator.tech/docs/generators/) in various languages. The package provides an integration with [Swashbuckle](https://github.com/domaindrivendev/Swashbuckle.AspNetCore).
44

55

6-
## Installation
6+
## Getting started
77

8-
Install the `JsonApiDotNetCore.OpenApi` NuGet package.
8+
1. Install the `JsonApiDotNetCore.OpenApi` NuGet package:
99

10-
### CLI
10+
```
11+
dotnet add package JsonApiDotNetCore.OpenApi
12+
```
1113
12-
```
13-
dotnet add package JsonApiDotNetCore.OpenApi
14-
```
15-
16-
### Visual Studio
17-
18-
```powershell
19-
Install-Package JsonApiDotNetCore.OpenApi
20-
```
21-
22-
### *.csproj
23-
24-
```xml
25-
<ItemGroup>
26-
<!-- Be sure to check NuGet for the latest version # -->
27-
<PackageReference Include="JsonApiDotNetCore.OpenApi" Version="4.0.0" />
28-
</ItemGroup>
29-
```
30-
31-
## Usage
14+
2. Add the integration in your `Startup` class.
3215
33-
Add the integration in your `Startup` class.
34-
35-
```c#
36-
public class Startup
37-
{
38-
public void ConfigureServices(IServiceCollection services)
16+
```c#
17+
public class Startup
3918
{
40-
IMvcCoreBuilder mvcBuilder = services.AddMvcCore();
41-
services.AddJsonApi<AppDbContext>(mvcBuilder: mvcBuilder);
19+
public void ConfigureServices(IServiceCollection services)
20+
{
21+
IMvcCoreBuilder mvcBuilder = services.AddMvcCore();
4222
43-
// Adds the Swashbuckle integration.
44-
services.AddOpenApi(mvcBuilder);
45-
}
23+
services.AddJsonApi<AppDbContext>(mvcBuilder: mvcBuilder);
4624
47-
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
48-
{
49-
app.UseRouting();
50-
app.UseJsonApi();
25+
// Adds the Swashbuckle integration.
26+
services.AddOpenApi(mvcBuilder);
27+
}
28+
29+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
30+
{
31+
app.UseRouting();
32+
app.UseJsonApi();
5133
52-
// Adds the Swashbuckle middleware.
53-
app.UseSwagger();
34+
// Adds the Swashbuckle middleware.
35+
app.UseSwagger();
5436
55-
app.UseEndpoints(endpoints => endpoints.MapControllers());
37+
app.UseEndpoints(endpoints => endpoints.MapControllers());
38+
}
5639
}
57-
}
58-
```
40+
```
5941
6042
By default, the OpenAPI specification will be available at `http://localhost:<port>/swagger/v1/swagger.json`.
6143
62-
Swashbuckle also ships with [SwaggerUI](https://swagger.io/tools/swagger-ui/), tooling for a generated documentation page. This can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Startup` class.
44+
## Documentation
45+
46+
Swashbuckle also ships with [SwaggerUI](https://swagger.io/tools/swagger-ui/), tooling for a generated documentation page. This can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Startup` class:
6347
6448
```c#
6549
// Startup.cs

docs/usage/toc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
# [Metadata](meta.md)
2323
# [Caching](caching.md)
2424

25-
# [OpenAPI](openapi.md))
26-
## [Client](openapi-client.md))
25+
# [OpenAPI](openapi.md)
26+
## [Client](openapi-client.md)
2727

2828
# Extensibility
2929
## [Layer Overview](extensibility/layer-overview.md)

0 commit comments

Comments
 (0)