Skip to content

Commit 0d1b9fa

Browse files
Code clean up
1 parent f1e56be commit 0d1b9fa

File tree

5 files changed

+24
-14
lines changed

5 files changed

+24
-14
lines changed

examples/AspNetCore/OData/ODataOpenApiExample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
.AddOData(
1717
options =>
1818
{
19-
options.Count().Select().OrderBy();
19+
options.Count().Select().OrderBy().SetMaxTop( 3 );
2020
options.RouteOptions.EnableKeyInParenthesis = false;
2121
options.RouteOptions.EnableNonParenthesisForEmptyParameterFunction = true;
2222
options.RouteOptions.EnablePropertyNameCaseInsensitive = true;

examples/AspNetCore/OData/ODataOpenApiExample/V3/SuppliersController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public class SuppliersController : ODataController
2929
/// <returns>All available suppliers.</returns>
3030
/// <response code="200">Suppliers successfully retrieved.</response>
3131
[HttpGet]
32-
[EnableQuery]
32+
//[EnableQuery]
33+
[EnableQuery( MaxTop = 2 )]
3334
[Produces( "application/json" )]
3435
[ProducesResponseType( typeof( ODataValue<IEnumerable<Supplier>> ), Status200OK )]
3536
public IQueryable<Supplier> Get() => suppliers;

examples/AspNetCore/WebApi/MinimalOpenApiExample/MinimalOpenApiExample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0" />
89
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0-*" />
910
</ItemGroup>
1011

examples/AspNetCore/WebApi/MinimalOpenApiExample/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
.HasApiVersion( 1.0 );
5757

5858
ordersV1.MapGet( "/{id:int}", ( int id ) => new OrderV1() { Id = id, Customer = "John Doe" } )
59+
.WithOpenApi()
5960
.Produces<OrderV1>()
6061
.Produces( 404 );
6162

src/AspNetCore/WebApi/src/Asp.Versioning.Http/ApiVersioningFeature.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Asp.Versioning;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.Extensions.DependencyInjection;
77
using System.Globalization;
8+
using System.Runtime.CompilerServices;
89

910
/// <summary>
1011
/// Represents the API versioning feature.
@@ -33,11 +34,10 @@ public IReadOnlyList<string> RawRequestedApiVersions
3334
{
3435
if ( rawApiVersions is null )
3536
{
36-
var reader =
37-
context.RequestServices.GetService<IApiVersionReader>() ??
38-
ApiVersionReader.Combine(
39-
new QueryStringApiVersionReader(),
40-
new UrlSegmentApiVersionReader() );
37+
var reader = context.RequestServices.GetService<IApiVersionReader>()
38+
?? ApiVersionReader.Combine(
39+
new QueryStringApiVersionReader(),
40+
new UrlSegmentApiVersionReader() );
4141

4242
rawApiVersions = reader.Read( context.Request );
4343
}
@@ -58,11 +58,7 @@ public string? RawRequestedApiVersion
5858
{
5959
0 => default,
6060
1 => values[0],
61-
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations; existing behavior via IApiVersionReader.Read
62-
_ => throw new AmbiguousApiVersionException(
63-
string.Format( CultureInfo.CurrentCulture, CommonSR.MultipleDifferentApiVersionsRequested, string.Join( ", ", values ) ),
64-
values ),
65-
#pragma warning restore CA1065
61+
_ => throw NewAmbiguousApiVersionException( values ),
6662
};
6763
}
6864
set
@@ -88,7 +84,8 @@ public ApiVersion? RequestedApiVersion
8884
return apiVersion;
8985
}
9086

91-
var parser = context.RequestServices.GetRequiredService<IApiVersionParser>();
87+
var parser = context.RequestServices.GetService<IApiVersionParser>()
88+
?? ApiVersionParser.Default;
9289

9390
try
9491
{
@@ -105,10 +102,20 @@ public ApiVersion? RequestedApiVersion
105102
{
106103
apiVersion = value;
107104

108-
if ( apiVersion is not null && ( rawApiVersions is null || rawApiVersions.Count == 0 ) )
105+
if ( apiVersion is not null &&
106+
( rawApiVersions is null || rawApiVersions.Count == 0 ) )
109107
{
110108
rawApiVersions = new[] { apiVersion.ToString() };
111109
}
112110
}
113111
}
112+
113+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
114+
private static AmbiguousApiVersionException NewAmbiguousApiVersionException( IReadOnlyList<string> values ) =>
115+
new(
116+
string.Format(
117+
CultureInfo.CurrentCulture,
118+
CommonSR.MultipleDifferentApiVersionsRequested,
119+
string.Join( ", ", values.ToArray(), 0, values.Count ) ),
120+
values );
114121
}

0 commit comments

Comments
 (0)