Skip to content
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

[Group 4] Enable nullable annotations for Microsoft.Extensions.Configuration.FileExtensions #57434

Merged
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
2677495
Add net6 to parent projects
maxkoshevoi Aug 15, 2021
3500c3b
Annotate
maxkoshevoi Aug 15, 2021
df01b01
Update FileConfigurationProvider.cs
maxkoshevoi Aug 24, 2021
3083bef
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Aug 24, 2021
d328d7b
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Aug 26, 2021
9878964
DisableImplicitAssemblyReferences
maxkoshevoi Aug 26, 2021
88c4584
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Sep 8, 2021
d03a847
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Oct 6, 2021
bd79d32
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Oct 13, 2021
d85eaa3
Use compaund assigment
maxkoshevoi Oct 13, 2021
fdf9aa2
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Nov 18, 2021
8fa2aa3
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Nov 19, 2021
2ed875d
IFileInfo.Name is nullable
maxkoshevoi Nov 19, 2021
851c34f
DisallowNull
maxkoshevoi Nov 19, 2021
4cdc005
NetCoreAppMinimum
maxkoshevoi Nov 19, 2021
afdfb12
Fix NetCoreAppMinimum build
maxkoshevoi Nov 20, 2021
ff33bcf
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Dec 9, 2021
0801c30
Revert "IFileInfo.Name is nullable"
maxkoshevoi Dec 9, 2021
a576913
NotFoundFileInfo replaces null with ""
maxkoshevoi Dec 9, 2021
a06b504
New default constructor for FileLoadExceptionContext
maxkoshevoi Dec 9, 2021
fe6973d
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Dec 11, 2021
e4f24a2
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Jan 5, 2022
c5e8d9d
Revert "NotFoundFileInfo replaces null with """
maxkoshevoi Jan 5, 2022
146577c
Revert "New default constructor for FileLoadExceptionContext"
maxkoshevoi Jan 5, 2022
c2bef34
Suppress warning
maxkoshevoi Jan 5, 2022
a143589
Merge branch 'main' into mk/43605-Configuration-FileExtensions
maxkoshevoi Jan 7, 2022
5be2593
GetFileProvider non nullable return
maxkoshevoi Jan 7, 2022
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 @@ -8,8 +8,8 @@ namespace Microsoft.Extensions.Configuration
{
public static partial class FileConfigurationExtensions
{
public static System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext> GetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
public static Microsoft.Extensions.FileProviders.IFileProvider GetFileProvider(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
public static System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext>? GetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
public static Microsoft.Extensions.FileProviders.IFileProvider? GetFileProvider(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetBasePath(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, string basePath) { throw null; }
public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetFileLoadExceptionHandler(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext> handler) { throw null; }
halter73 marked this conversation as resolved.
Show resolved Hide resolved
public static Microsoft.Extensions.Configuration.IConfigurationBuilder SetFileProvider(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, Microsoft.Extensions.FileProviders.IFileProvider fileProvider) { throw null; }
Expand All @@ -27,10 +27,11 @@ public override void Load() { }
public abstract partial class FileConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
{
protected FileConfigurationSource() { }
public Microsoft.Extensions.FileProviders.IFileProvider FileProvider { get { throw null; } set { } }
public System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext> OnLoadException { get { throw null; } set { } }
public Microsoft.Extensions.FileProviders.IFileProvider? FileProvider { get { throw null; } set { } }
public System.Action<Microsoft.Extensions.Configuration.FileLoadExceptionContext>? OnLoadException { get { throw null; } set { } }
public bool Optional { get { throw null; } set { } }
public string Path { get { throw null; } set { } }
[System.Diagnostics.CodeAnalysis.DisallowNull]
public string? Path { get { throw null; } set { } }
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
public int ReloadDelay { get { throw null; } set { } }
public bool ReloadOnChange { get { throw null; } set { } }
public abstract Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder);
Expand All @@ -39,7 +40,7 @@ public void ResolveFileProvider() { }
}
public partial class FileLoadExceptionContext
{
public FileLoadExceptionContext() { }
public FileLoadExceptionContext(FileConfigurationProvider provider, System.Exception exception) { }
public System.Exception Exception { get { throw null; } set { } }
public bool Ignore { get { throw null; } set { } }
public Microsoft.Extensions.Configuration.FileConfigurationProvider Provider { get { throw null; } set { } }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public static IConfigurationBuilder SetFileProvider(this IConfigurationBuilder b
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>The default <see cref="IFileProvider"/>.</returns>
public static IFileProvider GetFileProvider(this IConfigurationBuilder builder)
public static IFileProvider? GetFileProvider(this IConfigurationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (builder.Properties.TryGetValue(FileProviderKey, out object provider))
if (builder.Properties.TryGetValue(FileProviderKey, out object? provider))
{
return provider as IFileProvider;
}
Expand Down Expand Up @@ -94,14 +94,14 @@ public static IConfigurationBuilder SetFileLoadExceptionHandler(this IConfigurat
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static Action<FileLoadExceptionContext> GetFileLoadExceptionHandler(this IConfigurationBuilder builder)
public static Action<FileLoadExceptionContext>? GetFileLoadExceptionHandler(this IConfigurationBuilder builder)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (builder.Properties.TryGetValue(FileLoadExceptionHandlerKey, out object handler))
if (builder.Properties.TryGetValue(FileLoadExceptionHandlerKey, out object? handler))
{
return handler as Action<FileLoadExceptionContext>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Extensions.Configuration
/// </summary>
public abstract class FileConfigurationProvider : ConfigurationProvider, IDisposable
{
private readonly IDisposable _changeTokenRegistration;
private readonly IDisposable? _changeTokenRegistration;

/// <summary>
/// Initializes a new instance with the specified source.
Expand All @@ -30,7 +30,7 @@ public FileConfigurationProvider(FileConfigurationSource source)
if (Source.ReloadOnChange && Source.FileProvider != null)
{
_changeTokenRegistration = ChangeToken.OnChange(
() => Source.FileProvider.Watch(Source.Path),
() => Source.FileProvider.Watch(Source.Path!),
() =>
{
Thread.Sleep(Source.ReloadDelay);
Expand All @@ -53,12 +53,12 @@ public override string ToString()

private void Load(bool reload)
{
IFileInfo file = Source.FileProvider?.GetFileInfo(Source.Path);
IFileInfo? file = Source.FileProvider?.GetFileInfo(Source.Path!);
if (file == null || !file.Exists)
{
if (Source.Optional || reload) // Always optional on reload
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
}
else
{
Expand Down Expand Up @@ -100,7 +100,7 @@ static Stream OpenRead(IFileInfo fileInfo)
{
if (reload)
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
}
var exception = new InvalidDataException(SR.Format(SR.Error_FailedToLoad, file.PhysicalPath), ex);
HandleException(ExceptionDispatchInfo.Capture(exception));
Expand Down Expand Up @@ -136,11 +136,7 @@ private void HandleException(ExceptionDispatchInfo info)
bool ignoreException = false;
if (Source.OnLoadException != null)
{
var exceptionContext = new FileLoadExceptionContext
{
Provider = this,
Exception = info.SourceException
};
var exceptionContext = new FileLoadExceptionContext(this, info.SourceException);
Source.OnLoadException.Invoke(exceptionContext);
ignoreException = exceptionContext.Ignore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using Microsoft.Extensions.FileProviders;

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

/// <summary>
/// The path to the file.
/// </summary>
public string Path { get; set; }
[DisallowNull]
public string? Path { get; set; }

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

/// <summary>
/// Builds the <see cref="IConfigurationProvider"/> for this source.
Expand All @@ -56,8 +58,8 @@ public abstract class FileConfigurationSource : IConfigurationSource
/// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param>
public void EnsureDefaults(IConfigurationBuilder builder)
{
FileProvider = FileProvider ?? builder.GetFileProvider();
OnLoadException = OnLoadException ?? builder.GetFileLoadExceptionHandler();
FileProvider ??= builder.GetFileProvider();
OnLoadException ??= builder.GetFileLoadExceptionHandler();
}

/// <summary>
Expand All @@ -70,8 +72,8 @@ public void ResolveFileProvider()
!string.IsNullOrEmpty(Path) &&
System.IO.Path.IsPathRooted(Path))
{
string directory = System.IO.Path.GetDirectoryName(Path);
string pathToFile = System.IO.Path.GetFileName(Path);
string? directory = System.IO.Path.GetDirectoryName(Path);
string? pathToFile = System.IO.Path.GetFileName(Path);
while (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
{
pathToFile = System.IO.Path.Combine(System.IO.Path.GetFileName(directory), pathToFile);
Expand All @@ -84,6 +86,5 @@ public void ResolveFileProvider()
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace Microsoft.Extensions.Configuration
/// </summary>
public class FileLoadExceptionContext
{
public FileLoadExceptionContext(FileConfigurationProvider provider, Exception exception)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
Provider = provider;
Exception = exception;
}

/// <summary>
/// The <see cref="FileConfigurationProvider"/> that caused the exception.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
<EnableDefaultItems>true</EnableDefaultItems>
<!-- Use targeting pack references instead of granular ones in the project file. -->
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public NotFoundDirectoryContents() { }
}
public partial class NotFoundFileInfo : Microsoft.Extensions.FileProviders.IFileInfo
{
public NotFoundFileInfo(string name) { }
public NotFoundFileInfo(string? name) { }
public bool Exists { get { throw null; } }
public bool IsDirectory { get { throw null; } }
public System.DateTimeOffset LastModified { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class NotFoundFileInfo : IFileInfo
/// Initializes an instance of <see cref="NotFoundFileInfo"/>.
/// </summary>
/// <param name="name">The name of the file that could not be found</param>
public NotFoundFileInfo(string name)
public NotFoundFileInfo(string? name)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
Name = name;
Name = name ?? string.Empty;
}

/// <summary>
Expand Down