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

#3539 Support BeanMapping#ignoredTargets instead of repeating @Mapping(target = "foo", ignore = true) #3545

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

chenzijia12300
Copy link
Contributor

This commit attempts to implement #2599, mapping BeanMapping#ignoredTargets to multiple Mapping#ignore annotations.

If there are any necessary changes, I'll do my best to address them.

@filiphr
Copy link
Member

filiphr commented Mar 10, 2024

Thanks for your PR @chenzijia12300.

Can we perhaps add a test case when an unknown property is used in BeanMapping#ignoreTargets? How would the compile error look like? Where would it be reported?

@chenzijia12300
Copy link
Contributor Author

Hi @filiphr, thanks for your review, I have added a test case

Copy link

@zyberzebra zyberzebra left a comment

Choose a reason for hiding this comment

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

Just checked it out and had a look :)

@ProcessorTest
@WithClasses({Source.class, Target.class, BeanMappingIgnoreTargetMapper.class})
public void shouldIgnoreTargetProperty() {
BeanMappingIgnoreTargetMapper.INSTANCE.convert( new Source() );
Copy link

@zyberzebra zyberzebra May 7, 2024

Choose a reason for hiding this comment

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

Shouldn't there be an assertion? :)

    public void shouldIgnoreTargetProperty() {
        Source source = new Source();
        Source.NestedSource nested = new Source.NestedSource();
        source.setAge( 18 );
        source.setName( "John" );
        source.setNested( nested );
        nested.setScore( 50.0 );
        Target convert = BeanMappingIgnoreTargetMapper.INSTANCE.convert( source );
        assertThat(convert).hasNoNullFieldsOrPropertiesExcept( "address" );
        assertThat(convert.getNested()).hasNoNullFieldsOrPropertiesExcept( "flag" );
    }

SHouldn't we add a test like that? :) Or is it enough thats it does not generate an error that we have unmapped targets

Copy link
Member

Choose a reason for hiding this comment

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

I agree about this one. We should add an assertion as well.

@@ -284,6 +285,20 @@ else if ( !method.isUpdateMethod() ) {

initializeMappingReferencesIfNeeded( resultTypeToMap );

if ( beanMapping != null ) {

Choose a reason for hiding this comment

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

Maybe a new method?

@zyberzebra
Copy link

I guess we have to update the intelliJ plugin if this is implemented ;)
image

Mappers.getMapper( BeanMappingIgnoreTargetMapper.class );

@BeanMapping( ignoreTargets = {"nested.flag", "address"} )
Target convert( Source source );

Choose a reason for hiding this comment

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

    @Mapping( target = "address", ignore = true)
    @BeanMapping( ignoreTargets = {"nested.flag", "address"} )
    Target convert( Source source );

Should this lead to warning? What happens if we do that.

BeanMappingIgnoreTargetMapper INSTANCE =
Mappers.getMapper( BeanMappingIgnoreTargetMapper.class );

@BeanMapping( ignoreTargets = {"nested.flag", "address"} )
Copy link

@zyberzebra zyberzebra May 7, 2024

Choose a reason for hiding this comment

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

Shouldn't this also lead to: Target property "address" must not be mapped more than once.

    @BeanMapping( ignoreTargets = {"nested.flag", "address", "address"} )

this leads to: Target property "address" must not be mapped more than once.

    @Mapping( target = "address", ignore = true)
    @Mapping( target = "address", ignore = true)

does

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.

I think that the best solution for this would be to do it in the MapperCreationProcessor similar to how we are doing it for @BeanMapping( ignoreByDefault = true ).

Additionally, as @zyberzebra mentioned in some other comments. We need to make sure that people do not use duplicates and that you can't define things in @BeanMapping#ignoreTargets and @Mapping in the same time.

diagnostics = {
@Diagnostic(type = ErroneousBeanMappingIgnoreTargetMapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 19,
Copy link
Member

Choose a reason for hiding this comment

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

The reported line should be on the @BeanMapping not on the method.

@ProcessorTest
@WithClasses({Source.class, Target.class, BeanMappingIgnoreTargetMapper.class})
public void shouldIgnoreTargetProperty() {
BeanMappingIgnoreTargetMapper.INSTANCE.convert( new Source() );
Copy link
Member

Choose a reason for hiding this comment

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

I agree about this one. We should add an assertion as well.

@chenzijia12300
Copy link
Contributor Author

@zyberzebra @filiphr Thank you very much for your review. I will try to modify these problems.

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