You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/configure-and-customize-annotations.md
+29-26Lines changed: 29 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,11 +12,10 @@
12
12
2. In the `ConfigureServices` method of `Startup.cs`, enable annotations within in the Swagger config block:
13
13
14
14
```csharp
15
-
services.AddSwaggerGen(c =>
15
+
services.AddSwaggerGen(options =>
16
16
{
17
-
...
18
-
19
-
c.EnableAnnotations();
17
+
//...
18
+
options.EnableAnnotations();
20
19
});
21
20
```
22
21
@@ -26,7 +25,6 @@ Once annotations have been enabled, you can enrich the generated Operation metad
26
25
27
26
```csharp
28
27
[HttpPost]
29
-
30
28
[SwaggerOperation(
31
29
Summary = "Creates a new product",
32
30
Description = "Requires admin privileges",
@@ -38,7 +36,7 @@ public IActionResult Create([FromBody]Product product)
38
36
39
37
## Enrich Response Metadata
40
38
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:
42
40
43
41
```csharp
44
42
[HttpPost]
@@ -86,21 +84,24 @@ public class Product
86
84
}
87
85
```
88
86
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.
90
89
91
90
## Apply Schema Filters to Specific Types
92
91
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`:
@@ -122,25 +123,27 @@ By default, the Swagger generator will tag all operations with the controller na
122
123
[SwaggerTag("Create, read, update and delete Products")]
123
124
publicclassProductsController
124
125
{
125
-
...
126
+
//...
126
127
}
127
128
```
128
129
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`.
130
132
131
133
## List Known Subtypes for Inheritance and Polymorphism
132
134
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:
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:
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:
Copy file name to clipboardExpand all lines: docs/configure-and-customize-cli.md
+11-10Lines changed: 11 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,11 +2,12 @@
2
2
3
3
## Retrieve Swagger Directly from a Startup Assembly
4
4
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.
6
6
7
7
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.
8
8
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.
10
11
11
12
### Using the tool with the .NET SDK
12
13
@@ -28,10 +29,10 @@ It's packaged as a [.NET Tool](https://learn.microsoft.com/dotnet/core/tools/glo
Copy file name to clipboardExpand all lines: docs/configure-and-customize-redoc.md
+41-39Lines changed: 41 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,86 +2,88 @@
2
2
3
3
## Change Relative Path to the UI
4
4
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:
6
6
7
7
```csharp
8
-
app.UseReDoc(c=>
8
+
app.UseReDoc(options=>
9
9
{
10
-
c.RoutePrefix="docs"
11
-
...
12
-
}
10
+
options.RoutePrefix="docs"
11
+
//...
12
+
});
13
13
```
14
14
15
15
## Change Document Title
16
16
17
17
By default, the Redoc UI will have a generic document title. You can alter this when enabling the Redoc middleware:
18
18
19
19
```csharp
20
-
app.UseReDoc(c=>
20
+
app.UseReDoc(options=>
21
21
{
22
-
c.DocumentTitle="My API Docs";
23
-
...
24
-
}
22
+
options.DocumentTitle="My API Docs";
23
+
//...
24
+
});
25
25
```
26
26
27
27
## Apply Redoc Parameters
28
28
29
-
Redoc ships with its own set of configuration parameters, alldescribedherehttps://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:
> Using `options.SpecUrl("/v1/swagger.json")` multiple times within the same `UseReDoc(...)` will not add multiple URL.
52
53
53
54
## Inject Custom CSS
54
55
55
56
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:
56
57
57
58
```csharp
58
-
app.UseReDoc(c=>
59
+
app.UseReDoc(options=>
59
60
{
60
-
...
61
-
c.InjectStylesheet("/redoc/custom.css");
62
-
}
61
+
//...
62
+
options.InjectStylesheet("/redoc/custom.css");
63
+
});
63
64
```
64
65
65
-
It is also possible to modify the theme by using the `AdditionalItems` property, seehttps://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).
66
67
67
68
```csharp
68
-
app.UseReDoc(c=>
69
+
app.UseReDoc(options=>
69
70
{
70
-
...
71
-
c.ConfigObject.AdditionalItems=...
72
-
}
71
+
//...
72
+
options.ConfigObject.AdditionalItems=...
73
+
});
73
74
```
74
75
75
76
## Customize index.html
76
77
77
-
To customize the UI beyond the basic options listed above, youcanprovideyourownversionoftheRedocindex.htmlpage:
78
+
To customize the UI beyond the basic options listed above, you can provide your own version of the Redoc `index.html` page:
78
79
79
80
```csharp
80
-
app.UseReDoc(c=>
81
+
app.UseReDoc(options=>
81
82
{
82
-
c.IndexStream= () =>GetType().Assembly
83
+
options.IndexStream= () =>GetType().Assembly
83
84
.GetManifestResourceStream("CustomIndex.ReDoc.index.html"); // requires file to be added as an embedded resource
0 commit comments