Skip to content

Commit 7750aa6

Browse files
Fix reported code analysis items
1 parent 597472f commit 7750aa6

File tree

6 files changed

+74
-11
lines changed

6 files changed

+74
-11
lines changed

src/Common.OData/AspNet.OData/Routing/VersionedAttributeRoutingConvention.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
/// </summary>
2424
public partial class VersionedAttributeRoutingConvention
2525
{
26-
readonly ConcurrentDictionary<ApiVersion, IReadOnlyDictionary<ODataPathTemplate, ControllerActionDescriptor>> attributeMappingsPerApiVersion =
27-
new ConcurrentDictionary<ApiVersion, IReadOnlyDictionary<ODataPathTemplate, ControllerActionDescriptor>>();
26+
readonly ConcurrentDictionary<ApiVersion, IReadOnlyDictionary<ODataPathTemplate, ControllerActionDescriptor>> attributeMappingsPerApiVersion = new();
2827

2928
/// <summary>
3029
/// Gets the name of the route associated the routing convention.
@@ -101,7 +100,11 @@ static bool IsODataRouteParameter( KeyValuePair<string, object> routeDatum )
101100

102101
if ( pathTemplate.StartsWith( "/", Ordinal ) )
103102
{
103+
#if NETFRAMEWORK
104104
pathTemplate = pathTemplate.Substring( 1 );
105+
#else
106+
pathTemplate = pathTemplate[1..];
107+
#endif
105108
}
106109

107110
return ODataPathTemplateHandler.SafeParseTemplate( pathTemplate, serviceProvider );

src/Microsoft.AspNet.OData.Versioning.ApiExplorer/Web.Http.Description/ODataApiExplorer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
/// </summary>
3232
public class ODataApiExplorer : VersionedApiExplorer
3333
{
34-
static readonly Regex odataVariableRegex = new Regex( $"{{\\*{ODataRouteConstants.ODataPath}}}", CultureInvariant | Compiled | IgnoreCase );
34+
static readonly Regex odataVariableRegex = new( $"{{\\*{ODataRouteConstants.ODataPath}}}", CultureInvariant | Compiled | IgnoreCase );
3535

3636
/// <summary>
3737
/// Initializes a new instance of the <see cref="ODataApiExplorer"/> class.
@@ -77,7 +77,7 @@ protected override bool ShouldExploreAction( string actionRouteParameterValue, H
7777
throw new ArgumentNullException( nameof( actionDescriptor ) );
7878
}
7979

80-
if ( !( route is ODataRoute ) )
80+
if ( route is not ODataRoute )
8181
{
8282
return base.ShouldExploreAction( actionRouteParameterValue, actionDescriptor, route, apiVersion );
8383
}
@@ -154,7 +154,7 @@ protected override Collection<VersionedApiDescription> ExploreRouteControllers(
154154
throw new ArgumentNullException( nameof( controllerMappings ) );
155155
}
156156

157-
if ( !( route is ODataRoute ) )
157+
if ( route is not ODataRoute )
158158
{
159159
return base.ExploreRouteControllers( controllerMappings, route, apiVersion );
160160
}
@@ -333,7 +333,7 @@ void ExploreRouteActions(
333333

334334
static bool WillReadUri( HttpParameterBinding parameterBinding )
335335
{
336-
if ( !( parameterBinding is IValueProviderParameterBinding binding ) )
336+
if ( parameterBinding is not IValueProviderParameterBinding binding )
337337
{
338338
return false;
339339
}

src/Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer/ApiDescriptionExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public static class ApiDescriptionExtensions
2121
/// </summary>
2222
/// <param name="apiDescription">The <see cref="ApiDescription">API description</see> to get the API version for.</param>
2323
/// <returns>The associated <see cref="ApiVersion">API version</see> or <c>null</c>.</returns>
24-
public static ApiVersion GetApiVersion( this ApiDescription apiDescription ) => apiDescription.GetProperty<ApiVersion>();
24+
public static ApiVersion GetApiVersion( this ApiDescription apiDescription ) =>
25+
apiDescription.GetProperty<ApiVersion>() ?? ApiVersion.Neutral;
2526

2627
/// <summary>
2728
/// Gets a value indicating whether the associated API description is deprecated.
@@ -73,13 +74,13 @@ public static bool TryUpdateRelativePathAndRemoveApiVersionParameter( this ApiDe
7374
}
7475

7576
var parameter = apiDescription.ParameterDescriptions.FirstOrDefault( pd => pd.Source == Path && pd.ModelMetadata?.DataTypeName == nameof( ApiVersion ) );
77+
var relativePath = apiDescription.RelativePath;
7678

77-
if ( parameter == null )
79+
if ( parameter == null || string.IsNullOrEmpty( relativePath ) )
7880
{
7981
return false;
8082
}
8183

82-
var relativePath = apiDescription.RelativePath;
8384
var token = '{' + parameter.Name + '}';
8485
var value = apiDescription.GetApiVersion().ToString( options.SubstitutionFormat, InvariantCulture );
8586
var newRelativePath = relativePath.Replace( token, value, StringComparison.Ordinal );

src/Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer/ApiVersionModelMetadata.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,25 @@ public ApiVersionModelMetadata( IModelMetadataProvider modelMetadataProvider, st
3939
public override ModelPropertyCollection Properties => inner.Properties;
4040

4141
/// <inheritdoc />
42+
#if NET6_0_OR_GREATER
43+
public override string? BinderModelName => inner.BinderModelName;
44+
#else
4245
public override string BinderModelName => inner.BinderModelName;
46+
#endif
4347

4448
/// <inheritdoc />
49+
#if NET6_0_OR_GREATER
50+
public override Type? BinderType => inner.BinderType;
51+
#else
4552
public override Type BinderType => inner.BinderType;
53+
#endif
4654

4755
/// <inheritdoc />
56+
#if NET6_0_OR_GREATER
57+
public override BindingSource? BindingSource => inner.BindingSource;
58+
#else
4859
public override BindingSource BindingSource => inner.BindingSource;
60+
#endif
4961

5062
/// <inheritdoc />
5163
public override bool ConvertEmptyStringToNull => inner.ConvertEmptyStringToNull;
@@ -57,24 +69,40 @@ public ApiVersionModelMetadata( IModelMetadataProvider modelMetadataProvider, st
5769
public override string Description => description;
5870

5971
/// <inheritdoc />
72+
#if NET6_0_OR_GREATER
73+
public override string? DisplayFormatString => inner.DisplayFormatString;
74+
#else
6075
public override string DisplayFormatString => inner.DisplayFormatString;
76+
#endif
6177

6278
/// <inheritdoc />
6379
#pragma warning disable CA1721 // Property names should not match get methods; inherited member
6480
public override string DisplayName => SR.ApiVersionDisplayName;
6581
#pragma warning restore CA1721
6682

6783
/// <inheritdoc />
84+
#if NET6_0_OR_GREATER
85+
public override string? EditFormatString => inner.EditFormatString;
86+
#else
6887
public override string EditFormatString => inner.EditFormatString;
88+
#endif
6989

7090
/// <inheritdoc />
7191
public override ModelMetadata? ElementMetadata => inner.ElementMetadata;
7292

7393
/// <inheritdoc />
94+
#if NET6_0_OR_GREATER
95+
public override IEnumerable<KeyValuePair<EnumGroupAndName, string>>? EnumGroupedDisplayNamesAndValues => inner.EnumGroupedDisplayNamesAndValues;
96+
#else
7497
public override IEnumerable<KeyValuePair<EnumGroupAndName, string>> EnumGroupedDisplayNamesAndValues => inner.EnumGroupedDisplayNamesAndValues;
98+
#endif
7599

76100
/// <inheritdoc />
101+
#if NET6_0_OR_GREATER
102+
public override IReadOnlyDictionary<string, string>? EnumNamesAndValues => inner.EnumNamesAndValues;
103+
#else
77104
public override IReadOnlyDictionary<string, string> EnumNamesAndValues => inner.EnumNamesAndValues;
105+
#endif
78106

79107
/// <inheritdoc />
80108
public override bool HasNonDefaultEditFormat => inner.HasNonDefaultEditFormat;
@@ -110,13 +138,25 @@ public ApiVersionModelMetadata( IModelMetadataProvider modelMetadataProvider, st
110138
public override int Order => inner.Order;
111139

112140
/// <inheritdoc />
141+
#if NET6_0_OR_GREATER
142+
public override string? Placeholder => inner.Placeholder;
143+
#else
113144
public override string Placeholder => inner.Placeholder;
145+
#endif
114146

115147
/// <inheritdoc />
148+
#if NET6_0_OR_GREATER
149+
public override string? NullDisplayText => inner.NullDisplayText;
150+
#else
116151
public override string NullDisplayText => inner.NullDisplayText;
152+
#endif
117153

118154
/// <inheritdoc />
155+
#if NET6_0_OR_GREATER
156+
public override IPropertyFilterProvider? PropertyFilterProvider => inner.PropertyFilterProvider;
157+
#else
119158
public override IPropertyFilterProvider PropertyFilterProvider => inner.PropertyFilterProvider;
159+
#endif
120160

121161
/// <inheritdoc />
122162
public override bool ShowForDisplay => inner.ShowForDisplay;
@@ -125,10 +165,18 @@ public ApiVersionModelMetadata( IModelMetadataProvider modelMetadataProvider, st
125165
public override bool ShowForEdit => inner.ShowForEdit;
126166

127167
/// <inheritdoc />
168+
#if NET6_0_OR_GREATER
169+
public override string? SimpleDisplayProperty => inner.SimpleDisplayProperty;
170+
#else
128171
public override string SimpleDisplayProperty => inner.SimpleDisplayProperty;
172+
#endif
129173

130174
/// <inheritdoc />
175+
#if NET6_0_OR_GREATER
176+
public override string? TemplateHint => inner.TemplateHint;
177+
#else
131178
public override string TemplateHint => inner.TemplateHint;
179+
#endif
132180

133181
/// <inheritdoc />
134182
public override bool ValidateChildren => inner.ValidateChildren;
@@ -137,9 +185,17 @@ public ApiVersionModelMetadata( IModelMetadataProvider modelMetadataProvider, st
137185
public override IReadOnlyList<object> ValidatorMetadata => inner.ValidatorMetadata;
138186

139187
/// <inheritdoc />
188+
#if NET6_0_OR_GREATER
189+
public override Func<object, object?>? PropertyGetter => inner.PropertyGetter;
190+
#else
140191
public override Func<object, object> PropertyGetter => inner.PropertyGetter;
192+
#endif
141193

142194
/// <inheritdoc />
195+
#if NET6_0_OR_GREATER
196+
public override Action<object, object?>? PropertySetter => inner.PropertySetter;
197+
#else
143198
public override Action<object, object> PropertySetter => inner.PropertySetter;
199+
#endif
144200
}
145201
}

src/Microsoft.AspNetCore.Mvc.Versioning/Routing/ApiVersionLinkGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ public class ApiVersionLinkGenerator : LinkGenerator
6464
public override string? GetUriByAddress<TAddress>(
6565
TAddress address,
6666
RouteValueDictionary values,
67+
#if NET6_0_OR_GREATER
68+
string? scheme,
69+
#else
6770
string scheme,
71+
#endif
6872
HostString host,
6973
PathString pathBase = default,
7074
FragmentString fragment = default,

test/Microsoft.AspNet.WebApi.Versioning.Tests/Versioning/Conventions/VersionByNamespaceConventionTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public void apply_should_ignore_unmatched_namespace()
8686
var controllerType = new TestType( "Contoso.Api.Controllers" );
8787
var attributes = new Attribute[0];
8888
var controllerModel = new TestHttpControllerDescriptor( controllerType.GetTypeInfo(), attributes );
89-
var controller = new Mock<IControllerConventionBuilder>();
9089
var convention = new VersionByNamespaceConvention();
9190

9291
// act
@@ -136,7 +135,7 @@ internal TestHttpControllerDescriptor(Type controllerType, IReadOnlyList<Attribu
136135
this.attributes = attributes;
137136
}
138137

139-
public override Collection<T> GetCustomAttributes<T>( bool inherit ) => new Collection<T>( attributes.OfType<T>().ToArray() );
138+
public override Collection<T> GetCustomAttributes<T>( bool inherit ) => new( attributes.OfType<T>().ToArray() );
140139
}
141140
}
142141
}

0 commit comments

Comments
 (0)