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

Destination and source type mismatch does not seem to be validated #691

Open
Ekkeir opened this issue Apr 2, 2024 · 0 comments
Open

Destination and source type mismatch does not seem to be validated #691

Ekkeir opened this issue Apr 2, 2024 · 0 comments

Comments

@Ekkeir
Copy link

Ekkeir commented Apr 2, 2024

For the following classes

public class Source
{
    public DateTime Time { get; set; }
}

public class Destination
{
    public int Number { get; set; }
}

I'd expect configuration validation to fail, but we only observe an exception then executing the actual mapping

    [Fact]
    public void Should_Validate_Type_Mapping()
    {
        var config = new TypeAdapterConfig();

        config.ForType<Source, Destination>()
            .Map(dest => dest.Number, source => source.Time);

        // Does not throw
        config.Compile();

        var source = new Source
        {
            Time = DateTime.UtcNow,
        };

        // Actually throws System.InvalidCastException : Invalid cast from 'DateTime' to 'Int32'.
        var result = source.Adapt<Destination>(config);
    }

Is there some kind of configuration switch, that would allow us to validate matched types? We've switched from Automapper and would like to catch invalid mappings without writing tests for all of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant