Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type use , An error was obtained --- Can't generate mapping method for a generic type variable source. #2954

Open
lixiao1995 opened this issue Aug 11, 2022 · 12 comments · May be fixed by #2959

Comments

@lixiao1995
Copy link

lixiao1995 commented Aug 11, 2022

background

I want to mapping entity to VO for PageInfo,The code :

@Mapper
public interface PageInfoConvert {
    PageInfoConvert INSTANCE = Mappers.getMapper(PageInfoConvert.class);

    @Mapping(target = "list",expression  = "java(voList)")
    <T,S> PageInfo<T> convert(PageInfo<S> pageInfo, @Context List<T> voList);
}

When compile, an error was obtained ------ Can't generate mapping method for a generic type variable source.

environment
Jdk : 11
Mapstruct:1.5.0.RC1

I didn't get answer on the Internet
Please help me. Thanks

@Zegveld
Copy link
Contributor

Zegveld commented Aug 11, 2022

The problem is that mapstruct will attempt to map the S onto the T information, but it cannot determine what T or S is.

Perhaps the checking for generics can become less strict, when the fields affected by the generics are already explicitly specified. In your case it is most likely that inside PageInfo<T> exists a List<T> object.

@lixiao1995
Copy link
Author

The problem is that mapstruct will attempt to map the S onto the T information, but it cannot determine what T or S is.

Perhaps the checking for generics can become less strict, when the fields affected by the generics are already explicitly specified. In your case it is most likely that inside PageInfo<T> exists a List<T> object.

@lixiao1995
Copy link
Author

@Zegveld You are right, The List<T> exists. So, Can mapstruct support my demand?

@Zegveld
Copy link
Contributor

Zegveld commented Aug 15, 2022

It is currently not supported, but this is a nice enhancement to be added.

@lixiao1995
Copy link
Author

It is currently not supported, but this is a nice enhancement to be added.

I'll look forward to it.thinks

Zegveld pushed a commit to Zegveld/mapstruct that referenced this issue Aug 15, 2022
@Zegveld
Copy link
Contributor

Zegveld commented Aug 15, 2022

please do not close this issue, as it will be used to track this.

@Zegveld Zegveld reopened this Aug 15, 2022
@lixiao1995
Copy link
Author

please do not close this issue, as it will be used to track this.

got

Zegveld pushed a commit to Zegveld/mapstruct that referenced this issue Aug 15, 2022
@Zegveld Zegveld linked a pull request Aug 15, 2022 that will close this issue
Zegveld pushed a commit to Zegveld/mapstruct that referenced this issue Sep 1, 2022
@xg578962913
Copy link

xg578962913 commented Nov 7, 2022

hi, @Zegveld
MapStruct seems to have to specify the specific class type of the JavaBean. Will this enhancement change this feature
Or this enhancement is only for JavaBean member variables, so that member variables can use generics, but JavaBean class still need to specific class, and generics cannot be used
Like this?Such conversion will become possible?

class JavaBean01<A> {
    A object1;
   ...
}

class JavaBean02<A>{
    A object1;
   ...
}

Such conversion is still impossible?
<S, T> T mapper(S s, Class<T> t)

Or will both of these situations become possible

@filiphr
Copy link
Member

filiphr commented Nov 12, 2022

@xg578962913 a generic conversion such as <S, T> T mapper(S s, Class<T> t) is not possible in MapStruct and we are not planning to implement something like that.

If you are interested in something like that then you could implement it in your own way by implementing a custom processor that is going to create an implementation that works for something like this.

@xg578962913
Copy link

@filiphr Thanks, I got it

@thedrow
Copy link

thedrow commented Jun 2, 2023

@filiphr I have another use case which should be supported:

@Mapper
abstract class ServiceRequestMapper {
    @Mapping(target = ".", source = "request")
    fun <Request: IdlData<Request>> toRequest(serviceRequest: ServiceRequest<Request>) : Request = serviceRequest.request
}

The ServiceRequest class has a request attribute that is always of type Request.
I don't see why the constraints I provided do not provide enough type information for the automatic generation of the mapper.

@filiphr
Copy link
Member

filiphr commented Jun 16, 2023

@thedrow this looks like Kotlin code. How does the java code for this example look like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants