MapStruct generates wrong mapping code without any inverse config? - mapstruct

I have a #MapperConfig looks like this.
#MapperConfig(componentModel = "spring")
public interface SomeEntityTypeMapperConfig {
#Mapping(target = PROPERTY_NAME_ENTITY)
#Mapping(source = SomeEntity.ATTRIBUTE_NAME_ID, target = SomeEntityType.PROPERTY_NAME_ID)
#Mapping(source = SomeEntity.PROPERTY_NAME_CREATED_AT, target = SomeEntityType.PROPERTY_NAME_CREATED_AT)
#Mapping(source = SomeEntity.PROPERTY_NAME_UPDATED_AT, target = SomeEntityType.PROPERTY_NAME_UPDATED_AT)
#Mapping(source = SomeEntity.PROPERTY_NAME_CREATED_BY, target = SomeEntityType.PROPERTY_NAME_CREATED_BY)
#Mapping(source = SomeEntity.PROPERTY_NAME_UPDATED_BY, target = SomeEntityType.PROPERTY_NAME_UPDATED_BY)
SomeEntityType<?, ?> fromEntity(SomeEntity entity);
// No #Mapping
void toEntity(SomeEntityType<?, ?> type, #MappingTarget SomeEntity entity);
}
Here comes my base mapper interface.
public interface SomeEntityTypeMapper<T extends SomeEntityType<?, U>, U extends SomeEntity> {
T fromEntity(U entity);
void toEntity(T type, #MappingTarget U entity);
}
And here comes my real mapper.
#Mapper(config = SomeEntityTypeMapperConfig.class)
public interface UserTypeMapper extends SomeEntityTypeMapper<UserType, User> {
#Mapping(source = User.ATTRIBUTE_NAME_NAME, target = UserType.PROPERTY_NAME_NAME)
#Override
UserType fromEntity(User entity);
#Mapping(source = UserType.PROPERTY_NAME_NAME, target = User.ATTRIBUTE_NAME_NAME)
#Override
void toEntity(UserType type, #MappingTarget User entity);
}
And MapStruct generates following impl class with unwanted mappings in it.
public class UserTypeMapperImpl implements UserTypeMapper {
#Override
public UserType fromEntity(User entity) {
if ( entity == null ) {
return null;
}
UserType userType = new UserType();
userType.setName( entity.getName() ); // explicitly configured
userType.setId( entity.getId() ); // inherited from the config
userType.setCreatedAt( entity.getCreatedAt() ); // inherited from the config
userType.setUpdatedAt( entity.getUpdatedAt() ); // inherited from the config
userType.setCreatedBy( entity.getCreatedBy() ); // inherited from the config
userType.setUpdatedBy( entity.getUpdatedBy() ); // inherited from the config
return userType;
}
#Override
public void toEntity(UserType type, User entity) {
if ( type == null ) {
return;
}
entity.setName( type.getName() ); // explicitly configured
entity.setCreatedAt( type.getCreatedAt() ); // UNWANTED!!!
entity.setUpdatedAt( type.getUpdatedAt() ); // UNWANTED!!!
entity.setUpdatedBy( type.getUpdatedBy() ); // UNWANTED!!!
entity.setId( type.getId() ); // UNWANTED!!!
entity.setCreatedBy( type.getCreatedBy() ); // UNWANTED!!!
}
}
What did I do wrong and How can I fix it?

What you are referring as unwanted reverse mapping without any annotations is actually the normal way that MapStruct generates mappings. If the source and target beans have the same property (which they do in your case) MapStruct would create a mapping for it.
In case you don't want to map some properties you can either ignore those one by one or use #BeanMapping( ignoreByDefault = true). With the second option MapStruct would only create mappings for the defined #Mapping.

I'm sharing what I found.
I needed to annotate the method with #BeanMapping(ignoreByDefault = true).
Interestingly the annotation must be located with leaf mapper interfaces.
#BeanMapping(ignoreByDefault = true) // WORKS!!!
#Mapping(source = UserType.PROPERTY_NAME_NAME, target = User.ATTRIBUTE_NAME_NAME)
#Override
void toEntity(UserType type, #MappingTarget User entity);
Didn't work with configuration nor parent interface.
public interface SomeEntityTypeMapperConfig {
#BeanMapping(ignoreByDefault = true) // Doesn't work!
void toEntity(SomeEntityType<?, ?> type, #MappingTarget SomeEntity entity);
}
public interface SomeEntityTypeMapper<T extends SomeEntityType<?, U>, U extends SomeEntity> {
#BeanMapping(ignoreByDefault = true) // Doesn't work!
void toEntity(T type, #MappingTarget U entity);
}

Related

Mapping a field using existing target value (Mapstruct)

i have a custom case that some of my dto's have a field of type X, and i need to map this class to Y by using a spring service method call(i do a transactional db operation and return an instance of Y). But in this scenario i need to use existing value of Y field. Let me explain it by example.
// DTO
public class AnnualLeaveRequest {
private FileInfoDTO annualLeaveFile;
}
//ENTITY
public class AnnualLeave {
#OneToOne
private FileReference annualLeaveFile;
}
#Mapper
public abstract class FileMapper {
#Autowired
private FileReferenceService fileReferenceService;
public FileReference toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) {
return fileReferenceService.updateFile(fileInfoDTO, fileReference);
}
}
//ACTUAL ENTITY MAPPER
#Mapper(uses = {FileMapper.class})
public interface AnnualLeaveMapper {
void updateEntity(#MappingTarget AnnualLeave entity, AnnualLeaveRequest dto);
}
// WHAT IM TRYING TO ACHIEVE
#Component
public class MazeretIzinMapperImpl implements tr.gov.hmb.ikys.personel.izinbilgisi.mazeretizin.mapper.MazeretIzinMapper {
#Autowired
private FileMapper fileMapper;
#Override
public void updateEntity(AnnualLeave entity, AnnualLeaveUpdateRequest dto) {
entity.setAnnualLeaveFile(fileMapper.toFileReference(dto.getAnnualLeaveFile(), entity.getAnnualLeaveFile()));
}
}
But mapstruct ignores the result of "FileReference toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) " and does not map the result of it to the actual entity's FileReference field. Do you have any idea for resolving this problem?
Question
How do I replace the annualLeaveFile property while updating the AnnualLeave entity?
Answer
You can use expression to get this result. For example:
#Autowired
FileMapper fileMapper;
#Mapping( target = "annualLeaveFile", expression = "java(fileMapper.toFileReference(entity.getAnnualLeaveFile(), dto.getAnnualLeaveFile()))" )
abstract void updateEntity(#MappingTarget AnnualLeave entity, AnnualLeaveRequest dto);
MapStruct does not support this without expression usage. See the end of the Old analysis for why.
Alternative without expression
Instead of fixing it in the location where FileMapper is used, we fix it inside the FileMapper itself.
#Mapper
public abstract class FileMapper {
#Autowired
private FileReferenceService fileReferenceService;
public void toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) {
FileReference wanted = fileReferenceService.updateFile(fileInfoDTO, fileReference);
updateFileReference(fileReference, wanted);
}
// used to copy the content of the service one to the mapstruct one.
abstract void updateFileReference(#MappingTarget FileReference fileReferenceTarget, FileReference fileReferenceFromService);
}
Old analysis
The following is what I notice:
(Optional) your FileMapper class is not a MapStruct mapper. This can just be a normal class annotated with #Component, since it does not have any unimplemented abstract methods. (Does not affect code generation of the MazeretIzinMapper implementation)
(Optional, since you have this project wide configured) you do not have componentModel="spring" in your #Mapper definition, maybe you have this configured project wide, but that is not mentioned. (required for the #Autowired annotation, and #Component on implementations)
Without changing anything I already get a working result as you want it to be, but for non-update methods (not listed in your question, but was visible on the gitter page where you also requested help) the FileMapper as is will not be used. It requires an additional method that takes only 1 argument: public FileReference toFileReference(FileInfoDTO fileInfoDTO)
(Edit) to get rid of the else statement with null value handling you can add nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE to the #Mapper annotation.
I've run a test and with 1.5.0.Beta2 and 1.4.2.Final I get the following result with the thereafter listed FileMapper and MazeretIzinMapper classes.
Generated mapper implementation
#Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2022-03-11T18:01:30+0100",
comments = "version: 1.4.2.Final, compiler: Eclipse JDT (IDE) 1.4.50.v20210914-1429, environment: Java 17.0.1 (Azul Systems, Inc.)"
)
#Component
public class MazeretIzinMapperImpl implements MazeretIzinMapper {
#Autowired
private FileMapper fileMapper;
#Override
public AnnualLeave toEntity(AnnualLeaveRequest dto) {
if ( dto == null ) {
return null;
}
AnnualLeave annualLeave = new AnnualLeave();
annualLeave.setAnnualLeaveFile( fileMapper.toFileReference( dto.getAnnualLeaveFile() ) );
return annualLeave;
}
#Override
public void updateEntity(AnnualLeave entity, AnnualLeaveRequest dto) {
if ( dto == null ) {
return;
}
if ( dto.getAnnualLeaveFile() != null ) {
if ( entity.getAnnualLeaveFile() == null ) {
entity.setAnnualLeaveFile( new FileReference() );
}
fileMapper.toFileReference( entity.getAnnualLeaveFile(), dto.getAnnualLeaveFile() );
}
}
}
Source classes
Mapper
#Mapper( componentModel = "spring", uses = { FileMapper.class }, nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE )
public interface MazeretIzinMapper {
AnnualLeave toEntity(AnnualLeaveRequest dto);
void updateEntity(#MappingTarget AnnualLeave entity, AnnualLeaveRequest dto);
}
FileMapper component
#Mapper
public abstract class FileMapper {
#Autowired
private FileReferenceService fileReferenceService;
public FileReference toFileReference(#MappingTarget FileReference fileReference, FileInfoDTO fileInfoDTO) {
return fileReferenceService.updateFile( fileInfoDTO, fileReference );
}
public FileReference toFileReference(FileInfoDTO fileInfoDTO) {
return toFileReference( new FileReference(), fileInfoDTO );
}
// other abstract methods for MapStruct mapper generation.
}
Why the exact wanted code will not be generated
When generating the mapping code MapStruct will use the most generic way to do this.
An update mapper has the following criteria:
The #MappingTarget annotated argument will always be updated.
It is allowed to have no return type.
the generic way to update a field is then as follows:
// check if source has the value.
if (source.getProperty() != null) {
// Since it is allowed to have a void method for update mappings the following steps are needed:
// check if the property exists in the target.
if (target.getProperty() == null) {
// if it does not have the value then create it.
target.setProperty( new TypeOfProperty() );
}
// now we know that target has the property so we can call the update method.
propertyUpdateMappingMethod( target.getProperty(), source.getProperty() );
// The arguments will match the order as specified in the other update method. in this case the #MappingTarget annotated argument is the first one.
} else {
// default behavior is to set the target property to null, you can influence this with nullValuePropertyMappingStrategy.
target.setProperty( null );
}

Mapstruct constructor injection (spring)

In mapstruct 1.3.0.Final we have dependency injection via constructor. Documentation says:
The generated mapper will inject all classes defined in the uses
attribute
(...)
For abstract classes or decorators setter injection should be
used.
I have following example:
#Mapper
public abstract class VehicleMapper {
#Autowired
private CarMapper carMapper;
#Autowired
private BikeMapper bikeMapper;
#Override
public VehicleDTO toDto(final Vehicle source) {
if (source instanceof Car) {
return carMapper.toDto((Car) source);
} else if (source instanceof Bike) {
return bikeMapper.toDto((Bike) source);
} else {
throw new IllegalArgumentException();
}
}
(...)
So in my case it should look like this (componentModel defined in maven):
#Mapper
public abstract class VehicleMapper {
private CarMapper carMapper;
private BikeMapper bikeMapper;
#Autowired
public void setCarMapper(final CarMapper carMapper) {
this.carMapper = carMapper;
}
#Autowired
public void setBikeMapper(final BikeMapper bikeMapper) {
this.bikeMapper = bikeMapper;
}
#Override
public VehicleDTO toDto(final Vehicle source) {
if (source instanceof Car) {
return carMapper.toDto((Car) source);
} else if (source instanceof Bike) {
return bikeMapper.toDto((Bike) source);
} else {
throw new IllegalArgumentException();
}
}
(...)
Question:
So it is not possible to inject carMapper and bikeMapper via constructor ? does injectionStrategy = CONSTRUCTOR works only for classes declared in #Mapper(uses = {}) ?
I think that injectionStrategy = CONSTRUCTOR works on the interface that has the #Mapper annotation. I don't think it works with abstract classes. I'm sure it will not work when you define your own fields (instance variables). How would MapStruct know what user defined fields to initialise in the constructor?

Mapstruct #Mapper's uses attribute not working

I am facing an issue with one of my Mapstruct mappers not using another mapper with #Mapper(uses =
Our ValidationSupportNeedMapper maps from entities to DTOs. One ValidationSupportNeedEntity contains an ActivityEntity property and I am trying to map from this property to an Activity DTO.
The issue is therefore with a nested object i.e. ActivityEntity to Activity.
Here is the source code:
From ValidationSupportNeedMapper.java:
#Mapper(uses = {LifecycleErrorMessagesMapper.class, ActivityMapper.class})
public interface ValidationSupportNeedMapper {
ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source);
...
From ActivityMapper.java:
#Component
public class ActivityMapper {
public Activity toActivity(ActivityEntity activity) {
//Implementation
}
public ActivityEntity toActivityEntity(Activity activity) {
//Implementation
}
}
From ValidationSupportNeedEntity.java (Entity)
public class ValidationSupportNeedEntity {
private ActivityEntity activityEntity;
From ValidationSupportNeed.java (DTO)
public class ValidationSupportNeed implements AutoValidated {
private Activity validationActivity;
However Mapstruct seems to ignore the uses= attribute on the #Mapper annotation and goes ahead and generates its own mapping method as follows:
#Override
public ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source) {
if ( source == null ) {
return null;
}
ValidationSupportNeed validationSupportNeed = new ValidationSupportNeed();
validationSupportNeed.setValidationActivity( validationSupportNeedEntityToActivity( source ) );
...
}
protected Activity validationSupportNeedEntityToActivity(ValidationSupportNeedEntity validationSupportNeedEntity) {
if ( validationSupportNeedEntity == null ) {
return null;
}
Activity activity = new Activity();
activity.setCode( validationSupportNeedEntity.getValidationActivityCode() );
return activity;
}
What am I missing? Can someone please help?
edit: ActivityMapper is not autowired into the ValidationSupportNeedMapper implementation.
Adding a mapping annotation sorted the issue:
#Mapping(source = "activityEntity", target = "validationActivity")
ValidationSupportNeed toValidationSupportNeed(ValidationSupportNeedEntity source);
Notice the names of the attributes are different.

Why does my sub-dependency not get set in Dagger?

I am having a hard time figuring out how to inject CachedRithms into my RithmioManager and CachedKamms into my KamilManager?
I have the following files:
AppScopeModule:
#Module
(
library = true,
complete = false,
injects = {
KamilApplication.class,
KamilManager.class
}
)
public class AppScopeModule {
/* package */ static Context sApplicationContext = null;
private final Context mApplicationContext;
AppScopeModule(Context applicationContext) {
KamilManager.initInstance(applicationContext);
mApplicationContext = applicationContext;
}
#Provides
#Singleton
KamilManager provideKamilManager() {
return KamilManager.getInstance();
}
}
KamilApplication:
public class KamilApplication extends Application implements Injector {
private ObjectGraph mObjectGraph;
#Inject
KamilManager KamilManager;
#Override
public void onCreate() {
super.onCreate();
AppScopeModule sharedAppModule = new AppScopeModule(this);
// bootstrap. So that it allows no-arg constructor in AppScopeModule
sharedAppModule.sApplicationContext = this.getApplicationContext();
List<Object> modules = new ArrayList<Object>();
modules.add(sharedAppModule);
modules.add(new AuthModule());
modules.addAll(getAppModules());
mObjectGraph = ObjectGraph.create(modules.toArray());
mObjectGraph.inject(this);
}
}
KamilManager
public class KamilManager {
#Inject
CachedKamms mCachedKamms;
private static KamilManager instance;
private boolean mWearIsConnectedToMobile;
private KamilManager() {
Log.d(TAG, "KamilManager private constructor");
}
public static void initInstance(Context appContext) {
if (instance == null) {
instance = new KamilManager();
.....doing more things here...
}
}
public static KamilManager getInstance() {
return instance;
}
}
But mCAchedKamms is always blank when I initialize the app. Any idea what I'm doing wrong?
You need to call ObjectGraph.inject(this) somewhere in KamilManager.
I suggest you to add this code to your KamilApplication class:
public ObjectGraph getObjectGraph() {
return mObjectGraph;
}
After that you need to somehow get instance of KamilApplication(pass it via constructor maybe?) in KamilManager and call:
kamilApplication.getObjectGraph.inject(this);
after this call every field in class KamilManager annotated with #Inject should be injected.
OR
Just annotate constructor of CachedKamms with #Inject
Extra:
Avoid of using library = true and complete = false unless you know what are you doing. With this settings you disable some validations at compile time.

ListEditor with polymorphic types

I have searched around and been trying to figure out if I can use the editor framework with polymorphic types. I found this post at Using GWT Editors with a complex usecase which is close to what I am trying to do.
I am fairly new to the editor framework, so any help would be much appreciated.
For example, here is some of the code,
Data Transfer Objects:
public class Employee{
public List<Contact> contacts
}
public class Contact {
public class ContactEmail extends Contact {}
public class ContactAddress extends Contact {}
public class ContactPhoneNumber extends Contact {}
Editors:
public interface ContactBaseEditor<T extends Contact> extends Editor<T> {}
public class AddressEditor extends Composite implements Editor<ContactAddress>, ContactBaseEditor<ContactAddress>{}
public class EmailEditor extends Composite implements Editor<ContactEmail>, ContactBaseEditor<ContactEmail>{)
public class PhoneNumberEditor extends Composite implements Editor<ContactPhoneNumber>, ContactBaseEditor<ContactPhoneNumber>{}
ContactEditor class:
public class ContactEditor extends Composite implements IsEditor<ListEditor<Contact, ContactEditorWrapper>> {
private class ContactEditorSource extends EditorSource<ContactEditorWrapper> {
#Override
public ContactEditorWrapper create(final int index) {
ContactEditorWrapper contactEditor = new ContactEditorWrapper();
communicationContactsPanel.add(contactEditor);
return contactEditor;
}
#Override
public void dispose(ContactEditorWrapper subEditor) {
subEditor.removeFromParent();
}
#Override
public void setIndex(ContactEditorWrapper editor, int index) {
communicationContactsPanel.insert(editor, index);
}
}
private ListEditor<Contact, ContactEditorWrapper> editor = ListEditor.of(new ContactEditorSource());
public ListEditor<Contact, ContactEditorWrapper> asEditor() {
return editor;
}
}
ContactEditorWrapper:
class ContactEditorWrapper extends Composite implements ContactBaseEditor<Contact>, ValueAwareEditor<Contact> {
private SimplePanel panel = new SimplePanel();
#Path("") ContactBaseEditor<Contact> realEditor;
public ContactEditor() {
initWidget(panel);
}
#Override
public void setValue(Contact value) {
if (value instanceof Address) {
realEditor = new AddressEditor();
panel.setWidget((AddressEditor)realEditor);
}
else if (value instanceof Email) {
realEditor = new EmailEditor();
panel.setWidget((EmailEditor)realEditor);
}
else if (value instanceof PhoneNumber) {
realEditor = new PhoneNumberEditor();
panel.setWidget((PhoneNumberEditor)realEditor);
}
else {
realEditor = null;
}
}
}
Main Editor class:
public class AddEmployeeEditor extends Composite implements Editor<Employee> {
#UiField
ContactEditor contacts;
interface Driver extends SimpleBeanEditorDriver<Employee, AddEmployeeEditor> {
}
public AddEmployeeEditor(final Binder binder) {
driver = GWT.create(Driver.class);
driver.initialize(this);
List<Contact> list = new ArrayList<Contact>();
list.add(new Address());
list.add(new Email());
list.add(new PhoneNumber());
list.add(new PhoneNumber());
Employee employee = new Employee();
employee.setContacts(list);
driver.edit(employee);
}
}
Can anyone tell me if this will work, am I going in the right direction or ?
Thanks in advance,
Mac
I have updated the code above to now contain the ContactEditorWrapper class that Thomas advised.
That code has good chances to break: there's no guarantee that an editor returned by the EditorSource won't be used to edit another value in the list.
You should create a wrapper editor implementing ValueAwareEditor and with the actual editor as a child editor with #Path(""); and create the appropriate ContactBaseEditor in the setValue method.
class ContactEditor extends Composite implements ValueAwareEditor<Contact> {
private SimplePanel panel = new SimplePanel();
#Path("") ContactBaseEditor realEditor;
public ContactEditor() {
initWidget(panel);
}
#Override
public void setValue(Contact value) {
if (contact instanceof ContactAddress) {
realEditor = new AddressEditor();
}
else if (contact instanceof ContactEmail) {
realEditor = new EmailEditor();
}
else if (contact instanceof ContactPhoneNumber) {
realEditor = new PhoneNumberEditor();
}
else {
realEditor = null;
}
panel.setWidget(realEditor);
}
Note however that only the field/sub-editors from ContactBaseEditor will be edited, whichever the actual implementation being used. If there are additional fields/sub-editors in some ContactBaseEditor subclass, you'd have to implement ValueAwareEditor and handle things by-hand in the setValue and flush methods.