Skip to content

Commit 1f555b2

Browse files
authored
[Group 2] Enable nullable annotations for Microsoft.Extensions.Configuration.Abstractions (#57401)
* Annotate src * Annotate ref * Fix FakeConfigurationProvider * Don't use using in ref * DisableImplicitAssemblyReferences * Update ConfigurationExtensions.cs * TryGet can return null * Remove not used namespace
1 parent 1c30027 commit 1f555b2

10 files changed

+45
-34
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace Microsoft.Extensions.Configuration
88
{
99
public static partial class ConfigurationExtensions
1010
{
11-
public static Microsoft.Extensions.Configuration.IConfigurationBuilder Add<TSource>(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<TSource> configureSource) where TSource : Microsoft.Extensions.Configuration.IConfigurationSource, new() { throw null; }
12-
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration) { throw null; }
13-
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration, bool makePathsRelative) { throw null; }
14-
public static bool Exists(this Microsoft.Extensions.Configuration.IConfigurationSection section) { throw null; }
15-
public static string GetConnectionString(this Microsoft.Extensions.Configuration.IConfiguration configuration, string name) { throw null; }
11+
public static Microsoft.Extensions.Configuration.IConfigurationBuilder Add<TSource>(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<TSource>? configureSource) where TSource : Microsoft.Extensions.Configuration.IConfigurationSource, new() { throw null; }
12+
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration) { throw null; }
13+
public static System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>> AsEnumerable(this Microsoft.Extensions.Configuration.IConfiguration configuration, bool makePathsRelative) { throw null; }
14+
public static bool Exists([System.Diagnostics.CodeAnalysis.NotNullWhen(true)] this Microsoft.Extensions.Configuration.IConfigurationSection? section) { throw null; }
15+
public static string? GetConnectionString(this Microsoft.Extensions.Configuration.IConfiguration configuration, string name) { throw null; }
1616
public static Microsoft.Extensions.Configuration.IConfigurationSection GetRequiredSection(this Microsoft.Extensions.Configuration.IConfiguration configuration, string key) { throw null; }
1717
}
1818
[System.AttributeUsageAttribute(System.AttributeTargets.Property)]
@@ -26,16 +26,17 @@ public static partial class ConfigurationPath
2626
public static readonly string KeyDelimiter;
2727
public static string Combine(System.Collections.Generic.IEnumerable<string> pathSegments) { throw null; }
2828
public static string Combine(params string[] pathSegments) { throw null; }
29-
public static string GetParentPath(string path) { throw null; }
30-
public static string GetSectionKey(string path) { throw null; }
29+
public static string? GetParentPath(string? path) { throw null; }
30+
[return: System.Diagnostics.CodeAnalysis.NotNullIfNotNull("path")]
31+
public static string? GetSectionKey(string? path) { throw null; }
3132
}
3233
public static partial class ConfigurationRootExtensions
3334
{
3435
public static string GetDebugView(this Microsoft.Extensions.Configuration.IConfigurationRoot root) { throw null; }
3536
}
3637
public partial interface IConfiguration
3738
{
38-
string this[string key] { get; set; }
39+
string? this[string key] { get; set; }
3940
System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren();
4041
Microsoft.Extensions.Primitives.IChangeToken GetReloadToken();
4142
Microsoft.Extensions.Configuration.IConfigurationSection GetSection(string key);
@@ -49,11 +50,11 @@ public partial interface IConfigurationBuilder
4950
}
5051
public partial interface IConfigurationProvider
5152
{
52-
System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string parentPath);
53+
System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string? parentPath);
5354
Microsoft.Extensions.Primitives.IChangeToken GetReloadToken();
5455
void Load();
55-
void Set(string key, string value);
56-
bool TryGet(string key, out string value);
56+
void Set(string key, string? value);
57+
bool TryGet(string key, out string? value);
5758
}
5859
public partial interface IConfigurationRoot : Microsoft.Extensions.Configuration.IConfiguration
5960
{
@@ -64,7 +65,7 @@ public partial interface IConfigurationSection : Microsoft.Extensions.Configurat
6465
{
6566
string Key { get; }
6667
string Path { get; }
67-
string Value { get; set; }
68+
string? Value { get; set; }
6869
}
6970
public partial interface IConfigurationSource
7071
{
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
3+
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
4+
<Nullable>enable</Nullable>
45
</PropertyGroup>
56
<ItemGroup>
67
<Compile Include="Microsoft.Extensions.Configuration.Abstractions.cs" />
78
</ItemGroup>
89
<ItemGroup>
910
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj" />
1011
</ItemGroup>
12+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
13+
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\ref\System.Collections.csproj" />
14+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
15+
</ItemGroup>
1116
</Project>

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/ConfigurationExtensions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Linq;
78

89
namespace Microsoft.Extensions.Configuration
@@ -18,7 +19,7 @@ public static class ConfigurationExtensions
1819
/// <param name="builder">The builder to add to.</param>
1920
/// <param name="configureSource">Configures the source secrets.</param>
2021
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
21-
public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder builder, Action<TSource> configureSource) where TSource : IConfigurationSource, new()
22+
public static IConfigurationBuilder Add<TSource>(this IConfigurationBuilder builder, Action<TSource>? configureSource) where TSource : IConfigurationSource, new()
2223
{
2324
var source = new TSource();
2425
configureSource?.Invoke(source);
@@ -31,37 +32,36 @@ public static class ConfigurationExtensions
3132
/// <param name="configuration">The configuration to enumerate.</param>
3233
/// <param name="name">The connection string key.</param>
3334
/// <returns>The connection string.</returns>
34-
public static string GetConnectionString(this IConfiguration configuration, string name)
35+
public static string? GetConnectionString(this IConfiguration configuration, string name)
3536
{
36-
return configuration?.GetSection("ConnectionStrings")?[name];
37+
return configuration?.GetSection("ConnectionStrings")[name];
3738
}
3839

3940
/// <summary>
4041
/// Get the enumeration of key value pairs within the <see cref="IConfiguration" />
4142
/// </summary>
4243
/// <param name="configuration">The configuration to enumerate.</param>
4344
/// <returns>An enumeration of key value pairs.</returns>
44-
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration) => configuration.AsEnumerable(makePathsRelative: false);
45+
public static IEnumerable<KeyValuePair<string, string?>> AsEnumerable(this IConfiguration configuration) => configuration.AsEnumerable(makePathsRelative: false);
4546

4647
/// <summary>
4748
/// Get the enumeration of key value pairs within the <see cref="IConfiguration" />
4849
/// </summary>
4950
/// <param name="configuration">The configuration to enumerate.</param>
5051
/// <param name="makePathsRelative">If true, the child keys returned will have the current configuration's Path trimmed from the front.</param>
5152
/// <returns>An enumeration of key value pairs.</returns>
52-
public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfiguration configuration, bool makePathsRelative)
53+
public static IEnumerable<KeyValuePair<string, string?>> AsEnumerable(this IConfiguration configuration, bool makePathsRelative)
5354
{
5455
var stack = new Stack<IConfiguration>();
5556
stack.Push(configuration);
56-
var rootSection = configuration as IConfigurationSection;
57-
int prefixLength = (makePathsRelative && rootSection != null) ? rootSection.Path.Length + 1 : 0;
57+
int prefixLength = (makePathsRelative && configuration is IConfigurationSection rootSection) ? rootSection.Path.Length + 1 : 0;
5858
while (stack.Count > 0)
5959
{
6060
IConfiguration config = stack.Pop();
6161
// Don't include the sections value if we are removing paths, since it will be an empty key
6262
if (config is IConfigurationSection section && (!makePathsRelative || config != configuration))
6363
{
64-
yield return new KeyValuePair<string, string>(section.Path.Substring(prefixLength), section.Value);
64+
yield return new KeyValuePair<string, string?>(section.Path.Substring(prefixLength), section.Value);
6565
}
6666
foreach (IConfigurationSection child in config.GetChildren())
6767
{
@@ -75,7 +75,7 @@ public static IEnumerable<KeyValuePair<string, string>> AsEnumerable(this IConfi
7575
/// </summary>
7676
/// <param name="section">The section to enumerate.</param>
7777
/// <returns><see langword="true" /> if the section has values or children; otherwise, <see langword="false" />.</returns>
78-
public static bool Exists(this IConfigurationSection section)
78+
public static bool Exists([NotNullWhen(true)] this IConfigurationSection? section)
7979
{
8080
if (section == null)
8181
{

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/ConfigurationPath.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics.CodeAnalysis;
67

78
namespace Microsoft.Extensions.Configuration
89
{
@@ -49,7 +50,8 @@ public static string Combine(IEnumerable<string> pathSegments)
4950
/// </summary>
5051
/// <param name="path">The path.</param>
5152
/// <returns>The last path segment of the path.</returns>
52-
public static string GetSectionKey(string path)
53+
[return: NotNullIfNotNull("path")]
54+
public static string? GetSectionKey(string? path)
5355
{
5456
if (string.IsNullOrEmpty(path))
5557
{
@@ -65,7 +67,7 @@ public static string GetSectionKey(string path)
6567
/// </summary>
6668
/// <param name="path">The path.</param>
6769
/// <returns>The original path minus the last individual segment found in it. Null if the original path corresponds to a top level node.</returns>
68-
public static string GetParentPath(string path)
70+
public static string? GetParentPath(string? path)
6971
{
7072
if (string.IsNullOrEmpty(path))
7173
{

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/ConfigurationRootExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void RecurseChildren(
2525
{
2626
foreach (IConfigurationSection child in children)
2727
{
28-
(string Value, IConfigurationProvider Provider) valueAndProvider = GetValueAndProvider(root, child.Path);
28+
(string? Value, IConfigurationProvider? Provider) valueAndProvider = GetValueAndProvider(root, child.Path);
2929

3030
if (valueAndProvider.Provider != null)
3131
{
@@ -57,13 +57,13 @@ void RecurseChildren(
5757
return builder.ToString();
5858
}
5959

60-
private static (string Value, IConfigurationProvider Provider) GetValueAndProvider(
60+
private static (string? Value, IConfigurationProvider? Provider) GetValueAndProvider(
6161
IConfigurationRoot root,
6262
string key)
6363
{
6464
foreach (IConfigurationProvider provider in root.Providers.Reverse())
6565
{
66-
if (provider.TryGet(key, out string value))
66+
if (provider.TryGet(key, out string? value))
6767
{
6868
return (value, provider);
6969
}

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/IConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IConfiguration
1616
/// </summary>
1717
/// <param name="key">The configuration key.</param>
1818
/// <returns>The configuration value.</returns>
19-
string this[string key] { get; set; }
19+
string? this[string key] { get; set; }
2020

2121
/// <summary>
2222
/// Gets a configuration sub-section with the specified key.

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/IConfigurationProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public interface IConfigurationProvider
1717
/// <param name="key">The key.</param>
1818
/// <param name="value">The value.</param>
1919
/// <returns><c>True</c> if a value for the specified key was found, otherwise <c>false</c>.</returns>
20-
bool TryGet(string key, out string value);
20+
bool TryGet(string key, out string? value);
2121

2222
/// <summary>
2323
/// Sets a configuration value for the specified key.
2424
/// </summary>
2525
/// <param name="key">The key.</param>
2626
/// <param name="value">The value.</param>
27-
void Set(string key, string value);
27+
void Set(string key, string? value);
2828

2929
/// <summary>
3030
/// Returns a change token if this provider supports change tracking, null otherwise.
@@ -45,6 +45,6 @@ public interface IConfigurationProvider
4545
/// <param name="earlierKeys">The child keys returned by the preceding providers for the same parent path.</param>
4646
/// <param name="parentPath">The parent path.</param>
4747
/// <returns>The child keys.</returns>
48-
IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath);
48+
IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string? parentPath);
4949
}
5050
}

src/libraries/Microsoft.Extensions.Configuration.Abstractions/src/IConfigurationSection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public interface IConfigurationSection : IConfiguration
2121
/// <summary>
2222
/// Gets or sets the section value.
2323
/// </summary>
24-
string Value { get; set; }
24+
string? Value { get; set; }
2525
}
2626
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
4+
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
5+
<Nullable>enable</Nullable>
56
<EnableDefaultItems>true</EnableDefaultItems>
7+
<!-- Use targeting pack references instead of granular ones in the project file. -->
8+
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
69
<PackageDescription>Abstractions of key-value pair based configuration.
710

811
Commonly Used Types:

src/libraries/Microsoft.Extensions.Options.ConfigurationExtensions/tests/FakeConfigurationProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal class FakeConfigurationProvider : MemoryConfigurationProvider, IConfigu
1111
public FakeConfigurationProvider(MemoryConfigurationSource source)
1212
: base(source) { }
1313

14-
public new void Set(string key, string value)
14+
public new void Set(string key, string? value)
1515
{
1616
base.Set(key, value);
1717
OnReload();

0 commit comments

Comments
 (0)