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 3] Enable nullable annotations for Microsoft.Extensions.Configuration #57414

Merged
merged 24 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ad2ea3d
Annotate src
maxkoshevoi Aug 14, 2021
04bc30b
Annotate ref
maxkoshevoi Aug 14, 2021
65f47b2
Add net6 to Configuration.Abstractions
maxkoshevoi Aug 14, 2021
44d0ac0
Fix tests
maxkoshevoi Aug 14, 2021
1226d7e
Merge branch 'main' into mk/43605-Configuration
maxkoshevoi Aug 15, 2021
07cc6a8
Merge branch 'main' into mk/43605-Configuration
maxkoshevoi Aug 15, 2021
802a753
Merge branch 'main' into mk/43605-Configuration
maxkoshevoi Aug 16, 2021
051b15b
Merge branch 'main' into mk/43605-Configuration
maxkoshevoi Aug 26, 2021
5a7a6d3
DisableImplicitAssemblyReferences
maxkoshevoi Aug 26, 2021
9bde07c
Merge branch 'main' into mk/43605-Configuration
maxkoshevoi Sep 8, 2021
4945112
TryGet can return null
maxkoshevoi Sep 16, 2021
330dc3a
Merge branch 'main' into mk/43605-Configuration
maxkoshevoi Sep 16, 2021
f320ed0
new()
maxkoshevoi Sep 16, 2021
7cbd323
Merge remote-tracking branch 'upstream/main' into mk/43605-Configuration
eerhardt Oct 6, 2021
b9a0a93
Fix merge error
eerhardt Oct 6, 2021
e5228aa
InvalidOperationException
maxkoshevoi Oct 7, 2021
1c583ec
Source.Stream DisallowNull
maxkoshevoi Oct 7, 2021
a45a821
Revert unnecessary changes
maxkoshevoi Oct 8, 2021
d33438a
More message into resources
maxkoshevoi Oct 8, 2021
2ca692a
Configuration DisallowNull
maxkoshevoi Oct 8, 2021
4813528
MaybeNullWhen(false)
maxkoshevoi Oct 8, 2021
581c024
Revert "MaybeNullWhen(false)"
maxkoshevoi Oct 8, 2021
ff9af93
Remove MaybeNullWhen
maxkoshevoi Oct 11, 2021
3790ba6
NotNullWhen
maxkoshevoi Oct 11, 2021
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 @@ -15,23 +15,24 @@ public partial class ChainedConfigurationProvider : Microsoft.Extensions.Configu
{
public ChainedConfigurationProvider(Microsoft.Extensions.Configuration.ChainedConfigurationSource source) { }
public void Dispose() { }
public System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string parentPath) { throw null; }
public System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string? parentPath) { throw null; }
public Microsoft.Extensions.Primitives.IChangeToken GetReloadToken() { throw null; }
public void Load() { }
public void Set(string key, string value) { }
public bool TryGet(string key, out string value) { throw null; }
public void Set(string key, string? value) { }
public bool TryGet(string key, [System.Diagnostics.CodeAnalysis.MaybeNullWhen(false)] out string value) { throw null; }
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
}
public partial class ChainedConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
{
public ChainedConfigurationSource() { }
public Microsoft.Extensions.Configuration.IConfiguration Configuration { get { throw null; } set { } }
[System.Diagnostics.CodeAnalysis.DisallowNull]
public Microsoft.Extensions.Configuration.IConfiguration? Configuration { get { throw null; } set { } }
public bool ShouldDisposeConfiguration { get { throw null; } set { } }
public Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
}
public sealed partial class ConfigurationManager : Microsoft.Extensions.Configuration.IConfigurationBuilder, Microsoft.Extensions.Configuration.IConfigurationRoot, System.IDisposable
{
public ConfigurationManager() { }
public string this[string key] { get { throw null; } set { throw null; } }
public string? this[string key] { get { throw null; } set { throw null; } }
public IConfigurationSection GetSection(string key) { throw null; }
public System.Collections.Generic.IEnumerable<IConfigurationSection> GetChildren() { throw null; }
public void Dispose() { throw null; }
Expand All @@ -55,32 +56,32 @@ public partial class ConfigurationKeyComparer : System.Collections.Generic.IComp
{
public ConfigurationKeyComparer() { }
public static Microsoft.Extensions.Configuration.ConfigurationKeyComparer Instance { get { throw null; } }
public int Compare(string x, string y) { throw null; }
public int Compare(string? x, string? y) { throw null; }
}
public abstract partial class ConfigurationProvider : Microsoft.Extensions.Configuration.IConfigurationProvider
{
protected ConfigurationProvider() { }
protected System.Collections.Generic.IDictionary<string, string> Data { get { throw null; } set { } }
public virtual System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string parentPath) { throw null; }
protected System.Collections.Generic.IDictionary<string, string?> Data { get { throw null; } set { } }
public virtual System.Collections.Generic.IEnumerable<string> GetChildKeys(System.Collections.Generic.IEnumerable<string> earlierKeys, string? parentPath) { throw null; }
public Microsoft.Extensions.Primitives.IChangeToken GetReloadToken() { throw null; }
public virtual void Load() { }
protected void OnReload() { }
public virtual void Set(string key, string value) { }
public virtual void Set(string key, string? value) { }
public override string ToString() { throw null; }
public virtual bool TryGet(string key, out string value) { throw null; }
public virtual bool TryGet(string key, out string? value) { throw null; }
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
}
public partial class ConfigurationReloadToken : Microsoft.Extensions.Primitives.IChangeToken
{
public ConfigurationReloadToken() { }
public bool ActiveChangeCallbacks { get { throw null; } }
public bool HasChanged { get { throw null; } }
public void OnReload() { }
public System.IDisposable RegisterChangeCallback(System.Action<object> callback, object state) { throw null; }
public System.IDisposable RegisterChangeCallback(System.Action<object?> callback, object? state) { throw null; }
}
public partial class ConfigurationRoot : Microsoft.Extensions.Configuration.IConfiguration, Microsoft.Extensions.Configuration.IConfigurationRoot, System.IDisposable
{
public ConfigurationRoot(System.Collections.Generic.IList<Microsoft.Extensions.Configuration.IConfigurationProvider> providers) { }
public string this[string key] { get { throw null; } set { } }
public string? this[string key] { get { throw null; } set { } }
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationProvider> Providers { get { throw null; } }
public void Dispose() { }
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren() { throw null; }
Expand All @@ -91,18 +92,18 @@ public void Reload() { }
public partial class ConfigurationSection : Microsoft.Extensions.Configuration.IConfiguration, Microsoft.Extensions.Configuration.IConfigurationSection
{
public ConfigurationSection(Microsoft.Extensions.Configuration.IConfigurationRoot root, string path) { }
public string this[string key] { get { throw null; } set { } }
public string? this[string key] { get { throw null; } set { } }
public string Key { get { throw null; } }
public string Path { get { throw null; } }
public string Value { get { throw null; } set { } }
public string? Value { get { throw null; } set { } }
public System.Collections.Generic.IEnumerable<Microsoft.Extensions.Configuration.IConfigurationSection> GetChildren() { throw null; }
public Microsoft.Extensions.Primitives.IChangeToken GetReloadToken() { throw null; }
public Microsoft.Extensions.Configuration.IConfigurationSection GetSection(string key) { throw null; }
}
public static partial class MemoryConfigurationBuilderExtensions
{
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddInMemoryCollection(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder) { throw null; }
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; }
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; }
}
public abstract partial class StreamConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider
{
Expand All @@ -114,23 +115,24 @@ public override void Load() { }
public abstract partial class StreamConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
{
protected StreamConfigurationSource() { }
public System.IO.Stream Stream { get { throw null; } set { } }
[System.Diagnostics.CodeAnalysis.DisallowNull]
public System.IO.Stream? Stream { get { throw null; } set { } }
public abstract Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder);
}
}
namespace Microsoft.Extensions.Configuration.Memory
{
public partial class MemoryConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>>, System.Collections.IEnumerable
public partial class MemoryConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>>, System.Collections.IEnumerable
{
public MemoryConfigurationProvider(Microsoft.Extensions.Configuration.Memory.MemoryConfigurationSource source) { }
public void Add(string key, string value) { }
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, string>> GetEnumerator() { throw null; }
public void Add(string key, string? value) { }
public System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<string, string?>> GetEnumerator() { throw null; }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; }
}
public partial class MemoryConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
{
public MemoryConfigurationSource() { }
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string>> InitialData { get { throw null; } set { } }
public System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, string?>>? InitialData { get { throw null; } set { } }
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
public Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Compile Include="Microsoft.Extensions.Configuration.cs" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\ref\Microsoft.Extensions.Configuration.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Primitives\ref\Microsoft.Extensions.Primitives.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Collections\ref\System.Collections.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Primitives;

namespace Microsoft.Extensions.Configuration
Expand Down Expand Up @@ -40,7 +41,7 @@ public ChainedConfigurationProvider(ChainedConfigurationSource source)
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns><c>True</c> if a value for the specified key was found, otherwise <c>false</c>.</returns>
public bool TryGet(string key, out string value)
public bool TryGet(string key, [MaybeNullWhen(false)] out string value)
{
value = _config[key];
return !string.IsNullOrEmpty(value);
Expand All @@ -51,7 +52,7 @@ public bool TryGet(string key, out string value)
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void Set(string key, string value) => _config[key] = value;
public void Set(string key, string? value) => _config[key] = value;

/// <summary>
/// Returns a change token if this provider supports change tracking, null otherwise.
Expand All @@ -74,7 +75,7 @@ public void Load() { }
/// <returns>The child keys.</returns>
public IEnumerable<string> GetChildKeys(
IEnumerable<string> earlierKeys,
string parentPath)
string? parentPath)
{
IConfiguration section = parentPath == null ? _config : _config.GetSection(parentPath);
var keys = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Extensions.Configuration
{
/// <summary>
Expand All @@ -11,7 +13,8 @@ public class ChainedConfigurationSource : IConfigurationSource
/// <summary>
/// The chained configuration.
/// </summary>
public IConfiguration Configuration { get; set; }
[DisallowNull]
public IConfiguration? Configuration { get; set; }
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Whether the chained configuration should be disposed when the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ConfigurationKeyComparer : IComparer<string>
/// <param name="x">First string.</param>
/// <param name="y">Second string.</param>
/// <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>
public int Compare(string x, string y)
public int Compare(string? x, string? y)
{
string[] xParts = x?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
string[] yParts = y?.Split(_keyDelimiterArray, StringSplitOptions.RemoveEmptyEntries) ?? Array.Empty<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Configuration.Memory;
Expand Down Expand Up @@ -38,7 +39,7 @@ public ConfigurationManager()
}

/// <inheritdoc/>
public string this[string key]
public string? this[string key]
{
get
{
Expand Down Expand Up @@ -338,7 +339,7 @@ public bool Remove(KeyValuePair<string, object> item)
return wasRemoved;
}

public bool TryGetValue(string key, out object value)
public bool TryGetValue(string key, [MaybeNullWhen(false)] out object value)
maxkoshevoi marked this conversation as resolved.
Show resolved Hide resolved
{
return _properties.TryGetValue(key, out value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,29 @@ public abstract class ConfigurationProvider : IConfigurationProvider
/// </summary>
protected ConfigurationProvider()
{
Data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// The configuration key value pairs for this provider.
/// </summary>
protected IDictionary<string, string> Data { get; set; }
protected IDictionary<string, string?> Data { get; set; }

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

/// <summary>
/// Sets a value for a given key.
/// </summary>
/// <param name="key">The configuration key to set.</param>
/// <param name="value">The value to set.</param>
public virtual void Set(string key, string value)
public virtual void Set(string key, string? value)
=> Data[key] = value;

/// <summary>
Expand All @@ -60,13 +60,13 @@ public virtual void Load()
/// <returns>The list of keys for this provider.</returns>
public virtual IEnumerable<string> GetChildKeys(
IEnumerable<string> earlierKeys,
string parentPath)
string? parentPath)
{
var results = new List<string>();

if (parentPath is null)
{
foreach (KeyValuePair<string, string> kv in Data)
foreach (KeyValuePair<string, string?> kv in Data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: not related to this PR but it seems like we could just iterate over Data.Keys rather than KVP? We are just using the keys on these two for loops.

Copy link
Contributor Author

@maxkoshevoi maxkoshevoi Oct 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eerhardt is against non-nullable-related changes in nullable PRs. I've already reverted some of them a45a821.

{
results.Add(Segment(kv.Key, 0));
}
Expand All @@ -75,7 +75,7 @@ public virtual IEnumerable<string> GetChildKeys(
{
Debug.Assert(ConfigurationPath.KeyDelimiter == ":");

foreach (KeyValuePair<string, string> kv in Data)
foreach (KeyValuePair<string, string?> kv in Data)
{
if (kv.Key.Length > parentPath.Length &&
kv.Key.StartsWith(parentPath, StringComparison.OrdinalIgnoreCase) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ConfigurationReloadToken : IChangeToken
/// <param name="callback">The callback to invoke.</param>
/// <param name="state">State to be passed into the callback.</param>
/// <returns>The <see cref="CancellationToken"/> registration.</returns>
public IDisposable RegisterChangeCallback(Action<object> callback, object state) => _cts.Token.Register(callback, state);
public IDisposable RegisterChangeCallback(Action<object?> callback, object? state) => _cts.Token.Register(callback, state);

/// <summary>
/// Used to trigger the change token when a reload occurs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public ConfigurationRoot(IList<IConfigurationProvider> providers)
/// </summary>
/// <param name="key">The configuration key.</param>
/// <returns>The configuration value.</returns>
public string this[string key]
public string? this[string key]
{
get => GetConfiguration(_providers, key);
set => SetConfiguration(_providers, key, value);
Expand Down Expand Up @@ -111,13 +111,13 @@ public void Dispose()
}
}

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

if (provider.TryGet(key, out string value))
if (provider.TryGet(key, out string? value))
{
return value;
}
Expand All @@ -126,7 +126,7 @@ internal static string GetConfiguration(IList<IConfigurationProvider> providers,
return null;
}

internal static void SetConfiguration(IList<IConfigurationProvider> providers, string key, string value)
internal static void SetConfiguration(IList<IConfigurationProvider> providers, string key, string? value)
{
if (providers.Count == 0)
{
Expand Down
Loading