kylie.zoltan
kylie.zoltan

Reputation: 469

Mapstruct - Multiple parameters to inner class

I'm trying to map to a inner class but it's not working.

I have the following Pojos:

public record Author(UUID id, String name) {}

public record Book(Author author) {}

And this is the mapper:

@Mapper
public interface BookMapper {
    @Mapping(target="author", source=".");
    Book map(UUID id, String name);

    Author map(UUID id, String name);
}

But I get this error when compiling:

BookMapperImpl is not abstract and does not override abstract method map(UUID,String)

Any help is appreciated.

Thanks

Upvotes: 0

Views: 801

Answers (1)

Filip
Filip

Reputation: 21423

BookMapperImpl is not abstract and does not override abstract method map(UUID,String)

That is a strange error, it means that MapStruct generated some partial implementation due to something else, but it didn't report an addition error. This is most likely a bug in MapStruct.

Nevertheless, using source = "." doesn't work with multiple source parameters and even without that it won't be able to pass the mapping to the Author mapping.

I would suggest using a custom method for mapping this e.g.

@Mapper
public interface BookMapper {
    @Mapping(target="author", source=".");
    default Book map(UUID id, String name) {
        Author author = map(id, name);
        return author != null ? new Book(author) : null;
    }

    Author map(UUID id, String name);
}

Upvotes: 0

Related Questions