Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot map nested @Data with MapStruct

When trying to map nested Object using @Data and @Builder, mapStruct throw the following errors: "No read accessor found for property "profile" in target type."

@Mapper(componentModel = "spring")
public interface AuthMapper {

  // only for testing mapping is working
  @Mapping(target = "profile.organization", source = "organization")
  RequestCreateOktaUser toEntity(Integer organization);

  // only for testing mapping is working
  @Mapping(target = "profile.login", source = "request.profile.email")
  RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request);
  
  // Throw error "No read accessor found for property "profile" in target type" at compile time
  @Mapping(target = "profile.organization", source = "organization")
  @Mapping(target = "profile.login", source = "request.profile.email")
  RequestCreateOktaUser toEntity(RequestMobilePreRegisterLocation.User request, Integer organization);

}

Model using Lombok simplified for simplicity

@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class RequestCreateOktaUser {

  private Profile profile;

  @Data
  @NoArgsConstructor
  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  @Builder
  public static class Profile {
    private String login;
    private String email;
    private Integer organization;
  }

}
@Data
@NoArgsConstructor
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class RequestMobilePreRegisterUser {

  @Valid
  private User user;


  @Data
  @NoArgsConstructor
  @AllArgsConstructor(access = AccessLevel.PRIVATE)
  @Builder
  public static class User {

    @Valid
    private Profile profile;

    @Data
    @NoArgsConstructor
    @AllArgsConstructor(access = AccessLevel.PRIVATE)
    @Builder
    public static class Profile {

      @NotEmpty
      @Email
      private String email;

    }

}

The two first Mapping are working as expected, but when trying to combine the two the following errors is being thrown at compile time "No read accessor found for property "profile" in target type."

If someone could help me on this one I would really appreciate.

Thanks a lot,

Jonathan.

like image 316
Jonathan Chevalier Avatar asked Jul 09 '26 02:07

Jonathan Chevalier


1 Answers

solution:

@Mapper(componentModel = "spring", builder = @Builder(disableBuilder = true))
like image 147
Jonathan Chevalier Avatar answered Jul 11 '26 15:07

Jonathan Chevalier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!