-
Notifications
You must be signed in to change notification settings - Fork 337
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
馃悰 Enum data type is not applied when a member has both [StringLength]
and [EnumDataType]
annotations
#1409
Comments
[StringLength]
and [EnumDataType]
annotations
[StringLength]
and [EnumDataType]
annotations[StringLength]
and [EnumDataType]
annotations
Hi @aivascu , |
Hi @cervengoc , |
@cervengoc so, as a workaround for prioritizing the data annotations attributes differently, or adding support for more attributes, it's possible to insert a new customization node at the front of the var builder = new CompositeSpecimenBuilder(
new EnumDataTypeAttributeRelay(),
new EnumRangedRequestRelay(),
new RegularExpressionAttributeRelay(),
new TimeSpanRangedRequestRelay(),
new RangeAttributeRelay(),
new NumericRangedRequestRelay(),
new MinAndMaxLengthAttributeRelay(),
new StringLengthAttributeRelay());
fixture.Customizations.Insert(0, builder); I'll postpone a permanent fix until I decide how to address this request in combination with #1091 and #1348. Full codepublic class UnitTests
{
[Fact]
public void Foo()
{
var fixture = new Fixture().Customize(new CustomDataAnnotationsCustomization());
var actual = fixture.Create<TestType>();
actual.EnumAsString.Should().BeOneOf("One", "Two", "Three");
actual.RegexString.Length.Should().Be(15);
}
}
public enum TestEnum
{
One = 1,
Two,
Three,
}
public class TestType
{
[StringLength(64)]
[EnumDataType(typeof(TestEnum))]
public string EnumAsString { get; set; }
[StringLength(12)]
[RegularExpression("^[A-Z]{15}")]
public string RegexString { get; set; }
}
public class CustomDataAnnotationsCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
var builder = new CompositeSpecimenBuilder(
new EnumDataTypeAttributeRelay(),
new EnumRangedRequestRelay(),
new RegularExpressionAttributeRelay(),
new RangeAttributeRelay(),
new NumericRangedRequestRelay(),
new TimeSpanRangedRequestRelay(),
new StringLengthAttributeRelay(),
new MinAndMaxLengthAttributeRelay());
fixture.Customizations.Insert(0, builder);
}
} |
Thank you for the workaround suggestion, looks reasonable for now, especially as we already have a bunch of customizations around. |
Describe the Bug
In our EF Core data layer, in most cases enums are mapped as
string
s. So we have several classes where a member has both[StringLength]
and[EnumDataType]
attributes. When usingAutoFixture
to create an instance of such a class, the latter attribute is disregarded.Scenario
Expected Behavior
I'd expect that the
[EnumDataType]
attribute has a stronger precedence as it's more strict than a string length.Tasks
More Information
As I digged into the source code I saw that
EnumDataTypeAttributeRelay
is afterStringLengthAttributeRelay
in theFixture
constructor. Seems to me that it should be before that, and it would instantly solve the issue.Thanks!
EDIT: I'm aware of built-in options to map enums as strings in EF Core. It's a rather old codebase, and right now we're not able to do such a refactor.
The text was updated successfully, but these errors were encountered: