ExtraPropertyBindingHelper should supported containerless expressions. Added tests.

pull/3831/head
Halil İbrahim Kalkan 6 years ago
parent 932410b2fb
commit fbfd912a14

@ -5,33 +5,33 @@ namespace Volo.Abp.AspNetCore.Mvc.ModelBinding
public static class ExtraPropertyBindingHelper
{
/// <summary>
/// modelName is a string like "UserInfo.ExtraProperties[SocialSecurityNumber]"
/// <paramref name="expression"/> is a string like "UserInfo.ExtraProperties[SocialSecurityNumber]"
/// This method returns "SocialSecurityNumber" for this example. */
/// </summary>
public static string ExtractExtraPropertyName(string modelName)
public static string ExtractExtraPropertyName(string expression)
{
var index = modelName.IndexOf(".ExtraProperties[", StringComparison.Ordinal);
var index = expression.IndexOf("ExtraProperties[", StringComparison.Ordinal);
if (index < 0)
{
return null;
}
return modelName.Substring(index + 17, modelName.Length - index - 18);
return expression.Substring(index + 16, expression.Length - index - 17);
}
/// <summary>
/// modelName is a string like "UserInfo.ExtraProperties[SocialSecurityNumber]"
/// <paramref name="expression"/> is a string like "UserInfo.ExtraProperties[SocialSecurityNumber]"
/// This method returns "UserInfo" for this example.
/// </summary>
public static string ExtractContainerName(string modelName)
public static string ExtractContainerName(string expression)
{
var index = modelName.IndexOf(".ExtraProperties[", StringComparison.Ordinal);
var index = expression.IndexOf("ExtraProperties[", StringComparison.Ordinal);
if (index < 0)
{
return null;
}
return modelName.Left(index);
return expression.Left(index).TrimEnd('.');
}
}
}

@ -0,0 +1,48 @@
using Shouldly;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.ModelBinding
{
public class ExtraPropertyBindingHelper_Tests
{
[Fact]
public void ExtractExtraPropertyName_Tests()
{
ExtraPropertyBindingHelper.ExtractExtraPropertyName(
"MyObject.UserInfo.ExtraProperties[SocialSecurityNumber]"
).ShouldBe("SocialSecurityNumber");
ExtraPropertyBindingHelper.ExtractExtraPropertyName(
"UserInfo.ExtraProperties[SocialSecurityNumber]"
).ShouldBe("SocialSecurityNumber");
ExtraPropertyBindingHelper.ExtractExtraPropertyName(
"ExtraProperties[SocialSecurityNumber]"
).ShouldBe("SocialSecurityNumber");
ExtraPropertyBindingHelper.ExtractExtraPropertyName(
"SocialSecurityNumber"
).ShouldBeNull();
}
[Fact]
public void ExtractContainerName_Tests()
{
ExtraPropertyBindingHelper.ExtractContainerName(
"MyObject.UserInfo.ExtraProperties[SocialSecurityNumber]"
).ShouldBe("MyObject.UserInfo");
ExtraPropertyBindingHelper.ExtractContainerName(
"UserInfo.ExtraProperties[SocialSecurityNumber]"
).ShouldBe("UserInfo");
ExtraPropertyBindingHelper.ExtractContainerName(
"ExtraProperties[SocialSecurityNumber]"
).ShouldBe("");
ExtraPropertyBindingHelper.ExtractContainerName(
"SocialSecurityNumber"
).ShouldBeNull();
}
}
}
Loading…
Cancel
Save