Using @Context for a mapstruct mapper is considered like an additional parameter
I have a mapper where I'm trying to use a repository as context so I can fetch my object during the mapping. I spent a lot of time searching how context work and this is what I came up with
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING, nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
public interface EnumValueMapper {
@Mapping(target = "enumDefinition", source = "enumDefinitionId", qualifiedByName = "getEnumDefinition")
EnumValue toEntity(EnumValueBean enumValueBean);
@Mapping(target = "enumDefinitionId", source = "enumDefinition.name")
EnumValueBean toBean(EnumValue enumValue);
@Named("getEnumDefinition")
static EnumDefinition getEnumDefinition(String enumDefinitionName, @Context IEnumDefinitiontDao enumDefinitionDao) {
return enumDefinitionDao.findById(enumDefinitionName).orElse(null);
}
}
The issue with this is that I get the error
Qualifier error. No method found annotated with @Named#value: [ getEnumDefinition ]. See https://mapstruct.org/faq/#qualifier for more info.
due to the compiler seeing @Context as a 2nd parameter and not finding a named "getEnumDefinition" that matches the 1 String parameter method. If I remove the @Context parameter the error disappears and the build succeed. I'm confused at what I'm doing wrong because I didn't see anyone explicitly giving a context inside a @Mapping, they just give their source and the name for the qualifiedByName and it finds the correct method despite the @Context.
Comments
Post a Comment