-
-
Notifications
You must be signed in to change notification settings - Fork 155
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
Support mapping derived types #330
Comments
As stated by the MS docs
Could you provide an example of a mapper declaration including relevant classes and what you expect the generated code to look like? |
@latonz KnownType is also used by Swagger to find derived types. .NET's new [JsonDerivedType] attribute could also be used as an attribute to detect derived types. Usage could look like this [KnownType(typeof(Dog))]
[KnownType(typeof(Cat))]
public abstract class AnimalBase
{
public required string Name { get; set; }
}
public class Dog : AnimalBase
{
public required string DogName { get; set; }
}
public class Cat : AnimalBase
{
public int CatAge { get; set; }
}
public abstract class AnimalBaseDTO
{
public string? Name { get; set; }
}
public class DogDTO : AnimalBaseDTO
{
public string? DogName { get; set; }
}
public class CatDTO : AnimalBaseDTO
{
public int CatAge { get; set; }
}
public static partial class Mapper
{
public static partial List<AnimalBaseDTO>? MapAnimal(List<AnimalBase>? input);
} Mapperly's automatic implementation could look like this public static partial class ModelMapper
{
public static partial List<AnimalBaseDTO>? MapAnimal(List<AnimalBase>? input)
{
if (input == null)
return null;
return new List<AnimalBaseDTO>(
input.Select(p => p switch
{
Dog dog => MapDog(dog),
Cat cat => MapCat(cat),
_ => throw new NotSupportedException(),
})
);
}
public static MapDog(Dog input)
{
return new CatDto
{
Name = input.Name,
DogName = input.DogName,
};
}
public static MapCat(Cat input)
{
return new DogDto
{
Name = input.Name,
CatAge = input.CatAge,
};
}
} |
Ah now I understand what you are looking for. This would be a great addition to Mapperly! [Mapper]
public static partial class ModelMapper
{
public static partial List<AnimalBaseDTO> MapAnimals(List<AnimalBase> input);
[MapDerivedType(typeof(Dog), typeof(DogDto))]
[MapDerivedType(typeof(Cat), typeof(CatDto))]
private static partial AnimalBaseDTO MapAnimal(AnimalBase input);
} |
KnownTypeAttribute
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
A perhaps simpler case is if using Mapperly to generate a deep cloner. (You might want to hide the comments regarding generic attributes to keep the thread manageable) |
@jnyrup that's true. I don't think it is worth to introduce a separate attribute for this, as Mapperly's primary use case is object-to-object mapping and the verbosity in this case isn't much higher to configure Mapperly to just clone objects. |
🎉 This issue has been resolved in version 3.0.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
[KnownType] and [JsonDerivedType] attributes are used on a base type to inform callers/serializers etc what its known derived types are, Mapperly could use this to automatically generate bindings for known types
The text was updated successfully, but these errors were encountered: