diff --git a/README.md b/README.md
index d802749..218b0c8 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,7 @@ Currently, the following examples exist:
* _mapstruct-on-ant_: Shows how to use MapStruct in Ant-based projects; to build this example, run `ant build` on the command line
* _mapstruct-on-gradle_: Shows how to use MapStruct in Gradle-based projects; to build the example project, run `./gradlew clean build` on the command line
+* _mapstruct-immutables_: Shows how to use MapStruct together with [Immutables](https://immutables.github.io/).
* _mapstruct-lombok_: Shows how to use MapStruct together with Lombok (with both a Maven `pom.xml` and a Gradle `build.gradle`); to build the example project, run either `mvn clean install` or `./gradlew clean build` on the command line
* _mapstruct-iterable-non-iterable_: Shows how by means of a mapper util class conversions can be made from an iterable- to its non-iterable element
* _mapstruct-mapping-from-map_: Shows how by means of a mapper util class and qualifiers extracting value can be carried out on Maps. Watch [mapstruct/mapstruct#1075](https://github.com/mapstruct/mapstruct/issues/1075) for native support.
diff --git a/mapstruct-immutables/pom.xml b/mapstruct-immutables/pom.xml
new file mode 100644
index 0000000..21432b5
--- /dev/null
+++ b/mapstruct-immutables/pom.xml
@@ -0,0 +1,125 @@
+
+
+
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.example.immutables; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import org.immutables.value.Value; + + +@Value.Immutable +@JsonDeserialize(builder = ApiPerson.Builder.class) +@ExampleStyle +public interface ApiPerson { + String getId(); + + String getFirstName(); + + String getFullName(); + + String getMapperDoingFullName(); + + // Required for MapStruct to find the builder + static Builder builder() { + return new Builder(); + } + + // And Builder resides in generated Immutable implementation, which is private protected. + // See https://immutables.github.io/json.html#jackson + class Builder extends ImmutableApiPerson.Builder { + } + +} \ No newline at end of file diff --git a/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/DtoPerson.java b/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/DtoPerson.java new file mode 100644 index 0000000..e97ea1d --- /dev/null +++ b/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/DtoPerson.java @@ -0,0 +1,47 @@ +/** + * Copyright 2020 MapStruct developers + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.example.immutables; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import org.immutables.value.Value; + +@Value.Immutable +@JsonDeserialize(builder = DtoPerson.Builder.class) +@ExampleStyle +public interface DtoPerson { + String getUuid(); + + String getFirstName(); + + String getLastName(); + + default String getFullName() { + return getFirstName() + " " + getLastName(); + } + + // Required for MapStruct to find the builder + static Builder builder() { + return new Builder(); + } + + // And Builder resides in generated Immutable implementation, which is private protected. + // See https://immutables.github.io/json.html#jackson + class Builder extends ImmutableDtoPerson.Builder { + } +} \ No newline at end of file diff --git a/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ExampleStyle.java b/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ExampleStyle.java new file mode 100644 index 0000000..5718549 --- /dev/null +++ b/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ExampleStyle.java @@ -0,0 +1,37 @@ +/** + * Copyright 2020 MapStruct developers + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.example.immutables; + + +import org.immutables.value.Value; + +import static org.immutables.value.Value.Style.ImplementationVisibility.PACKAGE; + +/** + * Our ExampleStyle to make it easier to reuse annotations on our org.immutables interfaces. + *
+ * Details: https://immutables.github.io/style.html + *
+ * builder = "new" is required for serializing with jackson
+ * visibility to package is nice to hide implementation details from users using our builders
+ * overshadowImplementation sets return types on builders to be the interface instead of the generated immutable class. ( https://immutables.github.io/immutable.html#builder )
+ */
+@Value.Style(builder = "new", visibility = PACKAGE, overshadowImplementation = true)
+public @interface ExampleStyle {
+}
diff --git a/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ResponseMapper.java b/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ResponseMapper.java
new file mode 100644
index 0000000..cc17831
--- /dev/null
+++ b/mapstruct-immutables/src/main/java/org/mapstruct/example/immutables/ResponseMapper.java
@@ -0,0 +1,33 @@
+/**
+ * Copyright 2020 MapStruct developers
+ * and/or other contributors as indicated by the @authors tag. See the
+ * copyright.txt file in the distribution for a full listing of all
+ * contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mapstruct.example.immutables;
+
+import org.mapstruct.Mapper;
+import org.mapstruct.Mapping;
+import org.mapstruct.factory.Mappers;
+
+@Mapper
+public interface ResponseMapper {
+
+ ResponseMapper INSTANCE = Mappers.getMapper(ResponseMapper.class);
+
+ @Mapping(source = "uuid", target = "id")
+ @Mapping(expression = "java(person.getFirstName() + \" \" + person.getLastName())", target = "mapperDoingFullName")
+ ApiPerson mapFrom(DtoPerson person);
+}
diff --git a/mapstruct-immutables/src/test/java/org/mapstruct/example/immutables/ResponseMapperTest.java b/mapstruct-immutables/src/test/java/org/mapstruct/example/immutables/ResponseMapperTest.java
new file mode 100644
index 0000000..f6bed74
--- /dev/null
+++ b/mapstruct-immutables/src/test/java/org/mapstruct/example/immutables/ResponseMapperTest.java
@@ -0,0 +1,59 @@
+/**
+ * Copyright 2020 MapStruct developers
+ * and/or other contributors as indicated by the @authors tag. See the
+ * copyright.txt file in the distribution for a full listing of all
+ * contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mapstruct.example.immutables;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ResponseMapperTest {
+
+ @Test
+ public void testResponseMapper() {
+ DtoPerson.Builder barBuilder = new DtoPerson.Builder()
+ .uuid("foo")
+ .firstName("John")
+ .lastName("Snow");
+
+
+ assertThat(ResponseMapper.INSTANCE.mapFrom(barBuilder.build()))
+ .isEqualTo(new ApiPerson.Builder()
+ .id("foo")
+ .firstName("John")
+ .fullName("John Snow")
+ .mapperDoingFullName("John Snow")
+ .build());
+
+ }
+
+ @Test
+ public void testResponseMapperWithSerializingPersonToJson() throws JsonProcessingException {
+ DtoPerson.Builder personFromDatabase = new DtoPerson.Builder()
+ .uuid("foo")
+ .firstName("John")
+ .lastName("Snow");
+
+ ObjectMapper mapper = new ObjectMapper();
+ String json = mapper.writeValueAsString(ResponseMapper.INSTANCE.mapFrom(personFromDatabase.build()));
+ assertThat(json).isEqualTo("{\"id\":\"foo\",\"firstName\":\"John\",\"fullName\":\"John Snow\",\"mapperDoingFullName\":\"John Snow\"}");
+ }
+
+}
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 3d256ee..628634a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -31,6 +31,7 @@
mapstruct-field-mapping
mapstruct-iterable-to-non-iterable
mapstruct-lombok
+ mapstruct-immutables
mapstruct-mapping-from-map
mapstruct-mapping-with-cycles
mapstruct-nested-bean-mappings