Skip to content
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

openapi: include headers #1459

Merged
merged 36 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
cda34c7
openapi: include ETag/Location headers
verdie-g Feb 7, 2024
60e31b6
Update all open api files
verdie-g Feb 8, 2024
baa8677
Make response headers required
verdie-g Feb 8, 2024
07d4477
Fix coding style
verdie-g Feb 8, 2024
06a4d79
Fix test
verdie-g Feb 8, 2024
36d5ff7
Add schema to headers
verdie-g Feb 8, 2024
0a6c833
Fix test
verdie-g Feb 8, 2024
31b3b3a
Use WrapResponses option for NSwag
verdie-g Feb 8, 2024
f371aa5
Document 304 + If-None-Match
verdie-g Feb 8, 2024
4992baa
Stuff
verdie-g Feb 8, 2024
61dd786
Add content length header
verdie-g Feb 12, 2024
e34b303
Idk anymore
verdie-g Feb 14, 2024
056cc82
Address PR comments
verdie-g Feb 17, 2024
ad8ef94
Remove dead code
verdie-g Feb 17, 2024
f66b8ed
Use primary constructor
verdie-g Feb 17, 2024
69930d1
Address easy PR comments
verdie-g Feb 18, 2024
8c265c7
Rewrite e2e tests
verdie-g Feb 18, 2024
456a78c
Address PR comments (1/2) - will not compile
verdie-g Feb 19, 2024
d4ad3a6
Address PR comments (2/2)
verdie-g Feb 19, 2024
d5b7a40
Add HeaderTests
verdie-g Feb 21, 2024
1a8cc52
Fix inspection
verdie-g Feb 21, 2024
09338e1
Address PR comments
verdie-g Feb 21, 2024
0a6f86f
Fix inspection
verdie-g Feb 21, 2024
1b73bfb
Update src/JsonApiDotNetCore.OpenApi/SwaggerComponents/JsonApiOperati…
verdie-g Feb 23, 2024
4d805f1
Update src/JsonApiDotNetCore.OpenApi.Client/ApiResponse.cs
verdie-g Feb 23, 2024
8e21d98
Update test/OpenApiEndToEndTests/Headers/ETagTests.cs
verdie-g Feb 23, 2024
ef7f78b
Update test/OpenApiEndToEndTests/Headers/ETagTests.cs
verdie-g Feb 23, 2024
e083378
Update test/OpenApiTests/Headers/HeaderTests.cs
verdie-g Feb 23, 2024
e9899d9
Update test/OpenApiEndToEndTests/Headers/ETagTests.cs
verdie-g Feb 23, 2024
1d43181
Update test/OpenApiEndToEndTests/Headers/ETagTests.cs
verdie-g Feb 23, 2024
6611efb
Update test/OpenApiEndToEndTests/Headers/ETagTests.cs
verdie-g Feb 23, 2024
f8e8912
Address PR comments
verdie-g Feb 23, 2024
c22b8d1
Fix stuff
verdie-g Feb 23, 2024
22fa001
Suggestion to update OpenAPI docs for headers usage
bkoelman Feb 23, 2024
0eae574
Address PR comments
verdie-g Feb 23, 2024
a9f15f5
Add missing line break
bkoelman Feb 23, 2024
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
35 changes: 33 additions & 2 deletions docs/usage/openapi-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
5. Add the next line inside the **OpenApiReference** section in your project file:

```xml
<Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client.Exceptions</Options>
<Options>/GenerateExceptionClasses:false /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client</Options>
```

6. Add the following glue code to connect our package with your generated code.
Expand Down Expand Up @@ -77,7 +77,7 @@ The next steps describe how to generate a JSON:API client library and use our pa
["filter"] = "has(assignedTodoItems)",
["sort"] = "-lastName",
["page[size]"] = "5"
});
}, null);

foreach (var person in getResponse.Data)
{
Expand Down Expand Up @@ -166,3 +166,34 @@ For example, the next section puts the generated code in a namespace and generat
<Options>/GenerateClientInterfaces:true</Options>
</OpenApiReference>
```

## Headers and caching

To use [ETags for caching](~/usage/caching.md), NSwag needs extra settings to make response headers accessible.
Specify the following in the `<Options>` element of your project file:

```
/GenerateExceptionClasses:false /WrapResponses:true /GenerateResponseClasses:false /ResponseClass:ApiResponse /AdditionalNamespaceUsages:JsonApiDotNetCore.OpenApi.Client
```

This enables the following code, which is explained below:

```c#
var getResponse = await ApiResponse.TranslateAsync(() => apiClient.GetPersonCollectionAsync(null, null));
string eTag = getResponse.Headers[HeaderNames.ETag].Single();
Console.WriteLine($"Retrieved {getResponse.Result.Data.Count} people.");

// wait some time...

getResponse = await ApiResponse.TranslateAsync(() => apiClient.GetPersonCollectionAsync(null, eTag));

if (getResponse is { StatusCode: (int)HttpStatusCode.NotModified, Result: null })
{
Console.WriteLine("The HTTP response hasn't changed, so no response body was returned.");
}
```

The response of the first API call contains both data and an ETag header, which is a fingerprint of the response.
That ETag gets passed to the second API call. This enables the server to detect if something changed, which optimizes
network usage: no data is sent back, unless is has changed.
If you only want to ask whether data has changed without fetching it, use a HEAD request instead.
Loading
Loading