mapstruct / mapstruct-idea

An IntelliJ IDEA plugin for working with MapStruct
Other
136 stars 38 forks source link

support Map as MappingSource #168

Closed hduelme closed 7 months ago

hduelme commented 9 months ago

I added support for Map as MappingSource. If only Map<String, ?> as a valid source is present, unmapped target warnings/errors are suppressed. This fixes #167 I also added an inspection if the Map is raw or does not have a String as a key, like described in MapToBeanRawMapMapper and in MapToBeanTypeCheckMapper. For this I added two fixes, add <String, String> if the Map is raw and replace the key with String if the key is not of type String.

filiphr commented 8 months ago

Thanks for your work on this @hduelme and thanks for the review @thunderhook.

I also had a look. I think that the inspection should be on the warning level by default, in order to be aligned to what we are doing in the processor. Apart from that I think we need to be a but more lax, e.g. in the MapToBeanRawMapper and MapToBeanTypeCheckMapper if you add a target parameter matching the map then it would work. Also if you have a look at MapToBeanNonStringMapAsMultiSourceMapper you'll see that it is possible to use non string map as a source if the source matches the target mapping.

hduelme commented 8 months ago

@filiphr, @thunderhook, thanks for the feedback. I've fixed the typos, removed the unused intention, and used lowercase string in the properties file. I also switched the default level to WARNING for FromMapMappingMapTypeInspection. Additionally, I disabled the inspection if the name of the map parameter matches a target parameter or if the map parameter is used in a source mapping. The inspection only runs if the map parameter is the only source parameter and is not used for mapping directly.

For the UnmappedTargetPropertiesInspection I changed the check so that the inspection is disabled if the key of one map parameter is of type string. One thing I found during the investigation is that maps, arrays, and iterables are currently not recognized as source parameters when used as method parameters. The correct behavior would be to use the method parameter name as a source.

filiphr commented 7 months ago

Thanks @hduelme. Sorry that it took me a bit to review it and merge it.

One thing I found during the investigation is that maps, arrays, and iterables are currently not recognized as source parameters when used as method parameters. The correct behavior would be to use the method parameter name as a source.

I am not sure that I understand what you meant with this. However, feel free to create an issue and a fix if you know what the bug is.

hduelme commented 7 months ago

No problem. Thanks for having a look at this @filiphr I will open a bug and provide a example for the problem.