|
| 1 | +--- |
| 2 | +title: "Conditional mapping for source parameters and much more: MapStruct 1.6.0.Beta2 is out" |
| 3 | +author: Filip Hrisafov |
| 4 | +date: "2024-05-11" |
| 5 | +tags: [release, news] |
| 6 | +--- |
| 7 | + |
| 8 | +It's my pleasure to announce the second Beta release of MapStruct 1.6. |
| 9 | + |
| 10 | +The new release comes with some new functionality, e.g.: |
| 11 | + |
| 12 | +* Conditional mapping for source parameters |
| 13 | +* Support to access source property name in condition methods via an annotation |
| 14 | + |
| 15 | +This release is also our first release that has been fully automated using the great [JReleaser](https://jreleaser.org/). |
| 16 | +This would hopefully mean that we can realase more often, as the burden of manual release has been reduced. |
| 17 | + |
| 18 | +We'd like to thank our new supporters: |
| 19 | + |
| 20 | +* [Cybozu](https://github.com/cybozu) |
| 21 | +* [Juyoung Kim](https://github.com/kjuyoung) |
| 22 | +* [Lansana](https://opencollective.com/lansana) |
| 23 | +* [Mariselvam](https://github.com/marisnb) |
| 24 | +* [PRISMA European Capacity Platform GmbH](https://github.com/jan-prisma) |
| 25 | +* [St. Galler Kantonalbank AG](https://opencollective.com/st-galler-kantonalbank-ag) |
| 26 | + |
| 27 | +And of course thanks to our previous supporters: |
| 28 | + |
| 29 | +* [addesso SE](https://github.com/adessoSE) |
| 30 | +* [Bileto](https://opencollective.com/bileto) |
| 31 | +* [Frederik Hahne](https://opencollective.com/atomfrede) |
| 32 | + |
| 33 | +If you'd like to join this list and donate to the project MapStruct is accepting donations through [Open Collective](https://opencollective.com/mapstruct) or [GitHub](https://github.com/sponsors/mapstruct). |
| 34 | + |
| 35 | +<!--more--> |
| 36 | + |
| 37 | +Altogether, not less than [19 issues](https://github.com/mapstruct/mapstruct/issues?q=milestone%3A1.6.0.Beta2) were fixed for this release. |
| 38 | + |
| 39 | +This would not have been possible without our fantastic community of contributors: |
| 40 | + |
| 41 | +* [@dehasi](https://github.com/dehasi) |
| 42 | +* [@hduelme](https://github.com/hduelme) |
| 43 | +* [@mosesonline](https://github.com/mosesonline) |
| 44 | +* [@the-mgi](https://github.com/the-mgi) |
| 45 | +* [@wandi34](https://github.com/wandi34) |
| 46 | + |
| 47 | +* and of course seasoned MapStruct hackers [Ben Zegveld](https://github.com/Zegveld), [Oliver Erhart](https://github.com/thunderhook), [Sjaak Derksen](https://github.com/sjaakd), [Filip Hrisafov](https://github.com/filiphr). |
| 48 | + |
| 49 | +Thank you everyone for all your hard work! |
| 50 | + |
| 51 | +Enough of the pep talk, let's take a closer look at some of the new features and enhancement! |
| 52 | + |
| 53 | +### Conditional mapping for source parameters |
| 54 | + |
| 55 | +It is now possible to use custom condition checks for source parameters. |
| 56 | +In order to support this we have enhanced the existing `@Condition` annotation with a new attribute `appliesTo` and a new `ConditionStrategy`. |
| 57 | + |
| 58 | +By default, the `@Condition` is only applicable to properties. |
| 59 | +However, you co do `@Condition(appliesTo = ConditionStrategy.SOURCE_PARAMETERS)`, or use the meta annotated `@SourceParameterCondition`. |
| 60 | + |
| 61 | +e.g. |
| 62 | + |
| 63 | +{{< prettify java >}} |
| 64 | +public class PresenceCheckUtils { |
| 65 | + |
| 66 | + @SourceParameterCondition |
| 67 | + public static boolean isDefined(Car car) { |
| 68 | + return car != null && car.getId() !+ null; |
| 69 | + } |
| 70 | + |
| 71 | +} |
| 72 | + |
| 73 | +@Mapper(uses = PresenceCheckUtils.class) |
| 74 | +public interface CarMapper { |
| 75 | + |
| 76 | + CarDto map(Car car); |
| 77 | +} |
| 78 | +{{< /prettify >}} |
| 79 | + |
| 80 | +Will generate something like |
| 81 | + |
| 82 | +{{< prettify java >}} |
| 83 | +// GENERATED CODE |
| 84 | +public class CarMapperImpl implements CarMapper { |
| 85 | + |
| 86 | + @Override |
| 87 | + public CarDto map(Car car) { |
| 88 | + if ( !PresenceCheckUtils.isDefined( car ) ) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + // ... |
| 92 | + } |
| 93 | + |
| 94 | + // ... |
| 95 | +} |
| 96 | +{{< /prettify >}} |
| 97 | + |
| 98 | + |
| 99 | +### Support to access source property name in condition methods via an annotation |
| 100 | + |
| 101 | +In 1.6.0.Beta1 we added `@TargetPropertyName` which was giving access to the name of the target property. |
| 102 | +In this release we are adding `@SourceProeprtyName` which gives access to the name of the source property. |
| 103 | + |
| 104 | +e.g. |
| 105 | + |
| 106 | +{{< prettify java >}} |
| 107 | +public class HibernateUtils { |
| 108 | + |
| 109 | + @Condition |
| 110 | + public static boolean isAccessible(Customer customer, @SourcePropertyName String propertyName) { |
| 111 | + return Hibernate.isPropertyInitialized(customer, propertyName); |
| 112 | + } |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +@Mapper(uses = HibernateUtils.class) |
| 117 | +public interface CustomerMapper { |
| 118 | + |
| 119 | + CustomerDto map(Customer customer); |
| 120 | +} |
| 121 | +{{< /prettify >}} |
| 122 | + |
| 123 | +Will generate something like |
| 124 | + |
| 125 | +{{< prettify java >}} |
| 126 | +// GENERATED CODE |
| 127 | +public class CustomerMapperImpl implements CustomerMapper { |
| 128 | + |
| 129 | + @Override |
| 130 | + public CustomerDto map(Customer customer) { |
| 131 | + // ... |
| 132 | + if ( HiberateUtils.isAccessible( customer, "computedOrders" ) ) { |
| 133 | + customer.setOrders( mapOrders( customer.getComputedOrders() ) ); |
| 134 | + } |
| 135 | + // ... |
| 136 | + } |
| 137 | + |
| 138 | + // ... |
| 139 | +} |
| 140 | +{{< /prettify >}} |
| 141 | + |
| 142 | +### Enhancements |
| 143 | + |
| 144 | +* Improve error message when using `target = "."` - This has never been fully been supported by MapStruct, and it might have worked by accident. We are now displaying an improved error message. |
| 145 | +* Improve error messages for auto generated mappings - MapStruct will now show the user defined method and the mapper leading to the error. |
| 146 | +* Remove unnecessary casts to long in generated code. |
| 147 | + |
| 148 | +### Download |
| 149 | + |
| 150 | +This concludes our tour through MapStruct 1.6 Beta2. |
| 151 | +If you'd like to try out the features described above, you can fetch the new release from Maven Central using the following GAV coordinates: |
| 152 | + |
| 153 | +* Annotation JAR: [org.mapstruct:mapstruct:1.6.0.Beta2](http://search.maven.org/#artifactdetails|org.mapstruct|mapstruct|1.6.0.Beta2|jar) |
| 154 | +* Annotation processor JAR: [org.mapstruct:mapstruct-processor:1.6.0.Beta2](http://search.maven.org/#artifactdetails|org.mapstruct|mapstruct-processor|1.6.0.Beta2|jar) |
| 155 | + |
| 156 | +Alternatively, you can get ZIP and TAR.GZ distribution bundles - containing all the JARs, documentation etc. - [from GitHub](https://github.com/mapstruct/mapstruct/releases/tag/1.6.0.Beta2). |
| 157 | + |
| 158 | +If you run into any trouble or would like to report a bug, feature request or similar, use the following channels to get in touch: |
| 159 | + |
| 160 | +* Get help in [GitHub Discussions](https://github.com/mapstruct/mapstruct/discussions) or [StackOverflow](https://stackoverflow.com/questions/tagged/mapstruct) |
| 161 | +* Report bugs and feature requests via the [issue tracker](https://github.com/mapstruct/mapstruct/issues) |
| 162 | +* Follow [@GetMapStruct](https://twitter.com/GetMapStruct) on Twitter |
0 commit comments