Skip to content

Commit e9476b8

Browse files
committed
Add OpenAPI support for atomic operations
1 parent eab1f55 commit e9476b8

File tree

459 files changed

+46466
-1516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

459 files changed

+46466
-1516
lines changed

CSharpGuidelinesAnalyzer.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<cSharpGuidelinesAnalyzerSettings>
33
<setting rule="AV1561" name="MaxParameterCount" value="6" />
4-
<setting rule="AV1561" name="MaxConstructorParameterCount" value="13" />
4+
<setting rule="AV1561" name="MaxConstructorParameterCount" value="15" />
55
</cSharpGuidelinesAnalyzerSettings>

docs/usage/openapi-client.md

+38-12
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The following steps describe how to generate and use a JSON:API client in C#, us
6767
5. Add the following line inside the **OpenApiReference** section in your project file:
6868
6969
```xml
70-
<Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.NSwag</Options>
70+
<Options>/GenerateExceptionClasses:false /GenerateNullableReferenceTypes:true /GenerateOptionalPropertiesAsNullable:true /GenerateOptionalParameters:true /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.NSwag</Options>
7171
```
7272
7373
6. Add the following glue code to connect our package with your generated code.
@@ -105,31 +105,32 @@ The following steps describe how to generate and use a JSON:API client in C#, us
105105
106106
foreach (var person in getResponse.Data)
107107
{
108-
Console.WriteLine($"Found person {person.Id}: {person.Attributes.DisplayName}");
108+
Console.WriteLine($"Found person {person.Id}: {person.Attributes!.DisplayName}");
109109
}
110110
```
111111
112112
8. Extend your demo code to send a partial PATCH request with the help of our package:
113113
114114
```c#
115-
var patchRequest = new PersonPatchRequestDocument
115+
var updatePersonRequest = new UpdatePersonRequestDocument
116116
{
117-
Data = new PersonDataInPatchRequest
117+
Data = new DataInUpdatePersonRequest
118118
{
119119
Id = "1",
120-
Attributes = new PersonAttributesInPatchRequest
120+
Attributes = new AttributesInUpdatePersonRequest
121121
{
122122
LastName = "Doe"
123123
}
124124
}
125125
};
126126
127127
// This line results in sending "firstName: null" instead of omitting it.
128-
using (apiClient.WithPartialAttributeSerialization<PersonPatchRequestDocument, PersonAttributesInPatchRequest>(patchRequest,
129-
person => person.FirstName))
128+
using (apiClient.WithPartialAttributeSerialization<UpdatePersonRequestDocument, AttributesInUpdatePersonRequest>(
129+
updatePersonRequest, person => person.FirstName))
130130
{
131131
// Workaround for https://github.com/RicoSuter/NSwag/issues/2499.
132-
await ApiResponse.TranslateAsync(() => apiClient.PatchPersonAsync(patchRequest.Data.Id, null, patchRequest));
132+
await ApiResponse.TranslateAsync(() =>
133+
apiClient.PatchPersonAsync(updatePersonRequest.Data.Id, updatePersonRequest));
133134
134135
// The sent request looks like this:
135136
// {
@@ -171,6 +172,7 @@ Alternatively, the following section shows what to add to your client project fi
171172
<CodeGenerator>NSwagCSharp</CodeGenerator>
172173
<ClassName>ExampleApiClient</ClassName>
173174
<OutputPath>ExampleApiClient.cs</OutputPath>
175+
<Options>/GenerateExceptionClasses:false /GenerateNullableReferenceTypes:true /GenerateOptionalPropertiesAsNullable:true /GenerateOptionalParameters:true /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.NSwag</Options>
174176
</OpenApiReference>
175177
</ItemGroup>
176178
```
@@ -184,7 +186,7 @@ To generate your C# client, install the Kiota tool by following the steps at htt
184186
Next, generate client code by running the [command line tool](https://learn.microsoft.com/en-us/openapi/kiota/using#client-generation). For example:
185187

186188
```
187-
dotnet kiota generate --language CSharp --class-name ExampleApiClient --output ./GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --openapi ..\JsonApiDotNetCoreExample\GeneratedSwagger\JsonApiDotNetCoreExample.json
189+
dotnet kiota generate --language CSharp --class-name ExampleApiClient --output ./GeneratedCode --backing-store --exclude-backward-compatible --clean-output --clear-cache --openapi http://localhost:14140/swagger/v1/swagger.json
188190
```
189191

190192
> [!CAUTION]
@@ -216,7 +218,7 @@ For example, the following section puts the generated code in a namespace and ge
216218
</OpenApiReference>
217219
```
218220

219-
Likewise, you can enable nullable reference types by adding `/GenerateNullableReferenceTypes:true`, optionally combined with `/GenerateOptionalParameters:true`.
221+
Likewise, you can enable nullable reference types by adding `/GenerateNullableReferenceTypes:true /GenerateOptionalPropertiesAsNullable:true /GenerateOptionalParameters:true`.
220222

221223
# [Kiota](#tab/kiota)
222224

@@ -256,9 +258,9 @@ NSwag needs extra settings to make response headers accessible. Specify the foll
256258
This enables the following code, which is explained below:
257259

258260
```c#
259-
var getResponse = await ApiResponse.TranslateAsync(() => apiClient.GetPersonCollectionAsync(null, null));
261+
var getResponse = await ApiResponse.TranslateAsync(() => apiClient.GetPersonCollectionAsync());
260262
string eTag = getResponse.Headers["ETag"].Single();
261-
Console.WriteLine($"Retrieved {getResponse.Result.Data.Count} people.");
263+
Console.WriteLine($"Retrieved {getResponse.Result?.Data.Count ?? 0} people.");
262264

263265
// wait some time...
264266
@@ -295,3 +297,27 @@ Due to a [bug in Kiota](https://github.com/microsoft/kiota/issues/4190), a try/c
295297
For a full example, see the [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiKiotaClientExample).
296298

297299
---
300+
301+
## Atomic operations
302+
303+
# [NSwag](#tab/nswag)
304+
305+
[Atomic operations](~/usage/writing/bulk-batch-operations.md) are fully supported.
306+
The [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiNSwagClientExample)
307+
demonstrates how to use them. It uses local IDs to:
308+
- Create a new tag
309+
- Create a new person
310+
- Create a new todo-item, tagged with the new tag, and owned by the new person
311+
- Assign the todo-item to the created person
312+
313+
# [Kiota](#tab/kiota)
314+
315+
[Atomic operations](~/usage/writing/bulk-batch-operations.md) are fully supported.
316+
See the [example project](https://github.com/json-api-dotnet/JsonApiDotNetCore/tree/openapi/src/Examples/OpenApiKiotaClientExample)
317+
demonstrates how to use them. It uses local IDs to:
318+
- Create a new tag
319+
- Create a new person
320+
- Create a new todo-item, tagged with the new tag, and owned by the new person
321+
- Assign the todo-item to the created person
322+
323+
---

0 commit comments

Comments
 (0)