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

Check if dynamic property exists during add operation #6657

Merged
merged 1 commit into from
Feb 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Abp/DynamicEntityProperties/DynamicPropertyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ protected virtual void CheckDynamicProperty(DynamicProperty dynamicProperty)
{
throw new ApplicationException($"Input type is invalid, if you want to use \"{dynamicProperty.InputType}\" input type, define it in DynamicEntityPropertyDefinitionProvider.");
}

var existingProperty = _dynamicPropertyStore.Get(dynamicProperty.PropertyName);
if (existingProperty != null)
{
throw new ArgumentException($"There is already a dynamic property with name: '{dynamicProperty.PropertyName}'");
}
}

public virtual DynamicProperty Add(DynamicProperty dynamicProperty)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.DynamicEntityProperties;
using Abp.Runtime.Caching;
using NSubstitute;
Expand Down Expand Up @@ -172,6 +174,31 @@ public void Should_Not_Add_If_Property_Name_Is_Null_Or_Empty()
Should.Throw<ArgumentNullException>(() => dynamicPropertyManager.Add(testDynamicProperty2));
exception2.Message.ShouldContain(nameof(testDynamicProperty.PropertyName));
}

[Fact]
public void Should_Not_Add_Duplicate_Property_Name()
{
var propertyName = "Age";
var testDynamicProperty1 = new DynamicProperty
{
PropertyName = propertyName,
InputType = Resolve<IDynamicEntityPropertyDefinitionManager>().GetAllAllowedInputTypeNames().First(),
TenantId = AbpSession.TenantId
};

var testDynamicProperty2 = new DynamicProperty
{
PropertyName = propertyName,
InputType = Resolve<IDynamicEntityPropertyDefinitionManager>().GetAllAllowedInputTypeNames().First(),
TenantId = AbpSession.TenantId
};

var dynamicPropertyManager = Resolve<IDynamicPropertyManager>();

dynamicPropertyManager.Add(testDynamicProperty1);
var exception = Should.Throw<ArgumentException>(() => dynamicPropertyManager.Add(testDynamicProperty2));
exception.Message.ShouldBe($"There is already a dynamic property with name: '{propertyName}'");
}

[Fact]
public void Should_Update_And_Change_Cache()
Expand Down