From 4dcec3544ea4010a68ef76c2bd6e2a3815d2d90f Mon Sep 17 00:00:00 2001 From: Thomas Levesque <1864281+thomaslevesque@users.noreply.github.com> Date: Thu, 15 Oct 2020 16:33:14 +0200 Subject: [PATCH] Bind byte array from base64 config value (#43150) * Bind byte array from base64 config value * Add test case for failure * Add test for null case * Remove unnecessary null check Co-authored-by: Thomas Levesque --- .../src/ConfigurationBinder.cs | 13 ++++++ .../tests/ConfigurationBinderTests.cs | 54 ++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs index 199096a..171a8df 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/src/ConfigurationBinder.cs @@ -520,6 +520,19 @@ namespace Microsoft.Extensions.Configuration return true; } + if (type == typeof(byte[])) + { + try + { + result = Convert.FromBase64String(value); + } + catch (FormatException ex) + { + error = new InvalidOperationException(SR.Format(SR.Error_FailedBinding, path, type), ex); + } + return true; + } + return false; } diff --git a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs index d8fb3cf..87db0c9 100644 --- a/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs +++ b/src/libraries/Microsoft.Extensions.Configuration.Binder/tests/ConfigurationBinderTests.cs @@ -109,6 +109,11 @@ namespace Microsoft.Extensions.Configuration.Binder.Test public string MyString { get; set; } } + public class ByteArrayOptions + { + public byte[] MyByteArray { get; set; } + } + [Fact] public void CanBindIConfigurationSection() { @@ -812,6 +817,55 @@ namespace Microsoft.Extensions.Configuration.Binder.Test Assert.Equal("hello world", options.MyString); } + [Fact] + public void CanBindByteArray() + { + var bytes = new byte[] { 1, 2, 3, 4 }; + var dic = new Dictionary + { + { "MyByteArray", Convert.ToBase64String(bytes) } + }; + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddInMemoryCollection(dic); + var config = configurationBuilder.Build(); + + var options = config.Get(); + Assert.Equal(bytes, options.MyByteArray); + } + + [Fact] + public void CanBindByteArrayWhenValueIsNull() + { + var dic = new Dictionary + { + { "MyByteArray", null } + }; + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddInMemoryCollection(dic); + var config = configurationBuilder.Build(); + + var options = config.Get(); + Assert.Equal(null, options.MyByteArray); + } + + [Fact] + public void ExceptionWhenTryingToBindToByteArray() + { + var dic = new Dictionary + { + { "MyByteArray", "(not a valid base64 string)" } + }; + var configurationBuilder = new ConfigurationBuilder(); + configurationBuilder.AddInMemoryCollection(dic); + var config = configurationBuilder.Build(); + + var exception = Assert.Throws( + () => config.Get()); + Assert.Equal( + SR.Format(SR.Error_FailedBinding, "MyByteArray", typeof(byte[])), + exception.Message); + } + private interface ISomeInterface { } -- 2.7.4