You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
TypeAdapterConfig.GlobalSettings.Compile() throws exception when open generics are configured #925
When a mapping for a generic type is configured like described here TypeAdapterConfig.GlobalSettings.Compile() throws a Mapster.CompileException: 'Operation is not valid due to the current state of the object.' with an inner ArgumentException: Type ClassA`1[T] is a generic type definition (Parameter 'type').
Hi @cjung95 I checked, this isn't a regression in v10. It also threw an error in version 7.4.0.
It seems to actually work like a hidden Inherits - when a child class copies the parent's settings. But the OpenGeneric configuration itself ends up in the shared settings store.
And since it's impossible to create an OpenGeneric instance, it doesn't compile, as it's impossible to create a function that instantiates OpenGeneric.
Hi @DocSvartz,
thanks for you reply.
I'm running the Compile() function to validate my configuration, so I need a solution here.
Should I figure out a solution myself and create a pull request, or will someone on your team take on this issue?
I don't quite follow your argument. Registering open generic types is valid according to the specification. However, when you run the function provided in the specification to validate the configuration, it throws an exception. Yet you say that the validation result is correct. Could you please explain that to me?
Compile is the creation of a function to convert a source type to a destination type.
By calling Compile, you're actually trying to do the following:
var result = new ClassB<>() // - Which is an invalid operation
This classA.Adapt<ClassB<int>>(); in turn means:
Convert ClassA<int> to ClassB<int> by copying the mapping settings for the types ClassA<> to ClassB<> if it exist
var result = new ClassB<int>() // which is now a valid operation
Then the logical conclusion would be to generally exclude open generic types from the Compile function, otherwise, the documentary is incorrect.
Do you disagree?
Then the logical conclusion would be to generally exclude open generic types from the Compile function, otherwise, the documentary is incorrect.
Yes, this is the fastest solution. But in this case, there won't be any validation at all.
As far as I understand, the validation is run mainly to ensure that all fields are mapped.
If that's okay with you, I'll take a closer look at this issue next week and find a solution that allows for proper validation even with open generics. Let's not make any hasty changes here.
your implementation idea is very good, but unfortunately it doesn't work in my case.
I have a constraint for the generic parameter, so I get the following exception:
System.ArgumentException: 'GenericArguments[0], 'System.Object', on 'PaginatedList`1[T]' violates the constraint of type 'T'.'
I recommend reverting the change until we have a comprehensive solution.
Yes, This is another limitation, in addition to multiple generics OpenGeneric<T1,T2,T3>.
I think we could try to get restrictions for generics and build a stub based on them. 🤔
your implementation idea is very good, but unfortunately it doesn't work in my case.
I have a constraint for the generic parameter, so I get the following exception:
System.ArgumentException: 'GenericArguments[0], 'System.Object', on 'PaginatedList`1[T]' violates the constraint of type 'T'.'
I recommend reverting the change until we have a comprehensive solution.
@cjung95 Yes, it seems that given the extensive capabilities for setting restrictions for generic parameters, it will be necessary to write something similar to a mock library. 😅
Compile checking for open generics will be disabled.
However, you can always test how field mapping works by registering a stub yourself.
config
.NewConfig(typeof(A<>), typeof(B<>))
.Map("BProperty", "AProperty");
config
.NewConfig<A<int>, B<string>>(); // stub for check Property mapping
config.Compile(); // is not throw exception
You're probably right about that.
For now skipping the check for generic types solves my problem.
I'll still give it some thought and see if there is an easier way.
Thank you for your help!
Activity
DocSvartz commentedon Apr 24, 2026
Hi @cjung95 I checked, this isn't a regression in v10. It also threw an error in version 7.4.0.
It seems to actually work like a hidden
Inherits- when a child class copies the parent's settings. But the OpenGeneric configuration itself ends up in the shared settings store.And since it's impossible to create an OpenGeneric instance, it doesn't compile, as it's impossible to create a function that instantiates OpenGeneric.
cjung95 commentedon Apr 24, 2026
Hi @DocSvartz,
thanks for you reply.
I'm running the Compile() function to validate my configuration, so I need a solution here.
Should I figure out a solution myself and create a pull request, or will someone on your team take on this issue?
DocSvartz commentedon Apr 24, 2026
I'm currently working on a new way to create config for OpenGeneric
using marker
This problem won't occur there.
In your case, can remove the error by removing the check for Opengeneric types.
Because the check result is actually valid. 😅
cjung95 commentedon Apr 26, 2026
I don't quite follow your argument. Registering open generic types is valid according to the specification. However, when you run the function provided in the specification to validate the configuration, it throws an exception. Yet you say that the validation result is correct. Could you please explain that to me?
DocSvartz commentedon Apr 26, 2026
Compile is the creation of a function to convert a source type to a destination type.
By calling Compile, you're actually trying to do the following:
var result = new ClassB<>() // - Which is an invalid operationThis
classA.Adapt<ClassB<int>>();in turn means:Convert
ClassA<int>toClassB<int>by copying the mapping settings for the typesClassA<> to ClassB<>if it existvar result = new ClassB<int>() // which is now a valid operationcjung95 commentedon Apr 26, 2026
Then the logical conclusion would be to generally exclude open generic types from the
Compilefunction, otherwise, the documentary is incorrect.Do you disagree?
DocSvartz commentedon Apr 26, 2026
Yes, this is the fastest solution. But in this case, there won't be any validation at all.
As far as I understand, the validation is run mainly to ensure that all fields are mapped.
DocSvartz commentedon Apr 26, 2026
@cjung95 I can add this disabling of the check as a temporary solution.
But really, we need to come up with a proper, permanent solution 😒
cjung95 commentedon Apr 26, 2026
If that's okay with you, I'll take a closer look at this issue next week and find a solution that allows for proper validation even with open generics. Let's not make any hasty changes here.
DocSvartz commentedon Apr 26, 2026
@cjung95 Ok,
solution using the OpenGeneric definition via proxy stub
solves this problem. However,
Include()doesn't work correctly yet. 😅fix: MapsterMapper#925 - Compile config with open generic is not thro…
fix: MapsterMapper#925 - Compile config with open generic is not thro…
chore: Merge pull request #935 from DocSvartz/fix-#925
DocSvartz commentedon Apr 28, 2026
Hi @cjung95. This should be fixed in https://www.nuget.org/packages/Mapster/10.0.8-pre04
But this is only for the basic case, when the generic has one parameter.
fix: MapsterMapper#925 when many open generic param
cjung95 commentedon Apr 29, 2026
Hi @DocSvartz,
your implementation idea is very good, but unfortunately it doesn't work in my case.
I have a constraint for the generic parameter, so I get the following exception:
System.ArgumentException: 'GenericArguments[0], 'System.Object', on 'PaginatedList`1[T]' violates the constraint of type 'T'.'
I recommend reverting the change until we have a comprehensive solution.
DocSvartz commentedon Apr 29, 2026
Yes, This is another limitation, in addition to multiple generics OpenGeneric<T1,T2,T3>.
I think we could try to get restrictions for generics and build a stub based on them. 🤔
Chore: Merge pull request #33 from DocSvartz/fix-925-to-many-opengene…
DocSvartz commentedon May 5, 2026
@cjung95 Yes, it seems that given the extensive capabilities for setting restrictions for generic parameters, it will be necessary to write something similar to a mock library. 😅
Compile checking for open generics will be disabled.
However, you can always test how field mapping works by registering a stub yourself.
cjung95 commentedon May 6, 2026
You're probably right about that.
For now skipping the check for generic types solves my problem.
I'll still give it some thought and see if there is an easier way.
Thank you for your help!
fix: #925 disable Compile check for open generic configs
chore: Merge pull request #942 from DocSvartz/disable-check-for-OpenG…
test: ensure Compile skips open generic mapping rules (#925)
chore: Merge pull request #965 from leno23/test/compile-open-generic-…