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: Auto-generated documentation #1371

Merged
merged 5 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 23 additions & 2 deletions docs/usage/openapi.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# OpenAPI

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).
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).


## Getting started
Expand Down Expand Up @@ -35,10 +37,29 @@ By default, the OpenAPI specification will be available at `http://localhost:<po

## Documentation

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:
### SwaggerUI

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:

```c#
app.UseSwaggerUI();
```

By default, SwaggerUI will be available at `http://localhost:<port>/swagger`.

### Triple-slash comments

Documentation for JSON:API endpoints is provided out of the box, which shows in SwaggerUI and through IDE IntelliSense in auto-generated clients.
To also get documentation for your resource classes and their properties, add the following to your project file.
The `NoWarn` line is optional, which suppresses build warnings for undocumented types and members.

```xml
<PropertyGroup>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
```

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).
This adds documentation for additional types, such as triple-slash comments on enums used in your resource models.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private string ApplyTemplate(string operationIdTemplate, ResourceType resourceTy
string relationshipName = operationIdTemplate.Contains("[RelationshipName]") ? endpoint.RelativePath.Split("/").Last() : string.Empty;

// @formatter:wrap_chained_method_calls chop_always
// @formatter:wrap_before_first_method_call true true
// @formatter:wrap_before_first_method_call true

string pascalCaseOperationId = operationIdTemplate
.Replace("[Method]", method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection
SetOperationInfo(swaggerGenOptions, controllerResourceMapping, namingPolicy);
SetSchemaIdSelector(swaggerGenOptions, resourceGraph, namingPolicy);
swaggerGenOptions.DocumentFilter<EndpointOrderingFilter>();
swaggerGenOptions.OperationFilter<JsonApiOperationDocumentationFilter>();

setupSwaggerGenAction?.Invoke(swaggerGenOptions);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
ArgumentGuard.NotNull(swaggerDoc);
ArgumentGuard.NotNull(context);

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

swaggerDoc.Paths.Clear();

foreach ((string url, OpenApiPathItem path) in orderedEndpoints)
foreach ((string url, OpenApiPathItem path) in endpointsInOrder)
{
swaggerDoc.Paths.Add(url, path);
}
Expand Down
Loading