Skip to content

Commit bd778bc

Browse files
authored
[Docs] Improve file formatting (#3409)
Improve the formatting of the documentation.
1 parent 91b05c6 commit bd778bc

6 files changed

+356
-311
lines changed

docs/configure-and-customize-annotations.md

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
2. In the `ConfigureServices` method of `Startup.cs`, enable annotations within in the Swagger config block:
1313
1414
```csharp
15-
services.AddSwaggerGen(c =>
15+
services.AddSwaggerGen(options =>
1616
{
17-
...
18-
19-
c.EnableAnnotations();
17+
//...
18+
options.EnableAnnotations();
2019
});
2120
```
2221
@@ -26,7 +25,6 @@ Once annotations have been enabled, you can enrich the generated Operation metad
2625
2726
```csharp
2827
[HttpPost]
29-
3028
[SwaggerOperation(
3129
Summary = "Creates a new product",
3230
Description = "Requires admin privileges",
@@ -38,7 +36,7 @@ public IActionResult Create([FromBody]Product product)
3836

3937
## Enrich Response Metadata
4038

41-
ASP.NET Core provides the `ProducesResponseTypeAttribute` for listing the different responses that can be returned by an action. These attributes can be combined with XML comments, as described [above](#include-descriptions-from-xml-comments), to include human friendly descriptions with each response in the generated Swagger. If you'd prefer to do all of this with a single attribute, and avoid the use of XML comments, you can use `SwaggerResponseAttribute`s instead:
39+
ASP.NET Core provides the `ProducesResponseTypeAttribute` for listing the different responses that can be returned by an action. These attributes can be combined with XML comments, as described [here](configure-and-customize-swaggergen.md#include-descriptions-from-xml-comments), to include human friendly descriptions with each response in the generated Swagger. If you'd prefer to do all of this with a single attribute, and avoid the use of XML comments, you can use `SwaggerResponseAttribute`s instead:
4240

4341
```csharp
4442
[HttpPost]
@@ -86,21 +84,24 @@ public class Product
8684
}
8785
```
8886

89-
_NOTE: In Swagger / OpenAPI, serialized objects AND contained properties are represented as `Schema` instances, hence why this annotation can be applied to both classes and properties. Also worth noting, "required" properties are specified as an array of property names on the top-level schema as opposed to a flag on each individual property._
87+
> [!NOTE]
88+
> In Swagger / OpenAPI, serialized objects AND contained properties are represented as `Schema` instances, hence why this annotation can be applied to both classes and properties. Also worth noting, "required" properties are specified as an array of property names on the top-level schema as opposed to a flag on each individual property.
9089
9190
## Apply Schema Filters to Specific Types
9291

93-
The `SwaggerGen` package provides several extension points, including Schema Filters ([described here](#extend-generator-with-operation-schema--document-filter)) for customizing ALL generated Schemas. However, there may be cases where it's preferable to apply a filter to a specific Schema. For example, if you'd like to include an example for a specific type in your API. This can be done by decorating the type with a `SwaggerSchemaFilterAttribute`:
92+
The `SwaggerGen` package provides several extension points, including Schema Filters (described [here](configure-and-customize-swaggergen.md#extend-generator-with-operation-schema--document-filter)) for customizing **all** generated Schemas. However, there may be cases where it's preferable to apply a filter to a specific Schema. For example, if you'd like to include an example for a specific type in your API. This can be done by decorating the type with a `SwaggerSchemaFilterAttribute`:
9493

94+
📝 `Product.cs`
9595
```csharp
96-
// Product.cs
9796
[SwaggerSchemaFilter(typeof(ProductSchemaFilter))]
9897
public class Product
9998
{
100-
...
99+
//...
101100
}
101+
```
102102

103-
// ProductSchemaFilter.cs
103+
📝 `ProductSchemaFilter.cs`
104+
```csharp
104105
public class ProductSchemaFilter : ISchemaFilter
105106
{
106107
public void Apply(OpenApiSchema schema, SchemaFilterContext context)
@@ -122,25 +123,27 @@ By default, the Swagger generator will tag all operations with the controller na
122123
[SwaggerTag("Create, read, update and delete Products")]
123124
public class ProductsController
124125
{
125-
...
126+
//...
126127
}
127128
```
128129

129-
_NOTE: This will add the above description specifically to the tag named "Products". Therefore, you should avoid using this attribute if you're tagging Operations with something other than controller name - e.g. if you're customizing the tagging behavior with `TagActionsBy`._
130+
> [!NOTE]
131+
> This will add the above description specifically to the tag named "Products". Therefore, you should avoid using this attribute if you're tagging Operations with something other than controller name - e.g. if you're customizing the tagging behavior with `TagActionsBy`.
130132
131133
## List Known Subtypes for Inheritance and Polymorphism
132134

133-
If you want to use Swashbuckle's [inheritance and/or polymorphism behavior](#inheritance-and-polymorphism), you can use annotations to _explicitly_ indicate the "known" subtypes for a given base type. This will override the default selector function, which selects _all_ subtypes in the same assembly as the base type, and therefore needs to be explicitly enabled when you enable Annotations:
135+
If you want to use Swashbuckle's [inheritance and/or polymorphism behavior](configure-and-customize-swaggergen.md#inheritance-and-polymorphism), you can use annotations to _explicitly_ indicate the "known" subtypes for a given base type. This will override the default selector function, which selects _all_ subtypes in the same assembly as the base type, and therefore needs to be explicitly enabled when you enable Annotations:
134136

137+
📝 `Startup.cs`
135138
```csharp
136-
// Startup.cs
137-
services.AddSwaggerGen(c =>
139+
services.AddSwaggerGen(options =>
138140
{
139-
c.EnableAnnotations(enableAnnotationsForInheritance: true, enableAnnotationsForPolymorphism: true);
141+
options.EnableAnnotations(enableAnnotationsForInheritance: true, enableAnnotationsForPolymorphism: true);
140142
});
143+
```
141144

142-
// Shape.cs
143-
145+
📝 `Shape.cs`
146+
```csharp
144147
// .NET 7 or later
145148
[JsonDerivedType(typeof(Rectangle))]
146149
[JsonDerivedType(typeof(Circle))]
@@ -160,16 +163,16 @@ public abstract class Shape
160163

161164
If you're using annotations to _explicitly_ indicate the "known" subtypes for a polymorphic base type, you can combine the `JsonPolymorphicAttribute` with the `JsonDerivedTypeAttribute` to provide additional metadata about the "discriminator" property, which will then be incorporated into the generated schema definition:
162165

163-
166+
📝 `Startup.cs`
164167
```csharp
165-
// Startup.cs
166-
services.AddSwaggerGen(c =>
168+
services.AddSwaggerGen(options =>
167169
{
168-
c.EnableAnnotations(enableAnnotationsForInheritance: true, enableAnnotationsForPolymorphism: true);
170+
options.EnableAnnotations(enableAnnotationsForInheritance: true, enableAnnotationsForPolymorphism: true);
169171
});
172+
```
170173

171-
// Shape.cs
172-
174+
📝 `Shape.cs`
175+
```csharp
173176
// .NET 7 or later
174177
[JsonPolymorphic(TypeDiscriminatorPropertyName = "shapeType")]
175178
[JsonDerivedType(typeof(Rectangle), "rectangle")]
@@ -200,7 +203,7 @@ public enum ShapeType
200203

201204
This indicates that the corresponding payload will have a "shapeType" property to discriminate between subtypes, and that property will have a value of "rectangle" if the payload represents a `Rectangle` type and a value of "circle" if it represents a `Circle` type. This detail will be described in the generated schema definition as follows:
202205

203-
```
206+
```yaml
204207
schema: {
205208
oneOf: [
206209
{

docs/configure-and-customize-cli.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
## Retrieve Swagger Directly from a Startup Assembly
44

5-
Once your application has been setup with Swashbuckle (see [Getting Started](#getting-started)), you can use the Swashbuckle CLI tool to retrieve Swagger / OpenAPI JSON directly from your application's startup assembly, and write it to file. This can be useful if you want to incorporate Swagger generation into a CI/CD process, or if you want to serve it from static file at run-time.
5+
Once your application has been setup with Swashbuckle (see [Getting Started](../README.md#getting-started)), you can use the Swashbuckle CLI tool to retrieve Swagger / OpenAPI JSON directly from your application's startup assembly, and write it to file. This can be useful if you want to incorporate Swagger generation into a CI/CD process, or if you want to serve it from static file at run-time.
66

77
It's packaged as a [.NET Tool](https://learn.microsoft.com/dotnet/core/tools/global-tools) that can be installed and used via the dotnet SDK.
88

9-
> :warning: The tool needs to load your Startup DLL and its dependencies at runtime. Therefore, you should use a version of the `dotnet` SDK that is compatible with your application. For example, if your app targets `net8.0`, then you should use version 8.0.xxx of the SDK to run the CLI tool. If it targets `net9.0`, then you should use version 9.0.xxx of the SDK and so on.
9+
> [!WARNING]
10+
> The tool needs to load your Startup DLL and its dependencies at runtime. Therefore, you should use a version of the `dotnet` SDK that is compatible with your application. For example, if your app targets `net8.0`, then you should use version 8.0.xxx of the SDK to run the CLI tool. If it targets `net9.0`, then you should use version 9.0.xxx of the SDK and so on.
1011
1112
### Using the tool with the .NET SDK
1213

@@ -28,10 +29,10 @@ It's packaged as a [.NET Tool](https://learn.microsoft.com/dotnet/core/tools/glo
2829
swagger tofile --output [output] [startupassembly] [swaggerdoc]
2930
```
3031
31-
Where ...
32-
* [output] is the relative path where the Swagger JSON will be output to
33-
* [startupassembly] is the relative path to your application's startup assembly
34-
* [swaggerdoc] is the name of the swagger document you want to retrieve, as configured in your startup class
32+
Placeholders and their meaning:
33+
* `[output]`: the relative path where the Swagger JSON will be output to
34+
* `[startupassembly]`: the relative path to your application's startup assembly
35+
* `[swaggerdoc]`: the name of the swagger document you want to retrieve, as configured in your startup class
3536
3637
### Using the tool with the .NET 6.0 SDK or later
3738
@@ -59,10 +60,10 @@ It's packaged as a [.NET Tool](https://learn.microsoft.com/dotnet/core/tools/glo
5960
dotnet swagger tofile --output [output] [startupassembly] [swaggerdoc]
6061
```
6162
62-
Where ...
63-
* [output] is the relative path where the Swagger JSON will be output to
64-
* [startupassembly] is the relative path to your application's startup assembly
65-
* [swaggerdoc] is the name of the swagger document you want to retrieve, as configured in your startup class
63+
Placeholders and their meaning:
64+
* `[output]`: the relative path where the Swagger JSON will be output to
65+
* `[startupassembly]`: the relative path to your application's startup assembly
66+
* `[swaggerdoc]`: the name of the swagger document you want to retrieve, as configured in your startup class
6667
6768
## Use the CLI Tool with a Custom Host Configuration
6869

docs/configure-and-customize-redoc.md

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,88 @@
22

33
## Change Relative Path to the UI
44

5-
By default, the Redoc UI will be exposed at "/api-docs". If necessary, you can alter this when enabling the Redoc middleware:
5+
By default, the Redoc UI will be exposed at `"/api-docs"`. If necessary, you can alter this when enabling the Redoc middleware:
66

77
```csharp
8-
app.UseReDoc(c =>
8+
app.UseReDoc(options =>
99
{
10-
c.RoutePrefix = "docs"
11-
...
12-
}
10+
options.RoutePrefix = "docs"
11+
//...
12+
});
1313
```
1414

1515
## Change Document Title
1616

1717
By default, the Redoc UI will have a generic document title. You can alter this when enabling the Redoc middleware:
1818

1919
```csharp
20-
app.UseReDoc(c =>
20+
app.UseReDoc(options =>
2121
{
22-
c.DocumentTitle = "My API Docs";
23-
...
24-
}
22+
options.DocumentTitle = "My API Docs";
23+
//...
24+
});
2525
```
2626

2727
## Apply Redoc Parameters
2828

29-
Redoc ships with its own set of configuration parameters, all described here https://github.com/Rebilly/redoc/blob/main/README.md#redoc-options-object. In Swashbuckle, most of these are surfaced through the Redoc middleware options:
29+
Redoc ships with its own set of configuration parameters, all described [here](https://github.com/Rebilly/redoc/blob/main/README.md#redoc-options-object). In Swashbuckle, most of these are surfaced through the Redoc middleware options:
3030

3131
```csharp
32-
app.UseReDoc(c =>
32+
app.UseReDoc(options =>
3333
{
34-
c.SpecUrl("/v1/swagger.json");
35-
c.EnableUntrustedSpec();
36-
c.ScrollYOffset(10);
37-
c.HideHostname();
38-
c.HideDownloadButton();
39-
c.ExpandResponses("200,201");
40-
c.RequiredPropsFirst();
41-
c.NoAutoAuth();
42-
c.PathInMiddlePanel();
43-
c.HideLoading();
44-
c.NativeScrollbars();
45-
c.DisableSearch();
46-
c.OnlyRequiredInSamples();
47-
c.SortPropsAlphabetically();
34+
options.SpecUrl("/v1/swagger.json");
35+
options.EnableUntrustedSpec();
36+
options.ScrollYOffset(10);
37+
options.HideHostname();
38+
options.HideDownloadButton();
39+
options.ExpandResponses("200,201");
40+
options.RequiredPropsFirst();
41+
options.NoAutoAuth();
42+
options.PathInMiddlePanel();
43+
options.HideLoading();
44+
options.NativeScrollbars();
45+
options.DisableSearch();
46+
options.OnlyRequiredInSamples();
47+
options.SortPropsAlphabetically();
4848
});
4949
```
5050

51-
_Using `c.SpecUrl("/v1/swagger.json")` multiple times within the same `UseReDoc(...)` will not add multiple urls._
51+
> [!NOTE]
52+
> Using `options.SpecUrl("/v1/swagger.json")` multiple times within the same `UseReDoc(...)` will not add multiple URL.
5253
5354
## Inject Custom CSS
5455

5556
To tweak the look and feel, you can inject additional CSS stylesheets by adding them to your `wwwroot` folder and specifying the relative paths in the middleware options:
5657

5758
```csharp
58-
app.UseReDoc(c =>
59+
app.UseReDoc(options =>
5960
{
60-
...
61-
c.InjectStylesheet("/redoc/custom.css");
62-
}
61+
//...
62+
options.InjectStylesheet("/redoc/custom.css");
63+
});
6364
```
6465

65-
It is also possible to modify the theme by using the `AdditionalItems` property, see https://github.com/Rebilly/redoc/blob/main/README.md#redoc-options-object for more information.
66+
It is also possible to modify the theme by using the `AdditionalItems` property, more information can be found [here](https://github.com/Rebilly/redoc/blob/main/README.md#redoc-options-object).
6667

6768
```csharp
68-
app.UseReDoc(c =>
69+
app.UseReDoc(options =>
6970
{
70-
...
71-
c.ConfigObject.AdditionalItems = ...
72-
}
71+
//...
72+
options.ConfigObject.AdditionalItems = ...
73+
});
7374
```
7475

7576
## Customize index.html
7677

77-
To customize the UI beyond the basic options listed above, you can provide your own version of the Redoc index.html page:
78+
To customize the UI beyond the basic options listed above, you can provide your own version of the Redoc `index.html` page:
7879

7980
```csharp
80-
app.UseReDoc(c =>
81+
app.UseReDoc(options =>
8182
{
82-
c.IndexStream = () => GetType().Assembly
83+
options.IndexStream = () => GetType().Assembly
8384
.GetManifestResourceStream("CustomIndex.ReDoc.index.html"); // requires file to be added as an embedded resource
8485
});
8586
```
8687

87-
_To get started, you should base your custom index.html on the [default version](src/Swashbuckle.AspNetCore.ReDoc/index.html)_
88+
> [!TIP]
89+
> To get started, you should base your custom `index.html` on the [default version](../src/Swashbuckle.AspNetCore.ReDoc/index.html).

0 commit comments

Comments
 (0)