Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class ExceptionMessages
public const string CannotBindToConstructorParameter = "Cannot create instance of type '{0}' because one or more parameters cannot be bound to. Constructor parameters cannot be declared as in, out, or ref. Invalid parameters are: '{1}'";
public const string CannotSpecifyBindNonPublicProperties = "The configuration binding source generator does not support 'BinderOptions.BindNonPublicProperties'.";
public const string ConstructorParametersDoNotMatchProperties = "Cannot create instance of type '{0}' because one or more parameters cannot be bound to. Constructor parameters must have corresponding properties. Fields are not supported. Missing properties are: '{1}'";
public const string FailedBinding = "Failed to convert configuration value at '{0}' to type '{1}'.";
public const string FailedBinding = "Failed to convert configuration value '{0}' at '{1}' to type '{2}'.";
public const string MissingConfig = "'{0}' was set on the provided {1}, but the following properties were not found on the instance of {2}: {3}";
public const string MissingPublicInstanceConstructor = "Cannot create instance of type '{0}' because it is missing a public instance constructor.";
public const string MultipleParameterizedConstructors = "Cannot create instance of type '{0}' because it has multiple public parameterized constructors.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private static class Identifier
public const string HasValue = nameof(HasValue);
public const string IConfiguration = nameof(IConfiguration);
public const string IConfigurationSection = nameof(IConfigurationSection);
public const string ConfigurationSection = nameof(ConfigurationSection);
public const string Int32 = "int";
public const string InterceptsLocation = nameof(InterceptsLocation);
public const string InvalidOperationException = nameof(InvalidOperationException);
Expand All @@ -133,6 +134,7 @@ private static class Identifier
public const string Type = nameof(Type);
public const string Uri = nameof(Uri);
public const string ValidateConfigurationKeys = nameof(ValidateConfigurationKeys);
public const string TryGetConfigurationValue = nameof(TryGetConfigurationValue);
public const string Value = nameof(Value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ private static void BindProperty(PropertyInfo property, object instance, IConfig
// For property binding, there are some cases when HasNewValue is not set in BindingPoint while a non-null Value inside that object can be retrieved from the property getter.
// As example, when binding a property which not having a configuration entry matching this property and the getter can initialize the Value.
// It is important to call the property setter as the setters can have a logic adjusting the Value.
if (!propertyBindingPoint.IsReadOnly && propertyBindingPoint.Value is not null)
// Otherwise, if the HasNewValue set to true, it means that the property setter should be called anyway as encountering a new value.
if (!propertyBindingPoint.IsReadOnly && (propertyBindingPoint.Value is not null || propertyBindingPoint.HasNewValue))
{
property.SetValue(instance, propertyBindingPoint.Value);
}
Expand All @@ -338,15 +339,41 @@ private static void BindInstance(
return;
}

var section = config as IConfigurationSection;
string? configValue = section?.Value;
if (configValue != null && TryConvertValue(type, configValue, section?.Path, out object? convertedValue, out Exception? error))
IConfigurationSection? section;
string? configValue;
bool isConfigurationExist;

if (config is ConfigurationSection configSection)
{
section = configSection;
isConfigurationExist = configSection.TryGetValue(key:null, out configValue);
}
else
{
section = config as IConfigurationSection;
configValue = section?.Value;
isConfigurationExist = configValue != null;
}

if (isConfigurationExist && TryConvertValue(type, configValue, section?.Path, out object? convertedValue, out Exception? error))
{
if (error != null)
{
throw error;
}

if (type == typeof(byte[]) && bindingPoint.Value is byte[] byteArray && byteArray.Length > 0)
{
if (convertedValue is byte[] convertedByteArray && convertedByteArray.Length > 0)
{
Array a = Array.CreateInstance(type.GetElementType()!, byteArray.Length + convertedByteArray.Length);
Array.Copy(byteArray, a, byteArray.Length);
Array.Copy(convertedByteArray, 0, a, byteArray.Length, convertedByteArray.Length);
bindingPoint.TrySetValue(a);
}
return;
}

// Leaf nodes are always reinitialized
bindingPoint.TrySetValue(convertedValue);
return;
Expand Down Expand Up @@ -476,13 +503,29 @@ private static void BindInstance(
if (options.ErrorOnUnknownConfiguration)
{
Debug.Assert(section is not null);
throw new InvalidOperationException(SR.Format(SR.Error_FailedBinding, section.Path, type));
throw new InvalidOperationException(SR.Format(SR.Error_FailedBinding, configValue, section.Path, type));
}
}
else if (isParentCollection && bindingPoint.Value is null)
else
{
// Try to create the default instance of the type
bindingPoint.TrySetValue(CreateInstance(type, config, options, out _));
if (isParentCollection && bindingPoint.Value is null)
{
// Try to create the default instance of the type
bindingPoint.TrySetValue(CreateInstance(type, config, options, out _));
}
else if (isConfigurationExist && bindingPoint.Value is null)
{
// Don't override the existing array in bindingPoint.Value if it is already set.
if (type.IsArray || IsImmutableArrayCompatibleInterface(type))
{
// When having configuration value set to empty string, we create an empty array
bindingPoint.TrySetValue(configValue is null ? null : Array.CreateInstance(type.GetElementType()!, 0));
}
else
{
bindingPoint.TrySetValue(bindingPoint.Value); // force setting null value
}
}
}
}
}
Expand Down Expand Up @@ -930,7 +973,7 @@ private static Array BindArray(Type type, IEnumerable? source, IConfiguration co
private static bool TryConvertValue(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
Type type,
string value, string? path, out object? result, out Exception? error)
string? value, string? path, out object? result, out Exception? error)
{
error = null;
result = null;
Expand All @@ -954,11 +997,14 @@ private static bool TryConvertValue(
{
try
{
result = converter.ConvertFromInvariantString(value);
if (value is not null)
{
result = converter.ConvertFromInvariantString(value);
}
}
catch (Exception ex)
{
error = new InvalidOperationException(SR.Format(SR.Error_FailedBinding, path, type), ex);
error = new InvalidOperationException(SR.Format(SR.Error_FailedBinding, value, path, type), ex);
}
return true;
}
Expand All @@ -967,11 +1013,14 @@ private static bool TryConvertValue(
{
try
{
result = Convert.FromBase64String(value);
if (value is not null )
{
result = value == string.Empty ? Array.Empty<byte>() : Convert.FromBase64String(value);
}
}
catch (FormatException ex)
{
error = new InvalidOperationException(SR.Format(SR.Error_FailedBinding, path, type), ex);
error = new InvalidOperationException(SR.Format(SR.Error_FailedBinding, value, path, type), ex);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\src\Microsoft.Extensions.Configuration.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration\src\Microsoft.Extensions.Configuration.csproj" />
<ProjectReference Include="..\gen\Microsoft.Extensions.Configuration.Binder.SourceGeneration.csproj"
ReferenceOutputAssembly="false"
PackAsAnalyzer="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
<!--
Microsoft ResX Schema

Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple

There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -127,7 +127,7 @@
<value>Cannot create instance of type '{0}' because one or more parameters cannot be bound to. Constructor parameters must have corresponding properties. Fields are not supported. Missing properties are: '{1}'</value>
</data>
<data name="Error_FailedBinding" xml:space="preserve">
<value>Failed to convert configuration value at '{0}' to type '{1}'.</value>
<value>Failed to convert configuration value '{0}' at '{1}' to type '{2}'.</value>
</data>
<data name="Error_FailedToActivate" xml:space="preserve">
<value>Failed to create instance of type '{0}'.</value>
Expand All @@ -153,4 +153,4 @@
<data name="Error_UnsupportedMultidimensionalArray" xml:space="preserve">
<value>Cannot create instance of type '{0}' because multidimensional arrays are not supported.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void GetListNullValues()
var list = new List<string>();
config.GetSection("StringList").Bind(list);

Assert.Empty(list);
Assert.Equal([ null, null, null, null ], list);
}

[ConditionalFact(typeof(TestHelpers), nameof(TestHelpers.NotSourceGenMode))] // Ensure exception messages are in sync
Expand Down Expand Up @@ -2182,9 +2182,10 @@ public void CanBindInstantiatedIEnumerableWithNullItems()

var options = config.Get<ComplexOptions>()!;

Assert.Equal(2, options.InstantiatedIEnumerable.Count());
Assert.Equal("Yo1", options.InstantiatedIEnumerable.ElementAt(0));
Assert.Equal("Yo2", options.InstantiatedIEnumerable.ElementAt(1));
Assert.Equal(3, options.InstantiatedIEnumerable.Count());
Assert.Null(options.InstantiatedIEnumerable.ElementAt(0));
Assert.Equal("Yo1", options.InstantiatedIEnumerable.ElementAt(1));
Assert.Equal("Yo2", options.InstantiatedIEnumerable.ElementAt(2));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1147,5 +1147,36 @@ public enum MyValue
Value2,
Value3
}

public class NullConfiguration
{
public NullConfiguration()
{
// Initialize with non-default value to ensure binding will override these values
StringProperty1 = "Initial Value 1";
StringProperty2 = "Initial Value 2";
StringProperty3 = "Initial Value 3";

IntProperty1 = 123;
IntProperty2 = 456;
}
public string? StringProperty1 { get; set; }
public string? StringProperty2 { get; set; }
public string? StringProperty3 { get; set; }

public int? IntProperty1 { get; set; }
public int? IntProperty2 { get; set; }
}

public class ArraysContainer
{
public string[] StringArray1 { get; set; }
public string[] StringArray2 { get; set; }
public string[] StringArray3 { get; set; }

public byte[] ByteArray1 { get; set; }
public byte[] ByteArray2 { get; set; }
public byte[] ByteArray3 { get; set; }
}
}
}
Loading
Loading