Skip to content

Commit 6c9aff8

Browse files
authored
Merge pull request #1448 from json-api-dotnet/openapi-includes
OpenAPI: Support including related resources
2 parents 37f41fe + 97bd1bc commit 6c9aff8

File tree

99 files changed

+8976
-3356
lines changed

Some content is hidden

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

99 files changed

+8976
-3356
lines changed

.gitattributes

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# When running OpenAPI tests, these committed files are downloaded and written to disk (so we'll know when something changes).
2-
# On Windows, these text files are auto-converted to crlf on fetch, while the written downloaded files use lf line endings.
2+
# On Windows, these text files are auto-converted to crlf on git fetch, while the written downloaded files use lf line endings.
33
# Therefore, running the tests on Windows creates local changes. Staging them auto-converts back to crlf, which undoes the changes.
4-
# To avoid this annoyance, the next line opts out of the auto-conversion and forces to lf.
4+
# To avoid this annoyance, the next lines opt out of the auto-conversion and force to lf.
55
swagger.g.json text eol=lf
6+
**/GeneratedSwagger/*.json text eol=lf

JsonApiDotNetCore.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ JsonApiDotNetCore.ArgumentGuard.NotNull($EXPR$);</s:String>
1515
<s:Int64 x:Key="/Default/CodeEditing/NullCheckPatterns/PatternTypeNamesToPriority/=JetBrains_002EReSharper_002EFeature_002EServices_002ECSharp_002ENullChecking_002ETraceAssertPattern/@EntryIndexedValue">50</s:Int64>
1616
<s:Boolean x:Key="/Default/CodeInspection/CodeAnnotations/PropagateAnnotations/@EntryValue">False</s:Boolean>
1717
<s:Boolean x:Key="/Default/CodeInspection/ExcludedFiles/FileMasksToSkip/=swagger_002Ejson/@EntryIndexedValue">True</s:Boolean>
18+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/GeneratedFilesAndFolders/=71287D6F_002D6C3B_002D44B4_002D9FCA_002DE78FE3F02289_002Ff_003ASchemaGenerator_002Ecs/@EntryIndexedValue">71287D6F-6C3B-44B4-9FCA-E78FE3F02289/f:SchemaGenerator.cs</s:String>
1819
<s:String x:Key="/Default/CodeInspection/GeneratedCode/GeneratedFileMasks/=swagger_002Eg_002Ejson/@EntryIndexedValue">swagger.g.json</s:String>
1920
<s:String x:Key="/Default/CodeInspection/GeneratedCode/GeneratedFileMasks/=swagger_002Ejson/@EntryIndexedValue">swagger.json</s:String>
2021
<s:String x:Key="/Default/CodeInspection/Highlighting/AnalysisEnabled/@EntryValue">SOLUTION</s:String>

docs/usage/openapi-client.md

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
You can generate a JSON:API client in various programming languages from the [OpenAPI specification](https://swagger.io/specification/) file that JsonApiDotNetCore APIs provide.
44

55
For C# .NET clients generated using [NSwag](https://github.com/RicoSuter/NSwag), we provide an additional package
6-
that introduces support for partial PATCH/POST requests. The concern here is that a property on a generated C# class
7-
being `null` could mean "set the value to `null` in the request" or "this is `null` because I never touched it".
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".
89

910
## Getting started
1011

@@ -26,29 +27,19 @@ The next steps describe how to generate a JSON:API client library and use our pa
2627
2728
3. Although not strictly required, we recommend to run package update now, which fixes some issues.
2829

29-
4. Add code that calls one of your JSON:API endpoints.
30+
> [!WARNING]
31+
> NSwag v14 is currently *incompatible* with JsonApiDotNetCore (tracked [here](https://github.com/RicoSuter/NSwag/issues/4662)). Stick with v13.x for the moment.
3032
31-
```c#
32-
using var httpClient = new HttpClient();
33-
var apiClient = new ExampleApiClient("http://localhost:14140", httpClient);
34-
35-
PersonCollectionResponseDocument getResponse = await apiClient.GetPersonCollectionAsync(new Dictionary<string, string?>
36-
{
37-
["filter"] = "has(assignedTodoItems)",
38-
["sort"] = "-lastName",
39-
["page[size]"] = "5"
40-
});
33+
4. Add our client package to your project:
4134

42-
foreach (PersonDataInResponse person in getResponse.Data)
43-
{
44-
Console.WriteLine($"Found person {person.Id}: {person.Attributes.DisplayName}");
45-
}
35+
```
36+
dotnet add package JsonApiDotNetCore.OpenApi.Client
4637
```
4738
48-
5. Add our client package to your project:
39+
5. Add the next line inside the **OpenApiReference** section in your project file:
4940
50-
```
51-
dotnet add package JsonApiDotNetCore.OpenApi.Client
41+
```xml
42+
<Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions</Options>
5243
```
5344
5445
6. Add the following glue code to connect our package with your generated code.
@@ -73,8 +64,28 @@ The next steps describe how to generate a JSON:API client library and use our pa
7364
7465
> [!TIP]
7566
> The project at src/Examples/JsonApiDotNetCoreExampleClient 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+
7. Add code that calls one of your JSON:API endpoints.
70+
71+
```c#
72+
using var httpClient = new HttpClient();
73+
var apiClient = new ExampleApiClient(httpClient);
74+
75+
var getResponse = await apiClient.GetPersonCollectionAsync(new Dictionary<string, string?>
76+
{
77+
["filter"] = "has(assignedTodoItems)",
78+
["sort"] = "-lastName",
79+
["page[size]"] = "5"
80+
});
81+
82+
foreach (var person in getResponse.Data)
83+
{
84+
Console.WriteLine($"Found person {person.Id}: {person.Attributes.DisplayName}");
85+
}
86+
```
7687
77-
7. Extend your demo code to send a partial PATCH request with the help of our package:
88+
8. Extend your demo code to send a partial PATCH request with the help of our package:
7889
7990
```c#
8091
var patchRequest = new PersonPatchRequestDocument
@@ -94,7 +105,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
94105
person => person.FirstName))
95106
{
96107
// Workaround for https://github.com/RicoSuter/NSwag/issues/2499.
97-
await TranslateAsync(async () => await apiClient.PatchPersonAsync(patchRequest.Data.Id, null, patchRequest));
108+
await ApiResponse.TranslateAsync(() => apiClient.PatchPersonAsync(patchRequest.Data.Id, null, patchRequest));
98109
99110
// The sent request looks like this:
100111
// {
@@ -145,13 +156,13 @@ From here, continue from step 3 in the list of steps for Visual Studio.
145156
The `OpenApiReference` element in the project file accepts an `Options` element to pass additional settings to the client generator,
146157
which are listed [here](https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs).
147158

148-
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):
159+
For example, the next section puts the generated code in a namespace and generates an interface (which is handy for dependency injection):
149160

150161
```xml
151162
<OpenApiReference Include="swagger.json">
152163
<Namespace>ExampleProject.GeneratedCode</Namespace>
153164
<ClassName>SalesApiClient</ClassName>
154165
<CodeGenerator>NSwagCSharp</CodeGenerator>
155-
<Options>/UseBaseUrl:false /GenerateClientInterfaces:true</Options>
166+
<Options>/GenerateClientInterfaces:true</Options>
156167
</OpenApiReference>
157168
```

package-versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<SwashbuckleFrozenVersion>6.5.0</SwashbuckleFrozenVersion>
88

99
<!-- Non-published dependencies (these are safe to update, won't cause a breaking change) -->
10+
<ApiDescriptionServerVersion>8.0.*</ApiDescriptionServerVersion>
1011
<BenchmarkDotNetVersion>0.13.*</BenchmarkDotNetVersion>
1112
<BlushingPenguinVersion>1.0.*</BlushingPenguinVersion>
1213
<BogusVersion>35.2.*</BogusVersion>

0 commit comments

Comments
 (0)