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

Map lists #65

Closed
hypercube-software opened this issue Feb 27, 2023 · 6 comments · Fixed by #73
Closed

Map lists #65

hypercube-software opened this issue Feb 27, 2023 · 6 comments · Fixed by #73
Assignees
Labels
good first issue Good for newcomers

Comments

@hypercube-software
Copy link

hypercube-software commented Feb 27, 2023

It seems that mapping lists is not really possible, the generated code is not able to understand the type of the list:

public List<MyTarget> mapListToList(final List<MySource> source) {
       return conversionService.convert(source,List.class)
}

I have also the same generated method 2 times if I have the conversion in the both directions. the compiler end up saying "name clash: mapListToList.... and mapListToList have the same type erasure"

For lists, another method should be used in the interface ConversionService (see this stackoverflow):

Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType)

It would be nice if the documentation could say something about this

@Chessray
Copy link
Collaborator

Chessray commented Mar 4, 2023

This is a funny one. The linked solution using TypeDescriptors relies on another Spring-internal mechanism.

Looking at this a bit closer: What is your actual use case? Are you trying to define a Converter<List<A>, List<B>>?

@hypercube-software
Copy link
Author

indeed, I just want to convert a List to a List

@coding-guo
Copy link

I have the same problem,I want to achieve such a requirement,I defined two mapper

First:
@Mapper(componentModel = "spring") public interface AreaMapperStruct extends Converter<List<Area>, List<AreaSimpleOutDTO>> { @Override List<AreaSimpleOutDTO> convert(List<Area> areaList); }

The second:
@Mapper(componentModel = "spring") public interface PartTypeMapperStruct extends Converter<List<PartType>, List<PartTypeOut>> { @Override List<PartTypeOut> convert(List<PartType> partTypes); }
Compile Error:
`public class ConfigurationServiceAdapter {
private final ConversionService conversionService;

public ConfigurationServiceAdapter(
@qualifier("partTypeConversionService") @lazy final ConversionService conversionService) {
this.conversionService = conversionService;
}

public List mapListToList(final List source) {
return conversionService.convert(source, List.class);
}

public DeviceBrand mapDeviceBrandOptDTOToDeviceBrand(final DeviceBrandOptDTO source) {
return conversionService.convert(source, DeviceBrand.class);
}

public AlarmCode mapAlarmCodeOptDTOToAlarmCode(final AlarmCodeOptDTO source) {
return conversionService.convert(source, AlarmCode.class);
}

public List mapListToList(final List source) {
return conversionService.convert(source, List.class);
}
}`

have two "mapListToList" method,how can i solve this problem

@Chessray
Copy link
Collaborator

Chessray commented Mar 8, 2023

Will have to look into this a bit. Until we can provide a full solution, the best thing I can suggest is not implementing the List Mapper as a Spring Converter and following standard MapStruct instead.

@Chessray Chessray self-assigned this Mar 8, 2023
@coding-guo
Copy link

Thank you and look forward to a better solution!!

Will have to look into this a bit. Until we can provide a full solution, the best thing I can suggest is not implementing the List Mapper as a Spring Converter and following standard MapStruct instead.

@Chessray
Copy link
Collaborator

Chessray commented Mar 9, 2023

Some notes:

  • The method TypeDescriptor.collection explicitly states that it's only applicable to anything extending the Collection interface. We should be able to check that.
  • Cater for nested Generics, e.g. List<Set<Object>>.
  • If we do this, we want to do it right.

@Chessray Chessray added the good first issue Good for newcomers label Mar 18, 2023
@Chessray Chessray linked a pull request Apr 21, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants