Skip to content

Conversation

@paparadva
Copy link
Contributor

@paparadva paparadva commented Oct 30, 2022

Check that a parameter annotated with @TargetPropertyName is String and report an error.

With the following mapper:

@Mapper
public interface ErroneousNonStringTargetPropertyNameParameter {

    Employee map(EmployeeDto employee);

    @Condition
    default boolean isNotBlank(String value, @TargetPropertyName int propName) { // propName is int
        return value != null && !value.trim().isEmpty();
    }
}

Compilation will fail with this error message:

error: @TargetPropertyName can only by applied to a String parameter.

Fixes #2863

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.

The PR is great @paparadva.

I've only left one small comment around the error. You can actually see how it will look better in the error line if the erroneous mapper has the method like:

default boolean isNotBlank(
    String value,
    @TargetPropertyName int propName
) {
        return value != null && !value.trim().isEmpty();
}

@paparadva paparadva force-pushed the 2863-target-property-name-type-validation branch from 5c16a88 to a70bdbb Compare May 21, 2023 12:16
@paparadva
Copy link
Contributor Author

I updated isValidPresenceCheckMethod, the error message now points to propName:

    default boolean isNotBlank(String value, @TargetPropertyName int propName) {
                                             ^

@paparadva paparadva requested a review from filiphr May 21, 2023 12:22
@filiphr filiphr merged commit 7c90592 into mapstruct:main May 21, 2023
@filiphr
Copy link
Member

filiphr commented May 21, 2023

Thanks for your work on this @paparadva

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.

Add validation of String type for @TargetPropertyName

2 participants