Skip to content

Commit 088fde5

Browse files
authored
OpenAPI: Auto-generated documentation (#1371)
* Generate documentation for JSON:API endpoints * Generate documentation for JSON:API resource types and attributes * Rename path property, which is actually a directory * Add 403 status when client-generated IDs are forbidden (default) * Update documentation
1 parent 86af528 commit 088fde5

File tree

38 files changed

+4217
-1181
lines changed

38 files changed

+4217
-1181
lines changed

docs/usage/openapi.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# OpenAPI
22

3-
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).
3+
JsonApiDotNetCore provides an extension package that enables you to produce an [OpenAPI specification](https://swagger.io/specification/) for your JSON:API endpoints.
4+
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.
5+
The package provides an integration with [Swashbuckle](https://github.com/domaindrivendev/Swashbuckle.AspNetCore).
46

57

68
## Getting started
@@ -35,10 +37,29 @@ By default, the OpenAPI specification will be available at `http://localhost:<po
3537
3638
## Documentation
3739
38-
Swashbuckle also ships with [SwaggerUI](https://swagger.io/tools/swagger-ui/), which enables to visualize and interact with the API endpoints through a web page. This can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Program.cs` file:
40+
### SwaggerUI
41+
42+
Swashbuckle also ships with [SwaggerUI](https://swagger.io/tools/swagger-ui/), which enables to visualize and interact with the API endpoints through a web page.
43+
This can be enabled by installing the `Swashbuckle.AspNetCore.SwaggerUI` NuGet package and adding the following to your `Program.cs` file:
3944
4045
```c#
4146
app.UseSwaggerUI();
4247
```
4348

4449
By default, SwaggerUI will be available at `http://localhost:<port>/swagger`.
50+
51+
### Triple-slash comments
52+
53+
Documentation for JSON:API endpoints is provided out of the box, which shows in SwaggerUI and through IDE IntelliSense in auto-generated clients.
54+
To also get documentation for your resource classes and their properties, add the following to your project file.
55+
The `NoWarn` line is optional, which suppresses build warnings for undocumented types and members.
56+
57+
```xml
58+
<PropertyGroup>
59+
<GenerateDocumentationFile>True</GenerateDocumentationFile>
60+
<NoWarn>$(NoWarn);1591</NoWarn>
61+
</PropertyGroup>
62+
```
63+
64+
You can combine this with the documentation that Swagger itself supports, by enabling it as described [here](https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments).
65+
This adds documentation for additional types, such as triple-slash comments on enums used in your resource models.

src/JsonApiDotNetCore.OpenApi/JsonApiOperationIdSelector.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ private string ApplyTemplate(string operationIdTemplate, ResourceType resourceTy
116116
string relationshipName = operationIdTemplate.Contains("[RelationshipName]") ? endpoint.RelativePath.Split("/").Last() : string.Empty;
117117

118118
// @formatter:wrap_chained_method_calls chop_always
119-
// @formatter:wrap_before_first_method_call true true
119+
// @formatter:wrap_before_first_method_call true
120120

121121
string pascalCaseOperationId = operationIdTemplate
122122
.Replace("[Method]", method)

src/JsonApiDotNetCore.OpenApi/ServiceCollectionExtensions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection
7070
SetOperationInfo(swaggerGenOptions, controllerResourceMapping, namingPolicy);
7171
SetSchemaIdSelector(swaggerGenOptions, resourceGraph, namingPolicy);
7272
swaggerGenOptions.DocumentFilter<EndpointOrderingFilter>();
73+
swaggerGenOptions.OperationFilter<JsonApiOperationDocumentationFilter>();
7374

7475
setupSwaggerGenAction?.Invoke(swaggerGenOptions);
7576
});

src/JsonApiDotNetCore.OpenApi/SwaggerComponents/EndpointOrderingFilter.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
1616
ArgumentGuard.NotNull(swaggerDoc);
1717
ArgumentGuard.NotNull(context);
1818

19-
List<KeyValuePair<string, OpenApiPathItem>> orderedEndpoints = swaggerDoc.Paths.OrderBy(GetPrimaryResourcePublicName)
19+
List<KeyValuePair<string, OpenApiPathItem>> endpointsInOrder = swaggerDoc.Paths.OrderBy(GetPrimaryResourcePublicName)
2020
.ThenBy(GetRelationshipName).ThenBy(IsSecondaryEndpoint).ToList();
2121

2222
swaggerDoc.Paths.Clear();
2323

24-
foreach ((string url, OpenApiPathItem path) in orderedEndpoints)
24+
foreach ((string url, OpenApiPathItem path) in endpointsInOrder)
2525
{
2626
swaggerDoc.Paths.Add(url, path);
2727
}

0 commit comments

Comments
 (0)