Skip to content
Advertisement

Can I extend MapStruct methods?

I’m developing a library and I expect the library to have a mapper like so:

JavaScript

Which is meant for internal remapping. Now, I’d like to add a feature where the user can “override” this method, in the sense that they could for example add a statement to ignore the password of the account, while also calling the internal methods.

Few examples as reference points:

JavaScript

The user should be able to define a property to ignore, or for example add with a specified source. From what I experimented, even without the overrides and such, it just generates a separate mapping method, instead of even trying to reuse the other one.

Is this possible without needing to implement custom methods on the user side which would call the internal methods? If not, is there a particular reason, or has it just not come up as a possible feature in MapStruct?

Advertisement

Answer

MapStruct generates code during compilation. This means that it is not possible to ignore certain properties and invoke another mapper for this, because the other mapper already has that implemented .

As a new feature we could add something to the @Mapper that would basically merge the configuration from the parent mapper.

e.g.

JavaScript

This would mean that if this new methodOverrideStrategy (the name is not final) would mean that MapStruct will perform a merge of the annotation on the toAccountDtoDefault method.

For MapStruct it would be as if the user has written.

JavaScript

I would suggest that you raise this as a feature request in the MapStruct issue tracker.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement