-
Notifications
You must be signed in to change notification settings - Fork 697
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenAPI example for Minimal APIs
- Loading branch information
1 parent
efd523e
commit 93bd8dc
Showing
13 changed files
with
707 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
examples/AspNetCore/WebApi/MinimalOpenApiExample/ConfigureSwaggerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
namespace ApiVersioning.Examples; | ||
|
||
using Asp.Versioning; | ||
using Asp.Versioning.ApiExplorer; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
using System.Text; | ||
|
||
/// <summary> | ||
/// Configures the Swagger generation options. | ||
/// </summary> | ||
/// <remarks>This allows API versioning to define a Swagger document per API version after the | ||
/// <see cref="IApiVersionDescriptionProvider"/> service has been resolved from the service container.</remarks> | ||
public class ConfigureSwaggerOptions : IConfigureOptions<SwaggerGenOptions> | ||
{ | ||
private readonly IApiVersionDescriptionProvider provider; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class. | ||
/// </summary> | ||
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param> | ||
public ConfigureSwaggerOptions( IApiVersionDescriptionProvider provider ) => this.provider = provider; | ||
|
||
/// <inheritdoc /> | ||
public void Configure( SwaggerGenOptions options ) | ||
{ | ||
// add a swagger document for each discovered API version | ||
// note: you might choose to skip or document deprecated API versions differently | ||
foreach ( var description in provider.ApiVersionDescriptions ) | ||
{ | ||
options.SwaggerDoc( description.GroupName, CreateInfoForApiVersion( description ) ); | ||
} | ||
} | ||
|
||
private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription description ) | ||
{ | ||
var text = new StringBuilder( "An example application with OpenAPI, Swashbuckle, and API versioning." ); | ||
var info = new OpenApiInfo() | ||
{ | ||
Title = "Example API", | ||
Version = description.ApiVersion.ToString(), | ||
Contact = new OpenApiContact() { Name = "Bill Mei", Email = "bill.mei@somewhere.com" }, | ||
License = new OpenApiLicense() { Name = "MIT", Url = new Uri( "https://opensource.org/licenses/MIT" ) } | ||
}; | ||
|
||
if ( description.IsDeprecated ) | ||
{ | ||
text.Append( " This API version has been deprecated." ); | ||
} | ||
|
||
if ( description.SunsetPolicy is SunsetPolicy policy ) | ||
{ | ||
if ( policy.Date is DateTimeOffset when ) | ||
{ | ||
text.Append( " The API will be sunset on " ) | ||
.Append( when.Date.ToShortDateString() ) | ||
.Append( '.' ); | ||
} | ||
|
||
if ( policy.HasLinks ) | ||
{ | ||
text.AppendLine(); | ||
|
||
for ( var i = 0; i < policy.Links.Count; i++ ) | ||
{ | ||
var link = policy.Links[i]; | ||
|
||
if ( link.Type == "text/html" ) | ||
{ | ||
text.AppendLine(); | ||
|
||
if ( link.Title.HasValue ) | ||
{ | ||
text.Append( link.Title.Value ).Append( ": " ); | ||
} | ||
|
||
text.Append( link.LinkTarget.OriginalString ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
info.Description = text.ToString(); | ||
|
||
return info; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/AspNetCore/WebApi/MinimalOpenApiExample/MinimalOpenApiExample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.0-*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\src\AspNetCore\WebApi\src\Asp.Versioning.Mvc.ApiExplorer\Asp.Versioning.Mvc.ApiExplorer.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
examples/AspNetCore/WebApi/MinimalOpenApiExample/Models/V1/Order.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
namespace ApiVersioning.Examples.Models.V1; | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// Represents an order. | ||
/// </summary> | ||
public class Order | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for the order. | ||
/// </summary> | ||
/// <value>The order's unique identifier.</value> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the order was created. | ||
/// </summary> | ||
/// <value>The order's creation date.</value> | ||
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now; | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the ordering customer. | ||
/// </summary> | ||
/// <value>The name of the customer that placed the order.</value> | ||
[Required] | ||
public string Customer { get; set; } | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/AspNetCore/WebApi/MinimalOpenApiExample/Models/V1/Person.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
namespace ApiVersioning.Examples.Models.V1; | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// Represents a person. | ||
/// </summary> | ||
public class Person | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for a person. | ||
/// </summary> | ||
/// <value>The person's unique identifier.</value> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the first name of a person. | ||
/// </summary> | ||
/// <value>The person's first name.</value> | ||
[Required] | ||
[StringLength( 25 )] | ||
public string FirstName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the last name of a person. | ||
/// </summary> | ||
/// <value>The person's last name.</value> | ||
[Required] | ||
[StringLength( 25 )] | ||
public string LastName { get; set; } | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/AspNetCore/WebApi/MinimalOpenApiExample/Models/V2/Order.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace ApiVersioning.Examples.Models.V2; | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// Represents an order. | ||
/// </summary> | ||
public class Order | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for the order. | ||
/// </summary> | ||
/// <value>The order's unique identifier.</value> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the order was created. | ||
/// </summary> | ||
/// <value>The order's creation date.</value> | ||
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now; | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the order becomes effective. | ||
/// </summary> | ||
/// <value>The order's effective date.</value> | ||
public DateTimeOffset EffectiveDate { get; set; } = DateTimeOffset.Now; | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the ordering customer. | ||
/// </summary> | ||
/// <value>The name of the customer that placed the order.</value> | ||
[Required] | ||
public string Customer { get; set; } | ||
} |
37 changes: 37 additions & 0 deletions
37
examples/AspNetCore/WebApi/MinimalOpenApiExample/Models/V2/Person.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace ApiVersioning.Examples.Models.V2; | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// Represents a person. | ||
/// </summary> | ||
public class Person | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for a person. | ||
/// </summary> | ||
/// <value>The person's unique identifier.</value> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the first name of a person. | ||
/// </summary> | ||
/// <value>The person's first name.</value> | ||
[Required] | ||
[StringLength( 25 )] | ||
public string FirstName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the last name of a person. | ||
/// </summary> | ||
/// <value>The person's last name.</value> | ||
[Required] | ||
[StringLength( 25 )] | ||
public string LastName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the email address for a person. | ||
/// </summary> | ||
/// <value>The person's email address.</value> | ||
public string Email { get; set; } | ||
} |
34 changes: 34 additions & 0 deletions
34
examples/AspNetCore/WebApi/MinimalOpenApiExample/Models/V3/Order.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace ApiVersioning.Examples.Models.V3; | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// Represents an order. | ||
/// </summary> | ||
public class Order | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for the order. | ||
/// </summary> | ||
/// <value>The order's unique identifier.</value> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the order was created. | ||
/// </summary> | ||
/// <value>The order's creation date.</value> | ||
public DateTimeOffset CreatedDate { get; set; } = DateTimeOffset.Now; | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the order becomes effective. | ||
/// </summary> | ||
/// <value>The order's effective date.</value> | ||
public DateTimeOffset EffectiveDate { get; set; } = DateTimeOffset.Now; | ||
|
||
/// <summary> | ||
/// Gets or sets the name of the ordering customer. | ||
/// </summary> | ||
/// <value>The name of the customer that placed the order.</value> | ||
[Required] | ||
public string Customer { get; set; } | ||
} |
43 changes: 43 additions & 0 deletions
43
examples/AspNetCore/WebApi/MinimalOpenApiExample/Models/V3/Person.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace ApiVersioning.Examples.Models.V3; | ||
|
||
using System.ComponentModel.DataAnnotations; | ||
|
||
/// <summary> | ||
/// Represents a person. | ||
/// </summary> | ||
public class Person | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for a person. | ||
/// </summary> | ||
/// <value>The person's unique identifier.</value> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the first name of a person. | ||
/// </summary> | ||
/// <value>The person's first name.</value> | ||
[Required] | ||
[StringLength( 25 )] | ||
public string FirstName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the last name of a person. | ||
/// </summary> | ||
/// <value>The person's last name.</value> | ||
[Required] | ||
[StringLength( 25 )] | ||
public string LastName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the email address for a person. | ||
/// </summary> | ||
/// <value>The person's email address.</value> | ||
public string Email { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the telephone number for a person. | ||
/// </summary> | ||
/// <value>The person's telephone number.</value> | ||
public string Phone { get; set; } | ||
} |
Oops, something went wrong.