Skip to content

How to map inner generic type? #499

Answered by oraad
oraad asked this question in Q&A
Discussion options

You must be logged in to vote

One solution/workaround I did, is to use mapDefer since I have two types to be passed in this case.

createMap(mapper, PageableDto, Pageable,
    forMember(dest => dest.content,
        mapDefer(src => {
            if (src.content?.[0] instanceof BookDto)
                return mapWith(Book, BookDto, src => src.content)

            if (src.content?.[0] instanceof MovieDto)
                return mapWith(Movie, MovieDto, src => src.content)

            // handle all other types not yet specified as is
            return mapFrom(src => src.content)
        })
    )
)

The problem with this solution, is that it becomes very verbose if we have more inner types to map from.

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by nartc
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants