|
1 | 1 | using ApiVersioning.Examples; |
2 | 2 | using Asp.Versioning; |
3 | 3 | using Asp.Versioning.Conventions; |
| 4 | +using Microsoft.AspNetCore.Mvc.ApiExplorer; |
4 | 5 | using Microsoft.AspNetCore.OData; |
5 | | -using Microsoft.Extensions.Options; |
6 | | -using Swashbuckle.AspNetCore.SwaggerGen; |
| 6 | +using Scalar.AspNetCore; |
| 7 | +using System.Reflection; |
7 | 8 | using static Microsoft.AspNetCore.OData.Query.AllowedQueryOptions; |
8 | 9 | using PeopleControllerV2 = ApiVersioning.Examples.V2.PeopleController; |
9 | 10 | using PeopleControllerV3 = ApiVersioning.Examples.V3.PeopleController; |
10 | 11 |
|
11 | | -var builder = WebApplication.CreateBuilder( args ); |
| 12 | +[assembly: AssemblyDescription( "An example API" )] |
12 | 13 |
|
13 | | -// Add services to the container. |
| 14 | +var builder = WebApplication.CreateBuilder( args ); |
14 | 15 |
|
15 | 16 | builder.Services.AddControllers() |
16 | 17 | .AddOData( |
|
31 | 32 | // "api-supported-versions" and "api-deprecated-versions" |
32 | 33 | options.ReportApiVersions = true; |
33 | 34 |
|
| 35 | + options.Policies.Deprecate( 0.9 ) |
| 36 | + .Effective( DateTimeOffset.Now ) |
| 37 | + .Link( "policy.html" ) |
| 38 | + .Title( "Version Deprecation Policy" ) |
| 39 | + .Type( "text/html" ); |
| 40 | + |
34 | 41 | options.Policies.Sunset( 0.9 ) |
35 | 42 | .Effective( DateTimeOffset.Now.AddDays( 60 ) ) |
36 | 43 | .Link( "policy.html" ) |
37 | | - .Title( "Versioning Policy" ) |
| 44 | + .Title( "Version Sunset Policy" ) |
38 | 45 | .Type( "text/html" ); |
39 | 46 | } ) |
40 | 47 | .AddOData( options => options.AddRouteComponents( "api" ) ) |
|
61 | 68 | .Allow( Skip | Count ) |
62 | 69 | .AllowTop( 100 ) |
63 | 70 | .AllowOrderBy( "firstName", "lastName" ); |
64 | | - } ); |
65 | | - |
66 | | -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle |
67 | | -builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>(); |
68 | | -builder.Services.AddSwaggerGen( |
69 | | - options => |
70 | | - { |
71 | | - // add a custom operation filter which sets default values |
72 | | - options.OperationFilter<SwaggerDefaultValues>(); |
73 | | - |
74 | | - var fileName = typeof( Program ).Assembly.GetName().Name + ".xml"; |
75 | | - var filePath = Path.Combine( AppContext.BaseDirectory, fileName ); |
76 | | - |
77 | | - // integrate xml comments |
78 | | - options.IncludeXmlComments( filePath ); |
79 | | - } ); |
| 71 | + } ) |
| 72 | + .AddOpenApi( options => options.Document.AddScalarTransformers() ); |
80 | 73 |
|
81 | 74 | var app = builder.Build(); |
82 | 75 |
|
83 | | -// Configure HTTP request pipeline. |
84 | | - |
85 | 76 | if ( app.Environment.IsDevelopment() ) |
86 | 77 | { |
87 | | - // Access ~/$odata to identify OData endpoints that failed to match a route template. |
| 78 | + // access ~/$odata to identify OData endpoints that failed to match a route template |
88 | 79 | app.UseODataRouteDebug(); |
89 | | -} |
90 | | - |
91 | | -app.UseSwagger(); |
92 | | -if ( app.Environment.IsDevelopment() ) |
93 | | -{ |
94 | | - app.UseSwaggerUI( |
| 80 | + app.MapOpenApi().WithDocumentPerVersion(); |
| 81 | + app.MapScalarApiReference( |
95 | 82 | options => |
96 | 83 | { |
97 | 84 | var descriptions = app.DescribeApiVersions(); |
98 | 85 |
|
99 | | - // build a swagger endpoint for each discovered API version |
100 | | - foreach ( var description in descriptions ) |
| 86 | + for ( var i = 0; i < descriptions.Count; i++ ) |
101 | 87 | { |
102 | | - var url = $"/swagger/{description.GroupName}/swagger.json"; |
103 | | - var name = description.GroupName.ToUpperInvariant(); |
104 | | - options.SwaggerEndpoint( url, name ); |
| 88 | + var description = descriptions[i]; |
| 89 | + var isDefault = i == descriptions.Count - 1; |
| 90 | + |
| 91 | + options.AddDocument( description.GroupName, description.GroupName, isDefault: isDefault ); |
105 | 92 | } |
106 | 93 | } ); |
107 | 94 | } |
|
0 commit comments