-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
87 provide a conversionservice bean if missing (#91)
* New module and first quick implementation. * Simple Spring Boot Test employing the new mechanism. * The annotation is actually not MapStruct specific, so let's just call it ConverterScan. * Split service creation from linking converters so the latter can be reused. * Extracted ConverterRegistration into another "runtime" module as it is useful for situations beyond testing, * Quick Unit Test * Quick Unit Test * Refactoring since we will generate several files. * Generate scan and configuration classes for custom bean name. * Compiler warning * Use generated ConverterScan annotation rather than ComponentScan. * Control registration class generation via new attribute. * Modified examples so they use the test extension. * Use ConverterScan where possible * Documentation. Also made generated registration class package-private as only @ConverterScan will be needed in real-life code.
- Loading branch information
Showing
67 changed files
with
2,292 additions
and
515 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
docs/src/docs/asciidoc/chapter-4-external-conversions.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
[[externalConversions]] | ||
== External Conversions | ||
Spring ships with a variety of https://github.com/spring-projects/spring-framework/tree/main/spring-core/src/main/java/org/springframework/core/convert/support[builtin conversions], e.g. `String` to `Locale` or `Object` to `Optional`. In order to use these (or your own conversions from another module) in the same fashion, you can add them as `externalConversions` to your `SpringMapperConfig`: | ||
==== | ||
[source, java, linenums] | ||
[subs="verbatim,attributes"] | ||
---- | ||
import org.mapstruct.MapperConfig; | ||
import org.mapstruct.extensions.spring.ExternalConversion; | ||
import org.mapstruct.extensions.spring.SpringMapperConfig; | ||
import java.util.Locale; | ||
@MapperConfig(componentModel = "spring") | ||
@SpringMapperConfig( | ||
externalConversions = @ExternalConversion(sourceType = String.class, targetType = Locale.class)) | ||
public interface MapstructConfig {} | ||
---- | ||
==== | ||
|
||
The processor will add the corresponding methods to the generated adapter so MapStruct can use them in the same fashion as the ones for the Converter Mappers in the same module: | ||
==== | ||
[source, java, linenums] | ||
[subs="verbatim,attributes"] | ||
---- | ||
import java.lang.String; | ||
import java.util.Locale; | ||
import javax.annotation.Generated; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.core.convert.ConversionService; | ||
import org.springframework.stereotype.Component; | ||
@Generated( | ||
value = "org.mapstruct.extensions.spring.converter.ConversionServiceAdapterGenerator", | ||
date = "2021-06-25T18:51:21.585Z" | ||
) | ||
@Component | ||
public class ConversionServiceAdapter { | ||
private final ConversionService conversionService; | ||
public ConversionServiceAdapter(@Lazy final ConversionService conversionService) { | ||
this.conversionService = conversionService; | ||
} | ||
public Locale mapStringToLocale(final String source) { | ||
return (Locale) conversionService.convert(source, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Locale.class)); | ||
} | ||
} | ||
---- | ||
==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[[testExtensions]] | ||
== Test Extensions | ||
|
||
[[converterScan]] | ||
=== ConverterScan | ||
|
||
In a production environment, Spring will take care of initializing the `ConversionService` and registering all generated Mappers inside it. | ||
However, in integration tests, the developer will typically have to take care of all that themselves. | ||
In order to simplify this task, the `test-extensions` module provides a `@ConverterScan` annotation which can be used much like Spring's own `@ComponentScan`. | ||
It will perform the same scanning task and also provide a `ConversionService` with all found Mappers already registered inside. | ||
This is sufficient for most tests. | ||
In its simplest form, the annotation can be used like this: | ||
|
||
==== | ||
[source,java,linenums] | ||
[subs="verbatim,attributes"] | ||
---- | ||
@ExtendWith(SpringExtension.class) | ||
class ConversionServiceAdapterIntegrationTest { | ||
@Configuration | ||
@ConverterScan(basePackageClasses = MapperSpringConfig.class) | ||
static class ScanConfiguration {} | ||
@Autowired | ||
private ConversionService conversionService; | ||
[...] | ||
} | ||
---- | ||
==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.