Orika dynamically ignore fields

330 views Asked by At

I'm really struggling with finding a way to dynamically exclude fields when mapping with Orika. I've searched across the internet and on StackOverflow but really couldn't find any solution.

For example this works fine:

// register mapper
factory.classMap(UserEntity.class, User.class)
            .byDefault()
            .register();

// initialize user
UserEntity userEntity = new UserEntity("John", "Doe", ...);

// map all registered fields
User user = mapper.map(userEntity, User.class);

But I want to dynamically ignore fields in certain circumstances. So what I want to achieve is something like:

List<String> exclude = List.of("firstname");
User userWithoutFirstname = mapper.map(userEntity, User.class, exclude);

List<String> exclude = List.of("lastname");
User userWithoutLastname = mapper.map(userEntity, User.class, exclude);

Is there any way I can achieve this. My main goal is to dynamically exclude some fields to also prevent Lazy initialization in certain scenarios.

I know I can do it statically, but that's NOT what I want.

 factory.classMap(UserEntity.class, User.class)
            .exclude("firstname")
            .byDefault()
            .register();

Hope anyone can help me out!

Thankyou!

0

There are 0 answers