Skip to content

Commit 50ee332

Browse files
Chessrayfiliphr
authored andcommitted
Additional FAQ for using @context in Spring Converters - we've had this question several times.
1 parent 86fb3c8 commit 50ee332

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

content/faq/_index.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,21 @@ the method `doSomething` will be ignored by MapStruct.
337337

338338
{{% /faq_question %}}
339339

340+
{{% faq_question "How do I write a Spring Extension Converter using MapStruct's @Context feature?" %}}
341+
342+
The unfortunate short answer: You can't.
343+
Spring's [Converter definition](https://docs.spring.io/spring-framework/reference/core/validation/convert.html#core-convert-Converter-API)
344+
only allows for a `convert` method that has a single input parameter (the source object) and returns the converted (mapped) response.
345+
346+
If you ever find yourself in a situation where you'd like to combine Spring's `Converter` and MapStruct's `@context`
347+
concepts, your options are:
348+
349+
* Instead of supplying a `@Context` object as parameter, consider injecting it.
350+
* If injecting the object is no option, sometimes you may be able to inject some form of modifiable wrapper object that
351+
can be given the `@Contet` object at runtime.
352+
* If neither of the above two are possible and you can't implement a mapping without a `@Context` object, you should
353+
stick to MapStruct's core functionality and refrain from letting this particular Mapper extend the `Converter`
354+
interface.
355+
356+
{{% /faq_question %}}
357+

0 commit comments

Comments
 (0)