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

MemberConverter() with a different target type #1376

Closed
space-alien opened this issue Dec 21, 2024 · 3 comments
Closed

MemberConverter() with a different target type #1376

space-alien opened this issue Dec 21, 2024 · 3 comments

Comments

@space-alien
Copy link

I would like to be able to do something like this:

VerifierSettings.MemberConverter<Order, Customer, string>(order => order.Customer, customer => customer.Name);

Such that when snapshotting an Order its Customer will be converted to a string.

I am currently achieving this by registering a custom converter that projects Order for serialization.

However, I instinctively dislike this approach as it feels brittle, and will be harder to maintain:

class CustomConverter<T>(Func<T?, object?> convert)
    : WriteOnlyJsonConverter<T?>
{
    public override void Write(VerifyJsonWriter writer, T? value)
    {
        writer.Serializer.Serialize(writer, convert(value));
    }
}

VerifierSettings.AddExtraSettings(settings =>
{
   settings.Converters.Add(new CustomConverter<Order>(
        o => new { ... }));
});
@SimonCropp
Copy link
Member

I am currently achieving this by registering a custom converter that projects Order for serialization.
However, I instinctively dislike this approach as it feels brittle, and will be harder to maintain:

why do you think this is fragile?

@space-alien
Copy link
Author

why do you think this is fragile?

I would prefer to snapshot Order directly, so that if it is refactored in future I can be sure this will lead to changed snapshots, forcing a review of the tests. The projection gets in the way of this. (For example, the projection won't change if a new member is added to Order.)

But I suppose that's just my preference for my particular situation. What do you think of the idea in principle? I think it might be nice simplification to be able to remap a member to a different type for serialization, without needing the ceremony of a custom converter + projection.

@SimonCropp
Copy link
Member

sorry i dont see this as adding any value over a custom serializer. you are free to extend Verify and ship that custom behaviour in your own nuget package

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

No branches or pull requests

2 participants