-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Generic mapping for non-collection type #1044
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
Conversation
Hey @radiosterne sorry for not responding earlier, this PR totally flew under my radar. Will have a look tomrorrow, thanks for signing the CLA and including tests! |
@radiosterne Thanks for the PR. I agree Consider the case where we have a generic type like Also, I think it would be better to check for |
Thanks for pointing these issues out, @gmarz ! You're totally right on both accounts, so I've changed my code a bit according to your propositions. I can't think of any other cases when we should map to underlying type by default (of course, any behaviour can still be overriden by using |
@radiosterne Great, thanks for following up! These are definitely edge cases, but it would be best to stay as generic as possible. Thinking about this some more...I wonder if this makes the most sense:
Return the underlying type when the generic only has a single type argument. This takes care of It will never be perfect, but it's better than just arbitrarily assuming the first type argument of a generic type is the correct type to map as the current implementation does. Thoughts? |
@gmarz I have to object against such implementation: actually, when I first encountered this behaviour was with my custom generic type public class Range<T> where T : struct
{
public T? From { get; set; }
public T? To { get; set; }
} I think, this can be a pretty common usecase, so we should handle arbitrary generic with single type argument as a nested object by default. But you are, of course, right, we should only map IEnumerable to its underlying type only if it, actually, has one type argument: consider I therefore propose to change |
++ @radiosterne was just typing up a similar response. We should assume any |
Right on 👍 |
I've synced my branch with upstream, so you're good to go! |
Hey @radiosterne I ended up just cherry-picking your 94060e2 commit which was a bit cleaner than merging the entire PR, since your branch is from master and not develop. Also added a simple unit test for this...didn't think the full blown integration tests were necessary (although really appreciated). Unit tests are also nice because they run as part of our CI. Hopefully this is cool with you...feel free to expand on the tests more if you think we need to. Thanks again for the PR, this was a great find! Looking forward to more from you :) 👍 |
These changes fix wrong treatment of non-collection generic types: in upstream version they are treated as generic collections, so if it's instantiated with value type, it is mapped to its underlying type — "Tuple
<int, int>
" gets mapped to "integer". Here I'm just checking whether type implements ICollection and use underlying type only if it is. Also, basic test is provided. (And I've signed CLA =)