A_G
A_G

Reputation: 2370

Inner immutable class with mapStruct

We are using Immutables with MapStruct and ran into a problem while converting an entity to dto.

@Value.Immutable
public interface ProjectDto {
    String getId();
    String getName();
    //ProjectStatisticsDto getStatistics();
}

@Value.Immutable
public interface ProjectStatisticsDto {
    Long getCount();
}

@Immutable
public interface Project extends Serializable {
    @JsonProperty("_id")
    String getId();
    String getName();
    //ProjectStatistics getStatistics();
}

@Immutable
public interface ProjectStatistics extends Serializable {
    Long getCount();
}

The mapper class

@Mapper
public interface ProjectMapper {
    ProjectMapper INSTANCE = Mappers.getMapper(ProjectMapper.class);
    ImmProjectDto toDto(ImmProject project); // This works only when the inner model of project statistics is commented.
    //ProjectDto toDto(Project project); THIS DOES NOT WORK (Error 1)
    // ImmProjectDto toDto(ImmProject project); After I uncomment the inner class of project statistics then even this does not work (Error 2)

In the cases of error, the issue is exactly the same

Error 1: No implementation was created for ProjectMapper due to having a problem in the erroneous element com.xyz.ProjectDto. 
Error 2: No implementation was created for ProjectMapper due to having a problem in the erroneous element com.xyz.ProjectStatisticsDto. 

I checked the tests for mapstruct with immutables and there is nothing different I see https://github.com/mapstruct/mapstruct/blob/master/integrationtest/src/test/resources/immutablesBuilderTest/mapper/src/main/java/org/mapstruct/itest/immutables/Person.java.

I tried removing serialization statements but no luck. I added some verbose statements which say

Note: MapStruct: Immutables found on classpath
Note: MapStruct: Using accessor naming strategy: org.mapstruct.ap.spi.ImmutablesAccessorNamingStrategy
Note: MapStruct: Using builder provider: org.mapstruct.ap.spi.ImmutablesBuilderProvider
Note: MapStruct: Using enum naming strategy: org.mapstruct.ap.spi.DefaultEnumMappingStrategy

And this looks absolutely correct

Upvotes: 1

Views: 895

Answers (1)

Filip
Filip

Reputation: 21423

Looking at the title of the question "Inner immutable class with mapStruct" I guess that your immutable classes are inside another class.

This is a known problem for MapStruct (see mapstruct/mapstruct#2198) that already has a PR for it and it will be fixed in the next non patch release.

In the meantime you will have to make your Immutable classes top level classes.

Upvotes: 1

Related Questions