In OpenAPI, operations may be assigned an operationId. This ID must be unique among all operations described in the API.
Tools and libraries (e.g. client generators) may use the operationId to uniquely identify an operation, therefore, it is
recommended to follow common programming naming conventions.
Auto-generating an ID that matches these requirements, while also providing a name that would be meaningful in client libraries,
is a non-trivial task and thus Swashbuckle.AspNetCore omits the operationId by default. However, if necessary, you can assign
operationIds by decorating individual routes or by providing a custom strategy.
// operationId = "GetProductById"
[HttpGet("{id}", Name = "GetProductById")]
public IActionResult Get(int id)
{
// ...
return Ok();
}📝 Startup.cs
services.AddSwaggerGen(options =>
{
// Other configuration...
// Use method name as operationId
options.CustomOperationIds(apiDescription =>
{
return apiDescription.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null;
});
});📝 ProductsController.cs
// operationId = "GetProductById"
[HttpGet("/product/{id}")]
public IActionResult GetProductById(int id)
{
// ...
return Ok();
}Note
With either approach, API authors are responsible for ensuring the uniqueness of operationIds across all operations.
By default, Swashbuckle.AspNetCore will generate an HTTP "200" response for each operation. If the endpoint returns a
response object, then this will be used to generate a schema for the response body. For example:
[HttpPost("{id}")]
public Product GetById(int id)
{
// ...
return new Product();
}This endpoint will produce the following response metadata:
responses: {
200: {
description: "OK",
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/Product"
}
}
}
}
}If you need to specify a different status code and/or additional responses, or your MVC actions return IActionResult instead
of a response object, you can explicitly describe responses with [ProducesResponseType] which is part of ASP.NET Core. For example:
[HttpPost("product/{id}")]
[ProducesResponseType(typeof(Product), 200)]
[ProducesResponseType(typeof(IDictionary<string, string>), 400)]
[ProducesResponseType(500)]
public IActionResult GetProductInfoById(int id)
{
// ...
return Ok();
}This endpoint will produce the following response metadata:
responses: {
200: {
description: "OK",
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/Product"
}
}
}
},
400: {
description: "Bad Request",
content: {
"application/json": {
schema: {
type: "object",
additionalProperties: {
type: "string"
}
}
}
}
},
500: {
description: "Internal Server Error",
content: {}
}
}In an OpenAPI document, you can flag parameters and schema properties that are required for a request. If a parameter
(top-level or property-based) is decorated with [BindRequired] or [Required], then Swashbuckle.AspNetCore will automatically
flag it as a required parameter in the generated OpenAPI document:
📝 ProductsController.cs
public IActionResult Search([FromQuery, BindRequired] string keywords, [FromQuery] PagingOptions paging)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// ...
return Ok();
}📝 PagingOptions.cs
public class PagingOptions
{
[Required]
public int PageNumber { get; set; }
public int PageSize { get; set; }
}In addition to parameters, Swashbuckle.AspNetCore will also honor [Required] when used in a model that's bound to the request body.
In this case, the decorated properties will be flagged as required properties in the body description:
📝 ProductsController.cs
public IActionResult CreateNewProduct([FromBody] NewProduct product)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// ...
return Created();
}📝 NewProduct.cs
public class NewProduct
{
[Required]
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}This MVC controller will accept two form field values and one named file upload from the same form:
[HttpPost]
public void UploadFile([FromForm] string description, [FromForm] DateTime clientDate, IFormFile file)
{
// ...
}Important
As per the ASP.NET Core documentation, you're not supposed to
decorate IFormFile parameters with the [FromForm] attribute as the binding source is automatically inferred from the type. In fact,
the inferred value is BindingSource.FormFile and if you apply the attribute it will be set to BindingSource.Form instead, which breaks
ApiExplorer, the metadata component that ships with ASP.NET Core and is heavily relied on by Swashbuckle.AspNetCore. One particular issue here is
that SwaggerUI will not treat the parameter as a file and so will not display a file upload button, if you do mistakenly include this attribute.
Important
ApiExplorer (the ASP.NET Core metadata component that Swashbuckle.AspNetCore is built on) does not surface the FileResult types by
default and so you need to explicitly configure it to do so with [ProducesResponseType]:
[HttpGet("download/{fileName}")]
[ProducesResponseType(typeof(FileStreamResult), StatusCodes.Status200OK, "image/jpeg")]
public FileStreamResult GetImage(string fileName)
{
// ...
return new FileStreamResult(Stream.Null, "image/jpeg");
}To enhance the generated docs with human-friendly descriptions, you can annotate endpoints and models with XML Comments and configure Swashbuckle.AspNetCore to include those comments into the generated OpenAPI document.
First open the Properties dialog for your project, click the "Build" tab and ensure that "XML documentation file" is checked, or add an
<GenerateDocumentationFile>true</GenerateDocumentationFile> element to a <PropertyGroup> in your .csproj file. This
will produce a file containing all XML comments at build-time.
At this point, any classes or methods that are not annotated with XML comments will trigger a build warning. To suppress this, enter the warning code
1591into the "Suppress warnings" field in the Properties dialog or add<NoWarn>$(NoWarn);1591</NoWarn>to a<PropertyGroup>of your.csprojproject file.
Next configure Swashbuckle.AspNetCore to incorporate the XML comments on file into the generated OpenAPI JSON:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc(
"v1",
new OpenApiInfo
{
Title = "My API - V1",
Version = "v1"
}
);
options.IncludeXmlComments(Assembly.GetExecutingAssembly());
// or options.IncludeXmlComments(typeof(MyController).Assembly));
});Next annotate your endpoints with summary, remarks, param and/or response tags as desired:
/// <summary>
/// Retrieves a specific product line by unique id
/// </summary>
/// <remarks>Awesomeness!</remarks>
/// <param name="id" example="123">The product line id</param>
/// <response code="200">Product line retrieved</response>
/// <response code="404">Product line not found</response>
/// <response code="500">Oops! Can't lookup your product line right now</response>
[HttpGet("product/{id}")]
[ProducesResponseType(typeof(ProductLine), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(500)]
public ProductLine GetProductBySystemId(int id)
{
// ...
return new ProductLine();
}Then annotate your types with summary and example tags, other tags (remarks, para, etc.) are not supported:
public class ProductLine
{
/// <summary>
/// The name of the product
/// </summary>
/// <example>Men's basketball shoes</example>
public string Name { get; set; } = string.Empty;
/// <summary>
/// Quantity left in stock
/// </summary>
/// <example>10</example>
public int AvailableStock { get; set; }
/// <summary>
/// The sizes the product is available in
/// </summary>
/// <example>["Small", "Medium", "Large"]</example>
public List<string> Sizes { get; set; } = [];
}Finally, rebuild your project to update the XML Comments file and navigate to the OpenAPI JSON endpoint. Note how the descriptions are mapped onto corresponding OpenAPI properties.
Note
You can also provide OpenAPI schema descriptions by annotating your API models and their properties with <summary> tags. If you
have multiple XML comments files (e.g. separate libraries for controllers and models), you can invoke the IncludeXmlComments method
multiple times and they will all be merged into the generated OpenAPI document.
In addition to "PathItems", "Operations" and "Responses", which Swashbuckle.AspNetCore generates for you, OpenAPI also supports
global metadata. For example, you can provide a full description for your API, terms
of service or even contact and licensing information:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "My API - V1",
Version = "v1",
Description = "A sample API to demo Swashbuckle",
TermsOfService = new Uri("http://tempuri.org/terms"),
Contact = new OpenApiContact
{
Name = "Joe Developer",
Email = "joe.developer@tempuri.org"
},
License = new OpenApiLicense
{
Name = "Apache 2.0",
Url = new Uri("https://www.apache.org/licenses/LICENSE-2.0.html")
}
}
);
});Tip
Use IntelliSense to see what other members are available.
With the setup described above, the generator will include all API operations in a single Swagger document. However, you can create multiple documents if necessary. For example, you may want a separate document for each version of your API. To do this, start by defining multiple Swagger documents in your application startup code:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API - V1", Version = "v1" });
options.SwaggerDoc("v2", new OpenApiInfo { Title = "My API - V2", Version = "v2" });
});Note
Take note of the first argument to SwaggerDoc. It must be a URI-friendly name that uniquely identifies the document.
It's subsequently used to make up the path for requesting the corresponding Swagger JSON. For example, with the default
routing, the above documents will be available at /swagger/v1/swagger.json and /swagger/v2/swagger.json.
Next, you'll need to inform Swashbuckle which actions to include in each document. Although this can be customized (see below), by
default, the generator will use the ApiDescription.GroupName property, part of the built-in metadata layer that ships with
ASP.NET Core, to make this distinction. You can set this by decorating individual actions or by applying an application-wide convention.
To include an action in a specific Swagger document, decorate it with [ApiExplorerSettings] and set GroupName to the corresponding
document name (case sensitive):
[HttpPost]
[ApiExplorerSettings(GroupName = "v2")]
public void PostLine([FromBody] ProductLine product)
{
// ...
}To group by convention instead of decorating every action, you can apply a custom controller or action convention. For example, you could wire up the following convention to assign actions to documents based on the controller namespace.
📝 ApiExplorerGroupPerVersionConvention.cs
public class ApiExplorerGroupPerVersionConvention : IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
var controllerNamespace = controller.ControllerType.Namespace; // e.g. "Controllers.V1"
var apiVersion = controllerNamespace?.Split('.').Last().ToLower();
controller.ApiExplorer.GroupName = apiVersion;
}
}📝 Startup.cs
services.AddMvc(options =>
options.Conventions.Add(new ApiExplorerGroupPerVersionConvention())
);When selecting actions for a given Swagger document, the generator invokes a DocInclusionPredicate against every ApiDescription
that's surfaced by the framework. The default implementation inspects ApiDescription.GroupName and returns true if the value is either null
or equal to the requested document name. However, you can also provide a custom inclusion predicate. For example, if you're using an
attribute-based approach to implement API versioning (e.g. Microsoft.AspNetCore.Mvc.Versioning), you could configure a custom predicate that leverages the versioning attributes instead:
options.DocInclusionPredicate((docName, apiDesc) =>
{
if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo))
{
return false;
}
var versions = methodInfo.DeclaringType?
.GetCustomAttributes(true)
.OfType<ApiVersionAttribute>()
.SelectMany(attribute => attribute.Versions) ?? [];
return versions.Any(version => $"v{version}" == docName);
});If you're using the SwaggerUI middleware, you'll need to specify any additional OpenAPI endpoints you want to expose.
See List Multiple OpenAPI Documents for more information.
The OpenAPI specification includes a deprecated flag for indicating that an operation is deprecated
(obsolete) and should be refrained from being used. The OpenAPI generator will automatically set this flag if the corresponding action is
decorated with the [Obsolete] attribute. However, instead of setting a flag, you can configure the generator to ignore obsolete actions altogether:
services.AddSwaggerGen(options =>
{
options.IgnoreObsoleteActions();
});A similar approach can also be used to omit obsolete properties from Schemas in the OpenAPI document. That is, you can decorate
model properties with [Obsolete] and configure Swashbuckle.AspNetCore to omit those properties when generating JSON schemas:
services.AddSwaggerGen(options =>
{
options.IgnoreObsoleteProperties();
});You can omit operations from the OpenAPI output by decorating individual actions or by applying an application-wide convention.
To omit a specific action, decorate it with [ApiExplorerSettings] and set the IgnoreApi property to true:
[HttpDelete("{id}")]
[ApiExplorerSettings(IgnoreApi = true)]
public void Delete(int id)
{
// ...
}To omit actions by convention instead of decorating them individually, you can apply a custom action convention. For example, you
could wire up the following convention to only document GET operations:
📝 ApiExplorerGetsOnlyConvention.cs
public class ApiExplorerGetsOnlyConvention : IActionModelConvention
{
public void Apply(ActionModel action)
{
action.ApiExplorer.IsVisible = action.Attributes.OfType<HttpGetAttribute>().Any();
}
}📝 Startup.cs
services.AddMvc(options =>
options.Conventions.Add(new ApiExplorerGetsOnlyConvention())
);The OpenAPI specification allows one or more "tags" to be assigned to an operation. The OpenAPI generator
will assign the controller name as the default tag. This is important to note if you're using the SwaggerUI middleware as it uses this
value to group operations.
You can override the default tag by providing a function that applies tags by convention. For example, the following configuration will tag, and therefore group operations in the UI, by HTTP method:
services.AddSwaggerGen(options =>
{
options.TagActionsBy(api => [api.HttpMethod]);
});By default, actions are ordered by assigned tag (see above) before they're grouped into the path-centric, nested structure of the OpenAPI specification. However, you can change the default ordering of actions with a custom sorting strategy:
services.AddSwaggerGen(options =>
{
options.OrderActionsBy((apiDesc) => $"{apiDesc.ActionDescriptor.RouteValues["controller"]}_{apiDesc.HttpMethod}");
});Note
This dictates the sort order before actions are grouped and transformed into the OpenAPI format. Therefore it affects the ordering of groups (i.e. OpenAPI "PathItems"), and the ordering of operations within a group, in the OpenAPI document that is output.
If the generator encounters complex parameter or response types, it will generate a corresponding JSON schema, add it to the global
components:schemas dictionary, and reference it from the operation description by unique Id. For example, if you have an action
that returns a Product type, then the generated schema will be referenced as follows:
responses: {
200: {
description: "OK",
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/Product"
}
}
}
}
}However, if it encounters multiple types with the same name but different namespaces (e.g. RequestModels.Product and ResponseModels.Product),
then Swashbuckle.AspNetCore will raise an exception due to "Conflicting schemaIds". In this case, you'll need to provide a custom Id strategy that
further qualifies the name:
services.AddSwaggerGen(options =>
{
options.CustomSchemaIds((type) => type.FullName);
});Note
See this GitHub issue for support for nested types.
Out-of-the-box, Swashbuckle.AspNetCore performs a best-effort generating JSON schemas that accurately describe your request and response payloads. However, if you're customizing serialization behavior for certain types in your API, you may need to help it out to get accurate output.
For example, you might have a class with multiple properties that you want to represent in JSON as a comma-separated string. To do this you
would probably implement a custom JsonConverter. In this case, Swashbuckle.AspNetCore doesn't know how the converter is implemented and so you would
need to provide it with a schema that accurately describes the type:
📝 PhoneNumber.cs
public class PhoneNumber
{
public string CountryCode { get; set; } = string.Empty;
public string AreaCode { get; set; } = string.Empty;
public string SubscriberId { get; set; } = string.Empty;
}📝 Startup.cs
services.AddSwaggerGen(options =>
{
options.MapType<PhoneNumber>(() => new OpenApiSchema { Type = JsonSchemaType.String });
});Swashbuckle.AspNetCore exposes a filter pipeline that hooks into the generation process. Once generated, individual metadata objects are passed
into the pipeline where they can be modified further. You can wire up custom filters to enrich the generated Operations, Schemas
and Documents.
Swashbuckle.AspNetCore retrieves an ApiDescription, part of ASP.NET Core, for every action and uses it to generate a corresponding OpenApiOperation.
Once generated, it passes the OpenApiOperation and the ApiDescription through the list of configured Operation Filters.
In a typical filter implementation, you would inspect the ApiDescription for relevant information (e.g. route information, action attributes etc.)
and then update the OpenApiOperation accordingly. For example, the following filter lists an additional 401 response for all actions that
are decorated with [Authorize]:
📝 AuthResponsesOperationFilter.cs
public class AuthResponsesOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var hasAuthAttributes = context.MethodInfo.DeclaringType?.GetCustomAttributes(true)
.Union(context.MethodInfo.GetCustomAttributes(true))
.OfType<AuthorizeAttribute>()
.Any() ?? false;
if (hasAuthAttributes)
{
operation.Responses ??= [];
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
}
}
}📝 Startup.cs
services.AddSwaggerGen(options =>
{
options.OperationFilter<AuthResponsesOperationFilter>();
});Note
Filter pipelines are DI-aware. That is, you can create filters with constructor parameters and if the parameter types are registered with the DI framework, they'll be automatically injected when the filters are instantiated.
Swashbuckle.AspnetCore generates an OpenAPI-flavored JSONSchema for every parameter, response and property type that's exposed by your endpoints. Once generated, it passes the schema and type through the list of configured Schema Filters.
The example below adds an AutoRest vendor extension to inform the AutoRest tool how enums should be modelled when it generates the API client.
📝 AutoRestSchemaFilter.cs
public class AutoRestSchemaFilter : ISchemaFilter
{
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
{
var type = context.Type;
if (type.IsEnum && schema is OpenApiSchema concrete)
{
concrete.Extensions ??= new Dictionary<string, IOpenApiExtension>();
concrete.Extensions.Add(
"x-ms-enum",
new JsonNodeExtension(
new JsonObject
{
["name"] = type.Name,
["modelAsString"] = true
}
)
);
}
}
}📝 Startup.cs
services.AddSwaggerGen(options =>
{
options.SchemaFilter<AutoRestSchemaFilter>();
});The example below allows for automatic schema generation of generic Dictionary<Enum, TValue> objects.
Note that this only generates the OpenAPI document; System.Text.Json is not able to parse dictionary enums by default,
so you will need a special JsonConverter, as shown in the .NET documentation.
📝 DictionaryTKeyEnumTValueSchemaFilter.cs
public class DictionaryTKeyEnumTValueSchemaFilter : ISchemaFilter
{
public void Apply(IOpenApiSchema schema, SchemaFilterContext context)
{
if (schema is not OpenApiSchema concrete)
{
return;
}
// Only run for fields that are a Dictionary<Enum, TValue>
if (!context.Type.IsGenericType || !context.Type.GetGenericTypeDefinition().IsAssignableFrom(typeof(Dictionary<,>)))
{
return;
}
var genericArgs = context.Type.GetGenericArguments();
var keyType = genericArgs[0];
var valueType = genericArgs[1];
if (!keyType.IsEnum)
{
return;
}
concrete.Type = JsonSchemaType.Object;
concrete.Properties = keyType.GetEnumNames().ToDictionary(
name => name,
name => context.SchemaGenerator.GenerateSchema(valueType, context.SchemaRepository));
}
}📝 Startup.cs
services.AddSwaggerGen(options =>
{
// These will be replaced by DictionaryTKeyEnumTValueSchemaFilter, but are needed to avoid
// an error. You will need one for every kind of Dictionary<,> you have.
options.MapType<Dictionary<MyEnum, List<string>>>(() => new OpenApiSchema());
options.SchemaFilter<DictionaryTKeyEnumTValueSchemaFilter>();
});Once an OpenApiDocument has been generated, it too can be passed through a set of pre-configured Document Filters.
This gives full control to modify the document however you see fit. To ensure you're still returning valid OpenAPI JSON, you
should have a read through the specification before using this filter type.
The example below provides a description for any tags that are assigned to operations in the document:
public class TagDescriptionsDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.Tags = new HashSet<OpenApiTag>()
{
new() { Name = "Products", Description = "Browse/manage the product catalog" },
new() { Name = "Orders", Description = "Submit orders" }
};
}
}Note
If you're using the SwaggerUI middleware, the TagDescriptionsDocumentFilter demonstrated above
could be used to display additional descriptions beside each group of operations.
In OpenAPI, you can describe how your API is secured by defining one or more security schemes (e.g. Basic, API key, OAuth2 etc.) and declaring which of those schemes are applicable globally or for specific operations. For more details, take a look at the Security Requirement Object in the OpenAPI specification.
In Swashbuckle.AspNetCore, you can define schemes by invoking the AddSecurityDefinition method, providing a name and an instance of
OpenApiSecurityScheme. For example you can define an OAuth 2.0 - implicit flow as follows:
📝 Startup.cs
services.AddSwaggerGen(options =>
{
// Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("/auth-server/connect/authorize", UriKind.Relative),
Scopes = new Dictionary<string, string>
{
["readAccess"] = "Access read operations",
["writeAccess"] = "Access write operations"
}
}
}
});
});Note
In addition to defining a scheme, you also need to indicate which operations that scheme is applicable to. You can apply schemes
globally (i.e. to all operations) through the AddSecurityRequirement method. The example below indicates that the scheme called
"oauth2" should be applied to all operations, and that the "readAccess" and "writeAccess" scopes are required. When applying
schemes of type other than "oauth2", the array of scopes must be empty.
📝 Startup.cs
services.AddSwaggerGen(options =>
{
options.AddSecurityRequirement((document) => new OpenApiSecurityRequirement()
{
[new OpenApiSecuritySchemeReference("oauth2", document)] = ["readAccess", "writeAccess"]
});
});If you have schemes that are only applicable for certain operations, you can apply them through an Operation filter. For
example, the following filter adds OAuth2 requirements based on the presence of the AuthorizeAttribute:
📝 SecurityRequirementsOperationFilter.cs
public class SecurityRequirementsOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// Policy names map to scopes
var requiredScopes = context.MethodInfo
.GetCustomAttributes(true)
.OfType<AuthorizeAttribute>()
.Select(attribute => attribute.Policy!)
.Distinct()
.ToList();
if (requiredScopes.Count > 0)
{
operation.Responses ??= [];
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });
var scheme = new OpenApiSecuritySchemeReference("oauth2", context.Document);
operation.Security =
[
new OpenApiSecurityRequirement
{
[scheme] = requiredScopes
}
];
}
}
}Note
If you're using the SwaggerUI middleware, you can enable interactive OAuth2.0 flows that are powered by the emitted
security metadata. See Enabling OAuth2.0 Flows for more details.
services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("bearer", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
Description = "JWT Authorization header using the Bearer scheme."
});
options.AddSecurityRequirement(document => new OpenApiSecurityRequirement
{
[new OpenApiSecuritySchemeReference("bearer", document)] = []
});
});OpenAPI defines the allOf and oneOf keywords for describing
inheritance and polymorphism relationships
in schema definitions. For example, if you're using a base class for models that share common properties you can use the allOf
keyword to describe the inheritance hierarchy. Or, if your serializer supports polymorphic serialization/deserialization, you can use
the oneOf keyword to document all the "possible" schemas for requests/responses that vary by subtype.
By default, Swashbuckle.AspNetCore flattens inheritance hierarchies. That is, for derived models, the inherited properties are combined and listed
alongside the declared properties. This can cause a lot of duplication in the generated OpenAPI document, particularly when there's multiple subtypes.
It's also problematic if you're using a client generator (e.g. NSwag) and would like to maintain the inheritance hierarchy in the generated
client models. To work around this, you can apply the UseAllOfForInheritance setting, and this will leverage the allOf keyword to
incorporate inherited properties by reference in the generated OpenAPI document:
Circle: {
type: "object",
allOf: [
{
$ref: "#/components/schemas/Shape"
}
],
properties: {
radius: {
type: "integer",
format: "int32",
}
},
},
Shape: {
type: "object",
properties: {
name: {
type: "string",
nullable: true,
}
},
}If your serializer supports polymorphic serialization/deserialization and you would like to list the possible subtypes for an action
that accepts/returns abstract base types, you can apply the UseOneOfForPolymorphism setting. As a result, the generated request/response
schemas will reference a collection of "possible" schemas instead of just the base class schema:
"requestBody": {
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/Rectangle"
},
{
"$ref": "#/components/schemas/Circle"
}
]
}
}
}
}As inheritance and polymorphism relationships can often become quite complex, not just in your own models but also within the .NET class library, Swashbuckle.AspNetCore is selective about which hierarchies it does and doesn't expose in the generated OpenAPI document. By default, it will pick up any subtypes that are defined in the same assembly as a given base type. If you'd like to override this behavior, you can provide a custom selector method:
services.AddSwaggerGen(options =>
{
options.UseAllOfForInheritance();
options.SelectSubTypesUsing(baseType =>
{
return typeof(Program).Assembly.GetTypes().Where(type => type.IsSubclassOf(baseType));
});
});Note
If you're using the Swashbuckle Annotations library, it
contains a custom selector that's based on the presence of [JsonDerivedType] attributes on base class definitions. This
way, you can use simple attributes to explicitly list the inheritance and/or polymorphism relationships you want to expose.
To enable this behavior, check out the Annotations docs.
In conjunction with the oneOf and/or allOf keywords, OpenAPI supports a discriminator field on base schema definitions.
This keyword points to the property that identifies the specific type being represented by a given payload. In addition to the property
name, the discriminator description may also include a mapping which maps discriminator values to specific schema definitions.
For example, the Newtonsoft serializer supports polymorphic serialization/deserialization by emitting/accepting a "$type" property on
JSON instances. The value of this property will be the assembly qualified type name
of the type represented by a given JSON instance. So, to explicitly describe this behavior in OpenAPI, the corresponding request/response
schema could be defined as follows:
components: {
schemas: {
Shape: {
required: [
"$type"
],
type: "object",
properties: {
$type: {
type: "string"
},
discriminator: {
propertyName: "$type",
mapping: {
Rectangle: "#/components/schemas/Rectangle",
Circle: "#/components/schemas/Circle"
}
}
}
},
Rectangle: {
type: "object",
allOf: [
{
"$ref": "#/components/schemas/Shape"
}
],
...
},
Circle: {
type: "object",
allOf: [
{
"$ref": "#/components/schemas/Shape"
}
],
...
}
}
}If UseAllOfForInheritance or UseOneOfForPolymorphism is enabled, and your serializer supports (and has enabled) emitting/accepting
a discriminator property, then Swashbuckle will automatically generate the corresponding discriminator metadata on base schema definitions.
Alternatively, if you've customized your serializer to support polymorphic serialization/deserialization, you can provide some custom selector functions to determine the discriminator name and corresponding mapping:
services.AddSwaggerGen(options =>
{
options.UseAllOfForInheritance();
options.SelectDiscriminatorNameUsing((baseType) => "TypeName");
options.SelectDiscriminatorValueUsing((subType) => subType.Name);
});Note
If you're using the Swashbuckle Annotations library, it
contains custom selector functions that are based on the presence of [JsonPolymorphic] and [JsonDerivedType] attributes on base
class definitions. This way, you can use simple attributes to explicitly provide discriminator metadata. To enable this behavior, check
out the Annotations documentation.