Skip to content

Commit ff74f55

Browse files
authored
[Group 4] Enable nullable annotations for Microsoft.Extensions.Configuration.FileExtensions (#57434)
* Annotate * Update FileConfigurationProvider.cs * DisableImplicitAssemblyReferences * Use compaund assigment * DisallowNull * Suppress warning * GetFileProvider non nullable return
1 parent 64f4885 commit ff74f55

7 files changed

+28
-23
lines changed

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Microsoft.Extensions.Configuration
88
{
99
public static partial class FileConfigurationExtensions
1010
{
11-
public static System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext> GetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
11+
public static System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext>? GetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
1212
public static Microsoft.Extensions.FileProviders.IFileProvider GetFileProvider(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
1313
public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetBasePath(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, string basePath) { throw null; }
1414
public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext> handler) { throw null; }
@@ -27,10 +27,11 @@ public override void Load() { }
2727
public abstract partial class FileConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
2828
{
2929
protected FileConfigurationSource() { }
30-
public Microsoft.Extensions.FileProviders.IFileProvider FileProvider { get { throw null; } set { } }
31-
public System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext> OnLoadException { get { throw null; } set { } }
30+
public Microsoft.Extensions.FileProviders.IFileProvider? FileProvider { get { throw null; } set { } }
31+
public System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext>? OnLoadException { get { throw null; } set { } }
3232
public bool Optional { get { throw null; } set { } }
33-
public string Path { get { throw null; } set { } }
33+
[System.Diagnostics.CodeAnalysis.DisallowNull]
34+
public string? Path { get { throw null; } set { } }
3435
public int ReloadDelay { get { throw null; } set { } }
3536
public bool ReloadOnChange { get { throw null; } set { } }
3637
public abstract Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder);

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/ref/Microsoft.Extensions.Configuration.FileExtensions.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2+
23
<PropertyGroup>
34
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
5+
<Nullable>enable</Nullable>
46
</PropertyGroup>
57

68
<ItemGroup>

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public static IFileProvider GetFileProvider(this IConfigurationBuilder builder)
4343
throw new ArgumentNullException(nameof(builder));
4444
}
4545

46-
if (builder.Properties.TryGetValue(FileProviderKey, out object provider))
46+
if (builder.Properties.TryGetValue(FileProviderKey, out object? provider))
4747
{
48-
return provider as IFileProvider;
48+
return (IFileProvider)provider;
4949
}
5050

5151
return new PhysicalFileProvider(AppContext.BaseDirectory ?? string.Empty);
@@ -94,14 +94,14 @@ public static IConfigurationBuilder SetFileLoadExceptionHandler(this IConfigurat
9494
/// </summary>
9595
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
9696
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
97-
public static Action<FileLoadExceptionContext> GetFileLoadExceptionHandler(this IConfigurationBuilder builder)
97+
public static Action<FileLoadExceptionContext>? GetFileLoadExceptionHandler(this IConfigurationBuilder builder)
9898
{
9999
if (builder == null)
100100
{
101101
throw new ArgumentNullException(nameof(builder));
102102
}
103103

104-
if (builder.Properties.TryGetValue(FileLoadExceptionHandlerKey, out object handler))
104+
if (builder.Properties.TryGetValue(FileLoadExceptionHandlerKey, out object? handler))
105105
{
106106
return handler as Action<FileLoadExceptionContext>;
107107
}

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Microsoft.Extensions.Configuration
1717
/// </summary>
1818
public abstract class FileConfigurationProvider : ConfigurationProvider, IDisposable
1919
{
20-
private readonly IDisposable _changeTokenRegistration;
20+
private readonly IDisposable? _changeTokenRegistration;
2121

2222
/// <summary>
2323
/// Initializes a new instance with the specified source.
@@ -30,7 +30,7 @@ public FileConfigurationProvider(FileConfigurationSource source)
3030
if (Source.ReloadOnChange && Source.FileProvider != null)
3131
{
3232
_changeTokenRegistration = ChangeToken.OnChange(
33-
() => Source.FileProvider.Watch(Source.Path),
33+
() => Source.FileProvider.Watch(Source.Path!),
3434
() =>
3535
{
3636
Thread.Sleep(Source.ReloadDelay);
@@ -53,12 +53,12 @@ public override string ToString()
5353

5454
private void Load(bool reload)
5555
{
56-
IFileInfo file = Source.FileProvider?.GetFileInfo(Source.Path);
56+
IFileInfo? file = Source.FileProvider?.GetFileInfo(Source.Path ?? string.Empty);
5757
if (file == null || !file.Exists)
5858
{
5959
if (Source.Optional || reload) // Always optional on reload
6060
{
61-
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
61+
Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
6262
}
6363
else
6464
{
@@ -100,7 +100,7 @@ static Stream OpenRead(IFileInfo fileInfo)
100100
{
101101
if (reload)
102102
{
103-
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
103+
Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
104104
}
105105
var exception = new InvalidDataException(SR.Format(SR.Error_FailedToLoad, file.PhysicalPath), ex);
106106
HandleException(ExceptionDispatchInfo.Capture(exception));

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileConfigurationSource.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.IO;
67
using Microsoft.Extensions.FileProviders;
78

@@ -15,12 +16,13 @@ public abstract class FileConfigurationSource : IConfigurationSource
1516
/// <summary>
1617
/// Used to access the contents of the file.
1718
/// </summary>
18-
public IFileProvider FileProvider { get; set; }
19+
public IFileProvider? FileProvider { get; set; }
1920

2021
/// <summary>
2122
/// The path to the file.
2223
/// </summary>
23-
public string Path { get; set; }
24+
[DisallowNull]
25+
public string? Path { get; set; }
2426

2527
/// <summary>
2628
/// Determines if loading the file is optional.
@@ -41,7 +43,7 @@ public abstract class FileConfigurationSource : IConfigurationSource
4143
/// <summary>
4244
/// Will be called if an uncaught exception occurs in FileConfigurationProvider.Load.
4345
/// </summary>
44-
public Action<FileLoadExceptionContext> OnLoadException { get; set; }
46+
public Action<FileLoadExceptionContext>? OnLoadException { get; set; }
4547

4648
/// <summary>
4749
/// Builds the <see cref="IConfigurationProvider"/> for this source.
@@ -56,8 +58,8 @@ public abstract class FileConfigurationSource : IConfigurationSource
5658
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
5759
public void EnsureDefaults(IConfigurationBuilder builder)
5860
{
59-
FileProvider = FileProvider ?? builder.GetFileProvider();
60-
OnLoadException = OnLoadException ?? builder.GetFileLoadExceptionHandler();
61+
FileProvider ??= builder.GetFileProvider();
62+
OnLoadException ??= builder.GetFileLoadExceptionHandler();
6163
}
6264

6365
/// <summary>
@@ -70,8 +72,8 @@ public void ResolveFileProvider()
7072
!string.IsNullOrEmpty(Path) &&
7173
System.IO.Path.IsPathRooted(Path))
7274
{
73-
string directory = System.IO.Path.GetDirectoryName(Path);
74-
string pathToFile = System.IO.Path.GetFileName(Path);
75+
string? directory = System.IO.Path.GetDirectoryName(Path);
76+
string? pathToFile = System.IO.Path.GetFileName(Path);
7577
while (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
7678
{
7779
pathToFile = System.IO.Path.Combine(System.IO.Path.GetFileName(directory), pathToFile);
@@ -84,6 +86,5 @@ public void ResolveFileProvider()
8486
}
8587
}
8688
}
87-
8889
}
8990
}

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/FileLoadExceptionContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class FileLoadExceptionContext
1313
/// <summary>
1414
/// The <see cref="FileConfigurationProvider"/> that caused the exception.
1515
/// </summary>
16-
public FileConfigurationProvider Provider { get; set; }
16+
public FileConfigurationProvider Provider { get; set; } = null!;
1717

1818
/// <summary>
1919
/// The exception that occurred in Load.
2020
/// </summary>
21-
public Exception Exception { get; set; }
21+
public Exception Exception { get; set; } = null!;
2222

2323
/// <summary>
2424
/// If true, the exception will not be rethrown.

src/libraries/Microsoft.Extensions.Configuration.FileExtensions/src/Microsoft.Extensions.Configuration.FileExtensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
5+
<Nullable>enable</Nullable>
56
<EnableDefaultItems>true</EnableDefaultItems>
67
<!-- Use targeting pack references instead of granular ones in the project file. -->
78
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>

0 commit comments

Comments
 (0)