Skip to content

Commit ebd2e49

Browse files
Update OData examples
1 parent 3764f1c commit ebd2e49

20 files changed

Lines changed: 215 additions & 632 deletions

examples/AspNetCore/OData/ODataOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 0 additions & 162 deletions
This file was deleted.

examples/AspNetCore/OData/ODataOpenApiExample/FunctionsController.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
public class FunctionsController : ODataController
1313
{
1414
/// <summary>
15-
/// Gets the sales tax for a postal code.
15+
/// Get Sales Tax
1616
/// </summary>
17+
/// <description>Gets the sales tax for a postal code.</description>
1718
/// <param name="postalCode">The postal code to get the sales tax for.</param>
1819
/// <returns>The sales tax rate for the postal code.</returns>
20+
/// <response code="200">The sales tax was successfully retrieved.</response>
21+
/// <response code="404">The postal code was not found.</response>
1922
[HttpGet( "api/GetSalesTaxRate(PostalCode={postalCode})" )]
2023
[ProducesResponseType( typeof( double ), Status200OK )]
24+
[ProducesResponseType( Status404NotFound )]
2125
public IActionResult GetSalesTaxRate( int postalCode ) => Ok( 5.6 );
2226
}

examples/AspNetCore/OData/ODataOpenApiExample/ODataOpenApiExample.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
<PropertyGroup>
44
<TargetFramework>net10.0</TargetFramework>
5+
<AssemblyTitle>Example API</AssemblyTitle>
56
<GenerateDocumentationFile>true</GenerateDocumentationFile>
67
</PropertyGroup>
78

89
<ItemGroup>
9-
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0-*" />
10+
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="10.0.*">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="Scalar.AspNetCore" Version="2.12.*" />
15+
<PackageReference Include="Scalar.AspNetCore.Microsoft" Version="2.12.*" />
1016
</ItemGroup>
1117

1218
<ItemGroup>
1319
<ProjectReference Include="..\..\..\..\src\AspNetCore\OData\src\Asp.Versioning.OData.ApiExplorer\Asp.Versioning.OData.ApiExplorer.csproj" />
20+
<ProjectReference Include="..\..\..\..\src\AspNetCore\WebApi\src\Asp.Versioning.OpenApi\Asp.Versioning.OpenApi.csproj" />
1421
</ItemGroup>
1522

1623
</Project>

examples/AspNetCore/OData/ODataOpenApiExample/Program.cs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using ApiVersioning.Examples;
22
using Asp.Versioning;
33
using Asp.Versioning.Conventions;
4+
using Microsoft.AspNetCore.Mvc.ApiExplorer;
45
using Microsoft.AspNetCore.OData;
5-
using Microsoft.Extensions.Options;
6-
using Swashbuckle.AspNetCore.SwaggerGen;
6+
using Scalar.AspNetCore;
7+
using System.Reflection;
78
using static Microsoft.AspNetCore.OData.Query.AllowedQueryOptions;
89
using PeopleControllerV2 = ApiVersioning.Examples.V2.PeopleController;
910
using PeopleControllerV3 = ApiVersioning.Examples.V3.PeopleController;
1011

11-
var builder = WebApplication.CreateBuilder( args );
12+
[assembly: AssemblyDescription( "An example API" )]
1213

13-
// Add services to the container.
14+
var builder = WebApplication.CreateBuilder( args );
1415

1516
builder.Services.AddControllers()
1617
.AddOData(
@@ -31,10 +32,16 @@
3132
// "api-supported-versions" and "api-deprecated-versions"
3233
options.ReportApiVersions = true;
3334

35+
options.Policies.Deprecate( 0.9 )
36+
.Effective( DateTimeOffset.Now )
37+
.Link( "policy.html" )
38+
.Title( "Version Deprecation Policy" )
39+
.Type( "text/html" );
40+
3441
options.Policies.Sunset( 0.9 )
3542
.Effective( DateTimeOffset.Now.AddDays( 60 ) )
3643
.Link( "policy.html" )
37-
.Title( "Versioning Policy" )
44+
.Title( "Version Sunset Policy" )
3845
.Type( "text/html" );
3946
} )
4047
.AddOData( options => options.AddRouteComponents( "api" ) )
@@ -61,47 +68,27 @@
6168
.Allow( Skip | Count )
6269
.AllowTop( 100 )
6370
.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() );
8073

8174
var app = builder.Build();
8275

83-
// Configure HTTP request pipeline.
84-
8576
if ( app.Environment.IsDevelopment() )
8677
{
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
8879
app.UseODataRouteDebug();
89-
}
90-
91-
app.UseSwagger();
92-
if ( app.Environment.IsDevelopment() )
93-
{
94-
app.UseSwaggerUI(
80+
app.MapOpenApi().WithDocumentPerVersion();
81+
app.MapScalarApiReference(
9582
options =>
9683
{
9784
var descriptions = app.DescribeApiVersions();
9885

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++ )
10187
{
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 );
10592
}
10693
} );
10794
}

examples/AspNetCore/OData/ODataOpenApiExample/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"commandName": "Project",
1313
"dotnetRunMessages": true,
1414
"launchBrowser": true,
15-
"launchUrl": "swagger",
15+
"launchUrl": "scalar",
1616
"applicationUrl": "https://localhost:5001;http://localhost:5000",
1717
"environmentVariables": {
1818
"ASPNETCORE_ENVIRONMENT": "Development"
@@ -21,7 +21,7 @@
2121
"IIS Express": {
2222
"commandName": "IISExpress",
2323
"launchBrowser": true,
24-
"launchUrl": "swagger",
24+
"launchUrl": "scalar",
2525
"environmentVariables": {
2626
"ASPNETCORE_ENVIRONMENT": "Development"
2727
}

examples/AspNetCore/OData/ODataOpenApiExample/SwaggerDefaultValues.cs

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)