Skip to content

Commit 6033ef2

Browse files
[Group 3] Enable nullable annotations for Microsoft.Extensions.Configuration (#57414)
* Annotate src * Annotate ref * Add net6 to Configuration.Abstractions * Fix tests * DisableImplicitAssemblyReferences * TryGet can return null * new() * Fix merge error * InvalidOperationException * Source.Stream DisallowNull * Revert unnecessary changes * More message into resources * Configuration DisallowNull * MaybeNullWhen(false) * Revert "MaybeNullWhen(false)" This reverts commit 4813528. * Remove MaybeNullWhen * NotNullWhen Co-authored-by: Eric Erhardt <[email protected]>
1 parent 477789e commit 6033ef2

19 files changed

+82
-58
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,24 @@ public partial class ChainedConfigurationProvider : Microsoft.Extensions.Configu
1515
{
1616
public ChainedConfigurationProvider(Microsoft.Extensions.Configuration.ChainedConfigurationSource source) { }
1717
public void Dispose() { }
18-
public System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string parentPath) { throw null; }
18+
public System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string? parentPath) { throw null; }
1919
public Microsoft.Extensions.Primitives.IChangeToken GetReloadToken() { throw null; }
2020
public void Load() { }
21-
public void Set(string key, string value) { }
22-
public bool TryGet(string key, out string value) { throw null; }
21+
public void Set(string key, string? value) { }
22+
public bool TryGet(string key, out string? value) { throw null; }
2323
}
2424
public partial class ChainedConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
2525
{
2626
public ChainedConfigurationSource() { }
27-
public Microsoft.Extensions.Configuration.IConfiguration Configuration { get { throw null; } set { } }
27+
[System.Diagnostics.CodeAnalysis.DisallowNull]
28+
public Microsoft.Extensions.Configuration.IConfiguration? Configuration { get { throw null; } set { } }
2829
public bool ShouldDisposeConfiguration { get { throw null; } set { } }
2930
public Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
3031
}
3132
public sealed partial class ConfigurationManager : Microsoft.Extensions.Configuration.IConfigurationBuilder, Microsoft.Extensions.Configuration.IConfigurationRoot, System.IDisposable
3233
{
3334
public ConfigurationManager() { }
34-
public string this[string key] { get { throw null; } set { throw null; } }
35+
public string? this[string key] { get { throw null; } set { throw null; } }
3536
public IConfigurationSection GetSection(string key) { throw null; }
3637
public System.Collections.Generic.IEnumerable<IConfigurationSection> GetChildren() { throw null; }
3738
public void Dispose() { throw null; }
@@ -55,32 +56,32 @@ public partial class ConfigurationKeyComparer : System.Collections.Generic.IComp
5556
{
5657
public ConfigurationKeyComparer() { }
5758
public static Microsoft.Extensions.Configuration.ConfigurationKeyComparer Instance { get { throw null; } }
58-
public int Compare(string x, string y) { throw null; }
59+
public int Compare(string? x, string? y) { throw null; }
5960
}
6061
public abstract partial class ConfigurationProvider : Microsoft.Extensions.Configuration.IConfigurationProvider
6162
{
6263
protected ConfigurationProvider() { }
63-
protected System.Collections.Generic.IDictionary<string, string> Data { get { throw null; } set { } }
64-
public virtual System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string parentPath) { throw null; }
64+
protected System.Collections.Generic.IDictionary<string, string?> Data { get { throw null; } set { } }
65+
public virtual System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string? parentPath) { throw null; }
6566
public Microsoft.Extensions.Primitives.IChangeToken GetReloadToken() { throw null; }
6667
public virtual void Load() { }
6768
protected void OnReload() { }
68-
public virtual void Set(string key, string value) { }
69+
public virtual void Set(string key, string? value) { }
6970
public override string ToString() { throw null; }
70-
public virtual bool TryGet(string key, out string value) { throw null; }
71+
public virtual bool TryGet(string key, out string? value) { throw null; }
7172
}
7273
public partial class ConfigurationReloadToken : Microsoft.Extensions.Primitives.IChangeToken
7374
{
7475
public ConfigurationReloadToken() { }
7576
public bool ActiveChangeCallbacks { get { throw null; } }
7677
public bool HasChanged { get { throw null; } }
7778
public void OnReload() { }
78-
public System.IDisposable RegisterChangeCallback(System.Action<object> callback, object state) { throw null; }
79+
public System.IDisposable RegisterChangeCallback(System.Action<object?> callback, object? state) { throw null; }
7980
}
8081
public partial class ConfigurationRoot : Microsoft.Extensions.Configuration.IConfiguration, Microsoft.Extensions.Configuration.IConfigurationRoot, System.IDisposable
8182
{
8283
public ConfigurationRoot(System.Collections.Generic.IList<Microsoft.Extensions.Configuration.IConfigurationProvider> providers) { }
83-
public string this[string key] { get { throw null; } set { } }
84+
public string? this[string key] { get { throw null; } set { } }
8485
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationProvider> Providers { get { throw null; } }
8586
public void Dispose() { }
8687
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren() { throw null; }
@@ -91,18 +92,18 @@ public void Reload() { }
9192
public partial class ConfigurationSection : Microsoft.Extensions.Configuration.IConfiguration, Microsoft.Extensions.Configuration.IConfigurationSection
9293
{
9394
public ConfigurationSection(Microsoft.Extensions.Configuration.IConfigurationRoot root, string path) { }
94-
public string this[string key] { get { throw null; } set { } }
95+
public string? this[string key] { get { throw null; } set { } }
9596
public string Key { get { throw null; } }
9697
public string Path { get { throw null; } }
97-
public string Value { get { throw null; } set { } }
98+
public string? Value { get { throw null; } set { } }
9899
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren() { throw null; }
99100
public Microsoft.Extensions.Primitives.IChangeToken GetReloadToken() { throw null; }
100101
public Microsoft.Extensions.Configuration.IConfigurationSection GetSection(string key) { throw null; }
101102
}
102103
public static partial class MemoryConfigurationBuilderExtensions
103104
{
104105
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddInMemoryCollection(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder) { throw null; }
105-
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddInMemoryCollection(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> initialData) { throw null; }
106+
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddInMemoryCollection(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>>? initialData) { throw null; }
106107
}
107108
public abstract partial class StreamConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider
108109
{
@@ -114,23 +115,24 @@ public override void Load() { }
114115
public abstract partial class StreamConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
115116
{
116117
protected StreamConfigurationSource() { }
117-
public System.IO.Stream Stream { get { throw null; } set { } }
118+
[System.Diagnostics.CodeAnalysis.DisallowNull]
119+
public System.IO.Stream? Stream { get { throw null; } set { } }
118120
public abstract Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder);
119121
}
120122
}
121123
namespace Microsoft.Extensions.Configuration.Memory
122124
{
123-
public partial class MemoryConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Collections.IEnumerable
125+
public partial class MemoryConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>>, System.Collections.IEnumerable
124126
{
125127
public MemoryConfigurationProvider(Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource source) { }
126-
public void Add(string key, string value) { }
127-
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, string>> GetEnumerator() { throw null; }
128+
public void Add(string key, string? value) { }
129+
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, string?>> GetEnumerator() { throw null; }
128130
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
129131
}
130132
public partial class MemoryConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
131133
{
132134
public MemoryConfigurationSource() { }
133-
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> InitialData { get { throw null; } set { } }
135+
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>>? InitialData { get { throw null; } set { } }
134136
public Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
135137
}
136138
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
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.cs" />
78
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\ref\Microsoft.Extensions.Configuration.Abstractions.csproj" />
89
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj" />
910
</ItemGroup>
11+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
12+
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\ref\System.Collections.csproj" />
13+
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
14+
</ItemGroup>
1015
</Project>

src/libraries/Microsoft.Extensions.Configuration/src/ChainedConfigurationProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public ChainedConfigurationProvider(ChainedConfigurationSource source)
4040
/// <param name="key">The key.</param>
4141
/// <param name="value">The value.</param>
4242
/// <returns><c>True</c> if a value for the specified key was found, otherwise <c>false</c>.</returns>
43-
public bool TryGet(string key, out string value)
43+
public bool TryGet(string key, out string? value)
4444
{
4545
value = _config[key];
4646
return !string.IsNullOrEmpty(value);
@@ -51,7 +51,7 @@ public bool TryGet(string key, out string value)
5151
/// </summary>
5252
/// <param name="key">The key.</param>
5353
/// <param name="value">The value.</param>
54-
public void Set(string key, string value) => _config[key] = value;
54+
public void Set(string key, string? value) => _config[key] = value;
5555

5656
/// <summary>
5757
/// Returns a change token if this provider supports change tracking, null otherwise.
@@ -74,7 +74,7 @@ public void Load() { }
7474
/// <returns>The child keys.</returns>
7575
public IEnumerable<string> GetChildKeys(
7676
IEnumerable<string> earlierKeys,
77-
string parentPath)
77+
string? parentPath)
7878
{
7979
IConfiguration section = parentPath == null ? _config : _config.GetSection(parentPath);
8080
var keys = new List<string>();

src/libraries/Microsoft.Extensions.Configuration/src/ChainedConfigurationSource.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace Microsoft.Extensions.Configuration
57
{
68
/// <summary>
@@ -11,7 +13,8 @@ public class ChainedConfigurationSource : IConfigurationSource
1113
/// <summary>
1214
/// The chained configuration.
1315
/// </summary>
14-
public IConfiguration Configuration { get; set; }
16+
[DisallowNull]
17+
public IConfiguration? Configuration { get; set; }
1518

1619
/// <summary>
1720
/// Whether the chained configuration should be disposed when the

src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationKeyComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class ConfigurationKeyComparer : IComparer<string>
2727
/// <param name="x">First string.</param>
2828
/// <param name="y">Second string.</param>
2929
/// <returns>Less than 0 if x is less than y, 0 if x is equal to y and greater than 0 if x is greater than y.</returns>
30-
public int Compare(string x, string y)
30+
public int Compare(string? x, string? y)
3131
{
3232
string[] xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
3333
string[] yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();

src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Linq;
89
using System.Threading;
910
using Microsoft.Extensions.Configuration.Memory;
@@ -38,7 +39,7 @@ public ConfigurationManager()
3839
}
3940

4041
/// <inheritdoc/>
41-
public string this[string key]
42+
public string? this[string key]
4243
{
4344
get
4445
{
@@ -338,7 +339,7 @@ public bool Remove(KeyValuePair<string, object> item)
338339
return wasRemoved;
339340
}
340341

341-
public bool TryGetValue(string key, out object value)
342+
public bool TryGetValue(string key, [NotNullWhen(true)] out object? value)
342343
{
343344
return _properties.TryGetValue(key, out value);
344345
}

src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationProvider.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ public abstract class ConfigurationProvider : IConfigurationProvider
2121
/// </summary>
2222
protected ConfigurationProvider()
2323
{
24-
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
24+
Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
2525
}
2626

2727
/// <summary>
2828
/// The configuration key value pairs for this provider.
2929
/// </summary>
30-
protected IDictionary<string, string> Data { get; set; }
30+
protected IDictionary<string, string?> Data { get; set; }
3131

3232
/// <summary>
3333
/// Attempts to find a value with the given key, returns true if one is found, false otherwise.
3434
/// </summary>
3535
/// <param name="key">The key to lookup.</param>
3636
/// <param name="value">The value found at key if one is found.</param>
3737
/// <returns>True if key has a value, false otherwise.</returns>
38-
public virtual bool TryGet(string key, out string value)
38+
public virtual bool TryGet(string key, out string? value)
3939
=> Data.TryGetValue(key, out value);
4040

4141
/// <summary>
4242
/// Sets a value for a given key.
4343
/// </summary>
4444
/// <param name="key">The configuration key to set.</param>
4545
/// <param name="value">The value to set.</param>
46-
public virtual void Set(string key, string value)
46+
public virtual void Set(string key, string? value)
4747
=> Data[key] = value;
4848

4949
/// <summary>
@@ -60,13 +60,13 @@ public virtual void Load()
6060
/// <returns>The list of keys for this provider.</returns>
6161
public virtual IEnumerable<string> GetChildKeys(
6262
IEnumerable<string> earlierKeys,
63-
string parentPath)
63+
string? parentPath)
6464
{
6565
var results = new List<string>();
6666

6767
if (parentPath is null)
6868
{
69-
foreach (KeyValuePair<string, string> kv in Data)
69+
foreach (KeyValuePair<string, string?> kv in Data)
7070
{
7171
results.Add(Segment(kv.Key, 0));
7272
}
@@ -75,7 +75,7 @@ public virtual IEnumerable<string> GetChildKeys(
7575
{
7676
Debug.Assert(ConfigurationPath.KeyDelimiter == ":");
7777

78-
foreach (KeyValuePair<string, string> kv in Data)
78+
foreach (KeyValuePair<string, string?> kv in Data)
7979
{
8080
if (kv.Key.Length > parentPath.Length &&
8181
kv.Key.StartsWith(parentPath, StringComparison.OrdinalIgnoreCase) &&

src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationReloadToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class ConfigurationReloadToken : IChangeToken
3333
/// <param name="callback">The callback to invoke.</param>
3434
/// <param name="state">State to be passed into the callback.</param>
3535
/// <returns>The <see cref="CancellationToken"/> registration.</returns>
36-
public IDisposable RegisterChangeCallback(Action<object> callback, object state) => _cts.Token.Register(callback, state);
36+
public IDisposable RegisterChangeCallback(Action<object?> callback, object? state) => _cts.Token.Register(callback, state);
3737

3838
/// <summary>
3939
/// Used to trigger the change token when a reload occurs.

src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationRoot.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public ConfigurationRoot(IList<IConfigurationProvider> providers)
4747
/// </summary>
4848
/// <param name="key">The configuration key.</param>
4949
/// <returns>The configuration value.</returns>
50-
public string this[string key]
50+
public string? this[string key]
5151
{
5252
get => GetConfiguration(_providers, key);
5353
set => SetConfiguration(_providers, key, value);
@@ -111,13 +111,13 @@ public void Dispose()
111111
}
112112
}
113113

114-
internal static string GetConfiguration(IList<IConfigurationProvider> providers, string key)
114+
internal static string? GetConfiguration(IList<IConfigurationProvider> providers, string key)
115115
{
116116
for (int i = providers.Count - 1; i >= 0; i--)
117117
{
118118
IConfigurationProvider provider = providers[i];
119119

120-
if (provider.TryGet(key, out string value))
120+
if (provider.TryGet(key, out string? value))
121121
{
122122
return value;
123123
}
@@ -126,7 +126,7 @@ internal static string GetConfiguration(IList<IConfigurationProvider> providers,
126126
return null;
127127
}
128128

129-
internal static void SetConfiguration(IList<IConfigurationProvider> providers, string key, string value)
129+
internal static void SetConfiguration(IList<IConfigurationProvider> providers, string key, string? value)
130130
{
131131
if (providers.Count == 0)
132132
{

src/libraries/Microsoft.Extensions.Configuration/src/ConfigurationSection.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ConfigurationSection : IConfigurationSection
1414
{
1515
private readonly IConfigurationRoot _root;
1616
private readonly string _path;
17-
private string _key;
17+
private string? _key;
1818

1919
/// <summary>
2020
/// Initializes a new instance.
@@ -61,7 +61,7 @@ public string Key
6161
/// <summary>
6262
/// Gets or sets the section value.
6363
/// </summary>
64-
public string Value
64+
public string? Value
6565
{
6666
get
6767
{
@@ -78,13 +78,12 @@ public string Value
7878
/// </summary>
7979
/// <param name="key">The configuration key.</param>
8080
/// <returns>The configuration value.</returns>
81-
public string this[string key]
81+
public string? this[string key]
8282
{
8383
get
8484
{
8585
return _root[ConfigurationPath.Combine(Path, key)];
8686
}
87-
8887
set
8988
{
9089
_root[ConfigurationPath.Combine(Path, key)] = value;

0 commit comments

Comments
 (0)