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

#2946 allow update methods with @SubclassMapping #3330

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

D3PSI
Copy link

@D3PSI D3PSI commented Jul 16, 2023

this change implements the functionality to generate update methods
(@MappingTarget methods) in conjunction with @SubclassMapping

this change implements the functionality to generate update methods
(`@MappingTarget` methods) in conjunction with `@SubclassMapping`
@D3PSI D3PSI changed the title \#2946 allow update methods with @SubclassMapping #2946 allow update methods with @SubclassMapping Jul 16, 2023
Copy link
Member

@filiphr filiphr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this @D3PSI. I've left some comments.

In addition to the comments I have some other remarks:

  • Please add some more tests, perhaps by copying the existing tests for normal mapping and applying them for update mappings
  • How does the check for subclass mapping exhaustion look like? From what I saw we are doing if (source instanceof SourceType && target instanceof TargetType) then the mapping is done. However, this means that people can make mistakes. Therefore, we need to add some errors and other things like that for this. There are also other scenarios where the target type you are updating might not matter.

Having written the second remark we might need to think for a bit better solution for subclass update mappings. Especially in combinations of what you would like to upgrade and from what.

e.g. Consider the following

class Vehicle {}
class Car extends Vehicle {}
class Bike extends Vehicle {}
class VehicleDto {}
class CarDto extends VehicleDto {}
class BikeDto extends VehicleDto {}

When doing update mappings there are various scenarios

@Mapper
public interface CustomMapper {

    void update(@MappingTarget Vehicle target, VehicleDto source);

}

So what you can now do is the following:

  • Use CarDto, BikeDto or VehicleDto to update the target
  • Use CarDto to update the Car target
  • Use CarDto to update the Bike target
    ...

As you can see with updates there are way more possible combinations that can be done.

ParameterBinding.fromParameters( methodRef.getParameters() )
ParameterBinding.fromParameters( methodRef.getSourceParameters() )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

Comment on lines +1958 to +1961
public boolean isUpdateMethod() {
return Parameter.getMappingTargetParameter( getParameters() ) != null;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and use isExistingInstanceMapping instead

Comment on lines +174 to +176
if ( basedOn.isUpdateMethod() ) {
this.parameters.add( Parameter.forForgedMappingTarget( returnType ) );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Why don't we pass everything in the additional parameters?

void unsupportedUpdateMethod() {
@WithClasses({ SubclassUpdateMapper.class })
@ExpectedCompilationOutcome(value = CompilationResult.SUCCEEDED)
void updateMethod() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this test and write more thorough tests

@s-jepsen
Copy link

s-jepsen commented Sep 4, 2023

Any progress on this PR?

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

Successfully merging this pull request may close these issues.

None yet

3 participants