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

Add a MapStruct processor option to fail if mappers referenced in uses attribute are not really used #3579

Open
gclarisse opened this issue Apr 26, 2024 · 4 comments
Labels
enhancement up-for-grabs Issues targeted for community contribution

Comments

@gclarisse
Copy link

Use case

I would like to be able to cause code generation to fail if Mapper1 or Mapper2 are not really used.

@Mapper(uses = { Mapper1.class, Mapper2.class})
public interface MainMapper {

}

Generated Code

Add an option to MapStruct processor to fail if at least one mapper referenced in "uses" attribute will not be used in generated code.

For example an option similar to mapstruct.unmappedTargetPolicy / mapstruct.unmappedSourcePolicy :
mapstruct.unusedMappersPolicy = ERROR / WARN / IGNORE

Possible workarounds

No response

MapStruct Version

Mapstruct 1.5.3

@filiphr
Copy link
Member

filiphr commented Apr 27, 2024

Thanks for the suggestion @gclarisse. I'm not too keen on doing something like this. What's the reason that you'd like to add something like this? What kind of issue have you had?

@gclarisse
Copy link
Author

Thanks for your answer @filiphr.

We wanted MainMapper to use Mapper1 but we forgot to put a @Context annotation on a parameter in Mapper1.
Generated code did not inject Mapper1 in MainMapper and use a default implem instead.

We wasted quite a bit of time in analysis, so we would like to have at least a warning in order to be sure that our referenced mappers are used.

@filiphr
Copy link
Member

filiphr commented May 1, 2024

@gclarisse we can keep this open and see how much interest there is or whether someone wants to work on this.

The thing is that even if such an option exists, it could be possible that your Mapper1 has multiple methods and one method is used by MainMapper, but not another one that you want. You could have the exact same problem even without any warning, that's why I'm a bit sceptical about it.

@filiphr filiphr added enhancement up-for-grabs Issues targeted for community contribution labels May 1, 2024
@filiphr filiphr added this to the Future planning milestone May 1, 2024
@zyberzebra
Copy link

zyberzebra commented May 9, 2024

This used property might need a change then, I guess:

    private MapperReference findMapperReference(Method method) {
        for ( MapperReference ref : mapperReferences ) {
            if ( ref.getType().equals( method.getDeclaringMapper() ) ) {
                ref.setUsed( ref.isUsed() || !method.isStatic() );
                ref.setTypeRequiresImport( true );
                return ref;
            }
        }
        return null;
    }

If a static method is used, the flag will still be false, and reporting a warning or error based on this flag wouldn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement up-for-grabs Issues targeted for community contribution
Projects
None yet
Development

No branches or pull requests

3 participants