You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You can generate a JSON:API client in various programming languages from the [OpenAPI specification](https://swagger.io/specification/) file that JsonApiDotNetCore APIs provide.
3
+
After [enabling OpenAPI](~/usage/openapi.md), you can generate a JSON:API client for your API in various programming languages.
4
4
5
-
For C# .NET clients generated using [NSwag](https://github.com/RicoSuter/NSwag), we provide an additional package
6
-
that provides workarounds for NSwag bugs and introduces support for partial PATCH/POST requests.
7
-
The concern here is that a property on a generated C# class being `null` could either mean: "set the value to `null`
8
-
in the request" or: "this is `null` because I never touched it".
5
+
The following generators are supported, though you may try others as well:
6
+
-[NSwag](https://github.com/RicoSuter/NSwag): Produces clients for C# and TypeScript
7
+
-[Kiota](https://learn.microsoft.com/en-us/openapi/kiota/overview): Produces clients for C#, Go, Java, PHP, Python, Ruby, Swift and TypeScript
8
+
9
+
# [NSwag](#tab/nswag)
10
+
11
+
For C# clients, we provide an additional package that provides workarounds for bugs in NSwag and enables using partial PATCH/POST requests.
12
+
13
+
To add it to your project, run the following command:
To generate your C# client, follow the steps below.
32
+
33
+
# [NSwag](#tab/nswag)
34
+
12
35
### Visual Studio
13
36
14
37
The easiest way to get started is by using the built-in capabilities of Visual Studio.
15
-
The next steps describe how to generate a JSON:API client library and use our package.
38
+
The following steps describe how to generate and use a JSON:API client in C#, using our package.
16
39
17
40
1. In **Solution Explorer**, right-click your client project, select **Add** > **Service Reference** and choose **OpenAPI**.
18
41
19
42
2. On the next page, specify the OpenAPI URL to your JSON:API server, for example: `http://localhost:14140/swagger/v1/swagger.json`.
20
-
Specify `ExampleApiClient` as class name, optionally provide a namespace and click **Finish**.
43
+
Specify `ExampleApiClient` as the class name, optionally provide a namespace and click **Finish**.
21
44
Visual Studio now downloads your swagger.json and updates your project file.
22
45
This adds a pre-build step that generates the client code.
23
46
24
47
> [!TIP]
25
48
> To later re-download swagger.json and regenerate the client code,
26
49
> right-click **Dependencies** > **Manage Connected Services** and click the **Refresh** icon.
27
50
28
-
3. Although not strictly required, we recommend to run package update now, which fixes some issues.
51
+
3. Although not strictly required, we recommend running package update now, which fixes some issues.
29
52
30
53
> [!WARNING]
31
54
> NSwag v14 is currently *incompatible* with JsonApiDotNetCore (tracked [here](https://github.com/RicoSuter/NSwag/issues/4662)). Stick with v13.x for the moment.
@@ -36,7 +59,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
@@ -62,10 +85,6 @@ The next steps describe how to generate a JSON:API client library and use our pa
62
85
}
63
86
```
64
87
65
-
> [!TIP]
66
-
> The project at src/Examples/OpenApiNSwagClientExample contains an enhanced version that logs the HTTP requests and responses.
67
-
> Additionally, the example shows how to write the swagger.json file to disk when building the server, which is imported from the client project. This keeps the server and client automatically in sync, which is handy when both are in the same solution.
68
-
69
88
7. Add code that calls one of your JSON:API endpoints.
70
89
71
90
```c#
@@ -121,42 +140,64 @@ The next steps describe how to generate a JSON:API client library and use our pa
121
140
}
122
141
```
123
142
143
+
> [!TIP]
144
+
> The [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiNSwagClientExample) contains an enhanced version that uses `IHttpClientFactory` for [scalability](https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory) and [resiliency](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests#use-polly-based-handlers) and logs the HTTP requests and responses.
145
+
> Additionally, the example shows how to write the swagger.json file to disk when building the server, which is imported from the client project. This keeps the server and client automatically in sync, which is handy when both are in the same solution.
146
+
124
147
### Other IDEs
125
148
126
-
When using the command-line, you can try the [Microsoft.dotnet-openapi Global Tool](https://docs.microsoft.com/en-us/aspnet/core/web-api/microsoft.dotnet-openapi?view=aspnetcore-5.0).
149
+
When using the commandline, you can try the [Microsoft.dotnet-openapi Global Tool](https://docs.microsoft.com/en-us/aspnet/core/web-api/microsoft.dotnet-openapi?view=aspnetcore-5.0).
127
150
128
-
Alternatively, the next section shows what to add to your client project file directly:
151
+
Alternatively, the following section shows what to add to your client project file directly:
From here, continue from step 3 in the list of steps for Visual Studio.
151
171
172
+
# [Kiota](#tab/kiota)
173
+
174
+
To generate your C# client, install the Kiota tool by following the steps at https://learn.microsoft.com/en-us/openapi/kiota/install#install-as-net-tool.
175
+
176
+
Next, generate client code by running the [command line tool](https://learn.microsoft.com/en-us/openapi/kiota/using#client-generation). For example:
> The `--backing-store` switch is needed for JSON:API partial PATCH/POST requests to work correctly.
184
+
185
+
Kiota is pretty young and therefore still rough around the edges. At the time of writing, there are various bugs, for which we have workarounds
186
+
in place. For a full example, see the [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiKiotaClientExample).
187
+
188
+
---
189
+
152
190
## Configuration
153
191
154
-
### NSwag
192
+
Various switches enable you to tweak the client generation to your needs. See the section below for an overview.
193
+
194
+
# [NSwag](#tab/nswag)
155
195
156
196
The `OpenApiReference` element in the project file accepts an `Options` element to pass additional settings to the client generator,
157
197
which are listed [here](https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs).
198
+
A guide with common best practices is available [here](https://stevetalkscode.co.uk/openapireference-commands).
158
199
159
-
For example, the next section puts the generated code in a namespace and generates an interface (which is handy for dependency injection):
200
+
For example, the following section puts the generated code in a namespace and generates an interface (which is handy for dependency injection):
160
201
161
202
```xml
162
203
<OpenApiReferenceInclude="swagger.json">
@@ -167,10 +208,37 @@ For example, the next section puts the generated code in a namespace and generat
167
208
</OpenApiReference>
168
209
```
169
210
211
+
Likewise, you can enable nullable reference types by adding `/GenerateNullableReferenceTypes:true`, optionally combined with `/GenerateOptionalParameters:true`.
212
+
213
+
# [Kiota](#tab/kiota)
214
+
215
+
The available command-line switches for Kiota are described [here](https://learn.microsoft.com/en-us/openapi/kiota/using#client-generation).
216
+
217
+
At the time of writing, Kiota provides [no official integration](https://github.com/microsoft/kiota/issues/3005) with MSBuild.
218
+
Our [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiKiotaClientExample) takes a stab at it, although it has glitches. If you're an MSBuild expert, please help out!
219
+
220
+
```xml
221
+
<TargetName="KiotaRunTool"BeforeTargets="BeforeCompile;CoreCompile"Condition="$(DesignTimeBuild) != true And $(BuildingProject) == true">
Due to a [bug in Kiota](https://github.com/microsoft/kiota/issues/4190), a try/catch block is needed additionally to make this work.
285
+
286
+
For a full example, see the [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiKiotaClientExample).
0 commit comments