Comments (4)
Thanks, I'll use the workaround you suggested, seems to work!
from autofixture.
Hi @putnap,
Thank you for raising this issue. What I think the issue is, it's that records create classes that do not properly encapsulate data, when looked at from the perspective of reflection (see below).
AutoFixture currently looks at the list of constructors takes the least demanding public constructor and uses it to initialize the data, then it looks at your other members and assumes that since you exposed a public setter you need it initialized from outside the constructor, so it does just that. This is why you're observing the behavior that you described.
I'm currently working on an API for easier customization of record-like structures (customizing constructor parameters, selecting constructors, etc), but it's still in early development.
Feature requests like this have been raised before, but as far as I remember this wasn't implemented before because there is no static way to define constructor parameters like you would with members (ie. .With(x => x.Member, "MemberValue")
).
// Example record
record Customer(string FirstName, string LastName);
// Equivalent class generated in IL
class Customer
{
public Customer(string FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
public string FirstName { get; set; }
public string FirstName { get; set; }
}
from autofixture.
Thanks for a quick reply!
I guess there are two topics - customizing via record constructors feature and performance issues.
Right now I want to understand why performance hit is so severe.
Also, I believe that class you mention is not equivalent, since record types are immutable by default and generated properties don't have public setters, only init
setters.
public string FirstName { get; init; }
public string LastName { get; init; }
And I would like some peace of mind that my ICustomization with CompilerGeneratedAttribute
filter in this file is a good enough general workaround for now. I don't fully grasp all repercussions of using new OmitSpecimen();
so dramatically.
from autofixture.
About the init
setters you're kinda correct. It does generate the equivalent IL for init
for positional records, however the difference between a public setter and init
is an attribute set on the return value of the setter method, which AutoFixture at the moment doesn't know exists. Everything else in terms of accessibility modifiers is exactly the same as a public setter.
This said I don't think the customization in your sample is doing anything else than ignoring all properties.
A more correct way to ignore all redundant property setter is to explicitly account for them.
Here is a gist that shows an example, how to ignore specifically those properties that are init only and constructor initialized
https://gist.github.com/aivascu/739f2a845eac587ca4df11cd9c5f7d31
Expand for IL examples
// usual public setter
.property instance string Name()
{
.get instance string TestProject.ClassA::get_Name()
.set instance void TestProject.ClassA::set_Name(string)
} // end of property ClassA::Name
// positional record property
.property instance string Name()
{
.get instance string TestProject.ClassB::get_Name()
.set instance void modreq ([System.Runtime]System.Runtime.CompilerServices.IsExternalInit) TestProject.ClassB::set_Name(string)
} // end of property ClassB::Name
// init-only property
.property instance string Name()
{
.get instance string TestProject.ClassC::get_Name()
.set instance void modreq ([System.Runtime]System.Runtime.CompilerServices.IsExternalInit) TestProject.ClassC::set_Name(string)
} // end of property ClassC::Name
from autofixture.
Related Issues (20)
- β¨ Repeatable random numbers
- π [AutoFixture.Xunit2] MemberAutoDataAttribute generates fixtures for test method parameters provided from MemberData HOT 3
- π Enum data type is not applied when a member has both `[StringLength]` and `[EnumDataType]` annotations HOT 4
- π Unexpected ObjectCreationExceptionWithPath or if not a bug at least a confusing description HOT 3
- β¨ Support FakeItEasy 8 HOT 1
- β¨ Support for NUnit 4 HOT 16
- π FromFactory doesn't honor the Range attribute HOT 6
- β¨ Accept `ISpecimentBuilder` as value generator for `With` HOT 1
- β¨ DataAnnotations support for specimen factories
- ICustomization for DateOnly causes a hang. HOT 1
- π RandomNumericSequenceGenerator with limits, can create values outside of these limits in certain scenarios HOT 1
- autofixture is missing NuGet package README file HOT 2
- autofixture.automoq is missing NuGet package README file HOT 1
- `IAsyncEnumerable` generates an infinite sequence π HOT 2
- π SourceLink doesn't work because of incorrect casing in GitHub URLs HOT 4
- π¨ Vulnerability in packages System.Net.Http and System.Text.RegularExpressions HOT 3
- Builder.WithVariations⨠HOT 4
- π FromSeed() does not set value from seeded object property HOT 4
- Not able to create second nullalbe int π
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from autofixture.