can't map property. Consider to declare/implement a mapping method. Can't generate mapping method from non-iterable type to iterable type
Asked Answered
B

1

7

I can't seem to use mapstruct correctly

@Mapping(target = "products", source = "itemBookType")
SearchBookingResult backToTp(ItemBook itemBook);

When running this code I get the following error:

Can't map property "ProductType itemBookType" to "List<ProductOverview> products". Consider to declare/implement a mapping method: "List<ProductOverview> map(ProductType value)".

I added the following code at the bottom:

List<ProductOverview> map(ProductType value);

but still it returns me the following error:

Can't generate mapping method from non-iterable type to iterable type from java stdlib.

Itembook class:

public class ItemBook {
    private ProductType itemBookType; //ProductType class
    private Integer idref;
    private String reference;
}

ProductType class:

public enum ProductType {
    BOOK, PHONE, GAME
}

SearchBookingResult class:

public class SearchBookingResult extends BaseResponse<SearchBookingResult> {
    private String reference;
    private List<ProductOverview> products;
}

the Mapper

 @Mapper(componentModel = "spring")
    public interface ItemBookMapper {
        ItemBookTpMapper INSTANCE = Mappers.getMapper(ItemBookTpMapper.class);
        @Mapping(target = "reference", source = "idref")
        @Mapping(target = "products", source = "itemBookType")
        SearchBookingResult backToTp(ItemBook itemBook);
        List<ProductOverview> map(ProductType value);
    
    }

ProductOverView class is abstract:

public abstract class ProductOverview implements Serializable {

    private ProductType productType;

    public ProductOverview(ProductType productType) {
        this.productType = productType;
    }

    public ProductType getProductType() {
        return productType;
    }
}

map reference work but products return many error.

Bigmouth answered 9/8, 2022 at 6:53 Comment(9)
You need to implement the method yourself as MapStruct cannot deduce how to convert from your single element to a list element.Flightless
and how? can you give me directions?Unconventionality
By writing the method? How else would you do it? Instead of only the declaration List<ProductOverview> map(ProductType value); write a proper implementation of that method. If you have an interface use a default method else use an abstract class that implements it (and read the documentation of MapStruct which explains this as well).Flightless
can you give the documentation about it?Unconventionality
What is it you don't understand about writing a method? That is all you need to do? You have the signature already now write the implementation.Flightless
I said i wrote it but it still returns errorUnconventionality
No you didn't... You wrote the declaration not the implementation.Flightless
but the problem is that I don't know how to write the implementation, how, where?Unconventionality
You know how to write java don't you? Just instead of a method declaration write the actual method as you would write it in any other java class. It is no different then anything else you write.Flightless
S
10

MapStruct can't generate mapping method from non-iterable type to iterable type because it's impossible to create a generic mapping.

The only solution, as suggested by the exception, is to create a custom method where you can implement your own mapping algorithm.

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

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

    @Mapping(target = "reference", source = "idref")
    @Mapping(target = "products", source = "itemBookType", qualifiedByName = "mapProducts")
    SearchBookingResult backToTp(ItemBook itemBook);

    @Named("mapProducts") 
    default List<ProductOverview> mapProducts(ProductType value){
        List<ProductOverview> products = new ArrayList<>();
        
        //add your custom mapping implementation

        return products;
    }

}
Shemeka answered 9/8, 2022 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.