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

"No converter found capable of converting from type" in Spring Boot Starter Web project #40

Closed
marvk opened this issue Mar 1, 2022 · 4 comments

Comments

@marvk
Copy link

marvk commented Mar 1, 2022

Related to #22

We're trying to integrate mapstruct-spring-extensions into a project and hit a little bit of a roadblock.

I was under the impression that, when defining a mapper, extending Converter and adding a @Mapper annotation would be enough for ConversionService to pick this mapper up automatically. However, this isn't happening in my Spring Boot project with Spring Boot Starter Web.

This is how I defined a Mapper:

@Mapper
interface FooMapper : Converter<Foo, FooDto> {
    override fun convert(source: Foo): FooDto
}

I've built an MCVE so that you can verify my test setup.

FooMapperImpl is being generated as expected and so is ConversionServiceAdapter, and yet, ConversionService fails to convert from Foo to FooDto.

Am I missing something here? Is there some configuration I need to do after all? I feel like I exhausted the available documentation on this issue. Thanks in advance!

edit: It doesn't seem to be a problem with Spring. I've just tested it, and Converters that aren't generated are detected automatically.

@filiphr
Copy link
Member

filiphr commented Mar 1, 2022

From what I can see you have @Mapper which means that the default component model is used, which means that the generated mapper is not a Spring bean. In order for things to work properly with spring your mappers need to be spring beans as well. i.e. you need to use @Mapper(componentModel = "spring")

@Chessray
Copy link
Collaborator

Chessray commented Mar 1, 2022

Hi,

On top of what @filiphr already pointed out, the project is missing the central configuration which allows e.g. specifying the package name for the generated Adapter class. Without this, the Adapter would be generated into the default package which you typically don't want inside a SpringBoot project, because the component scan will miss it.

@Chessray Chessray closed this as completed Mar 1, 2022
@marvk
Copy link
Author

marvk commented Mar 1, 2022

@filiphr Yeah thanks, we found that out minutes before your comment!

We just kinda followed the docs on this one and they're are ambiguous at best.

The beginning of §3 very much reads like only @Mapper is required to make the ConversionService pick up the generated converters. I don't know if this is an oversight or not, but I would kindly suggest describing that componentModel is required for this.

Thank you guys for the fast responses!

@Chessray
Copy link
Collaborator

Chessray commented Mar 1, 2022

Thanks for the feedback. We'll consider it. :-)

On another note: If all you want to do is register Mappers as converters, you don't need the extensions project. This is only required for more advanced use cases.

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

No branches or pull requests

3 participants