Skip to content

Support OData Collection Parameters #1075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
else
{
UpdateModelTypes( result, matched );
UpdateFunctionCollectionParameters( result, matched );
}
}

Expand Down Expand Up @@ -456,6 +457,75 @@
}
}

private static void UpdateFunctionCollectionParameters( ApiDescription description, IODataRoutingMetadata metadata )
{
var parameters = description.ParameterDescriptions;

if ( parameters.Count == 0 )
{
return;
}

var function = default( IEdmFunction );
var mapping = default( IDictionary<string, string> );

for ( var i = 0; i < metadata.Template.Count; i++ )
{
var segment = metadata.Template[i];

if ( segment is FunctionSegmentTemplate func )
{
function = func.Function;
mapping = func.ParameterMappings;
break;
}
else if ( segment is FunctionImportSegmentTemplate import )
{
function = import.FunctionImport.Function;
mapping = import.ParameterMappings;
break;
}
}

if ( function is null || mapping is null )
{
return;
}

var name = default( string );

foreach ( var parameter in function.Parameters )
{
if ( parameter.Type.IsCollection() &&
mapping.TryGetValue( parameter.Name, out name ) &&
parameters.SingleOrDefault( p => p.Name == name ) is { } param )
{
param.Source = BindingSource.Path;
break;
}
}
Comment on lines +497 to +506

Check notice

Code scanning / CodeQL

Missed opportunity to use Where Note

This foreach loop
implicitly filters its target sequence
- consider filtering the sequence explicitly using '.Where(...)'.

var path = description.RelativePath;

if ( string.IsNullOrEmpty( name ) || string.IsNullOrEmpty( path ) )
{
return;
}

var span = name.AsSpan();
Span<char> oldValue = stackalloc char[name.Length + 2];
Span<char> newValue = stackalloc char[name.Length + 4];

newValue[1] = oldValue[0] = '{';
newValue[^2] = oldValue[^1] = '}';
newValue[0] = '[';
newValue[^1] = ']';
span.CopyTo( oldValue.Slice( 1, name.Length ) );
span.CopyTo( newValue.Slice( 2, name.Length ) );

description.RelativePath = path.Replace( oldValue.ToString(), newValue.ToString(), Ordinal );
}

private sealed class ApiDescriptionComparer : IEqualityComparer<ApiDescription>
{
private readonly IEqualityComparer<string?> comparer = StringComparer.OrdinalIgnoreCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,10 @@ private static int ODataOrder() =>
new ODataApiDescriptionProvider(
new StubModelMetadataProvider(),
new StubModelTypeBuilder(),
new OptionsFactory<ODataOptions>(
Enumerable.Empty<IConfigureOptions<ODataOptions>>(),
Enumerable.Empty<IPostConfigureOptions<ODataOptions>>() ),
new OptionsFactory<ODataOptions>( [], [] ),
Opts.Create(
new ODataApiExplorerOptions(
new(
new StubODataApiVersionCollectionProvider(),
Enumerable.Empty<IModelConfiguration>() ) ) ) ).Order;
new( new StubODataApiVersionCollectionProvider(), [] ) ) ) ).Order;

[MethodImpl( MethodImplOptions.AggressiveInlining )]
private static void MarkAsAdHoc( ODataModelBuilder builder, IEdmModel model ) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,14 @@ public static bool TryUpdateRelativePathAndRemoveApiVersionParameter( this ApiDe
return false;
}

var token = '{' + parameter.Name + '}';
Span<char> token = stackalloc char[parameter.Name.Length + 2];

token[0] = '{';
token[^1] = '}';
parameter.Name.AsSpan().CopyTo( token.Slice( 1, parameter.Name.Length ) );

var value = apiVersion.ToString( options.SubstitutionFormat, CultureInfo.InvariantCulture );
var newRelativePath = relativePath.Replace( token, value, StringComparison.Ordinal );
var newRelativePath = relativePath.Replace( token.ToString(), value, StringComparison.Ordinal );

if ( relativePath == newRelativePath )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Asp.Versioning.ApiExplorer;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.AspNetCore.Routing.Template;
using System.Runtime.CompilerServices;
using static Asp.Versioning.ApiVersionParameterLocation;
using static System.Linq.Enumerable;
using static System.StringComparison;
Expand Down Expand Up @@ -304,7 +305,7 @@ routeInfo.Constraints is IEnumerable<IRouteConstraint> constraints &&
continue;
}

var token = $"{parameter.Name}:{constraintName}";
var token = FormatToken( parameter.Name, constraintName );

parameterDescription.Name = parameter.Name;
description.RelativePath = relativePath.Replace( token, parameter.Name, Ordinal );
Expand Down Expand Up @@ -375,7 +376,7 @@ routeInfo.Constraints is IEnumerable<IRouteConstraint> constraints &&
},
Source = BindingSource.Path,
};
var token = $"{parameter.Name}:{constraintName}";
var token = FormatToken( parameter.Name!, constraintName! );

description.RelativePath = relativePath.Replace( token, parameter.Name, Ordinal );
description.ParameterDescriptions.Insert( 0, result );
Expand Down Expand Up @@ -457,4 +458,18 @@ private static bool FirstParameterIsOptional(

return apiVersion == defaultApiVersion;
}

[MethodImpl( MethodImplOptions.AggressiveInlining )]
private static string FormatToken( ReadOnlySpan<char> parameterName, ReadOnlySpan<char> constraintName )
{
var left = parameterName.Length;
var right = constraintName.Length;
Span<char> token = stackalloc char[left + right + 1];

parameterName.CopyTo( token[..left] );
token[left] = ':';
constraintName.CopyTo( token.Slice( left + 1, right ) );

return token.ToString();
}
}
Loading