From 3ebd09eec935ad3ad0caf96a0b458c086fb6cf1e Mon Sep 17 00:00:00 2001 From: Filip Hrisafov Date: Mon, 10 Jul 2017 20:37:09 +0200 Subject: [PATCH] #1244 Fix problems with special word for FreeMarker in some cases --- .../ap/internal/model/BeanMappingMethod.java | 5 +- .../ap/internal/model/common/Parameter.java | 3 +- .../ap/internal/model/BeanMappingMethod.ftl | 10 ++-- .../ap/test/bugs/_1244/Issue1244Test.java | 46 +++++++++++++++ .../ap/test/bugs/_1244/SizeMapper.java | 58 +++++++++++++++++++ .../ap/test/bugs/_909/ValuesMapper.java | 4 ++ 6 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/Issue1244Test.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/SizeMapper.java diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java index 1b0573910..484b93b40 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/BeanMappingMethod.java @@ -760,8 +760,9 @@ public class BeanMappingMethod extends NormalTypeMappingMethod { return constantMappings; } - public Map> getPropertyMappingsByParameter() { - return mappingsByParameter; + public List propertyMappingsByParameter(Parameter parameter) { + // issues: #909 and #1244. FreeMarker has problem getting values from a map when the search key is size or value + return mappingsByParameter.get( parameter.getName() ); } @Override diff --git a/processor/src/main/java/org/mapstruct/ap/internal/model/common/Parameter.java b/processor/src/main/java/org/mapstruct/ap/internal/model/common/Parameter.java index e10f8730c..98fb5dab5 100644 --- a/processor/src/main/java/org/mapstruct/ap/internal/model/common/Parameter.java +++ b/processor/src/main/java/org/mapstruct/ap/internal/model/common/Parameter.java @@ -44,8 +44,7 @@ public class Parameter extends ModelElement { private final boolean mappingContext; private Parameter(String name, Type type, boolean mappingTarget, boolean targetType, boolean mappingContext) { - // issue #909: FreeMarker doesn't like "values" as a parameter name - this.name = "values".equals( name ) ? "values_" : name; + this.name = name; this.originalName = name; this.type = type; this.mappingTarget = mappingTarget; diff --git a/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl b/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl index 1db3e2586..d19ee8a76 100644 --- a/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl +++ b/processor/src/main/resources/org/mapstruct/ap/internal/model/BeanMappingMethod.ftl @@ -45,24 +45,24 @@ <#if (sourceParameters?size > 1)> <#list sourceParametersExcludingPrimitives as sourceParam> - <#if (propertyMappingsByParameter[sourceParam.name]?size > 0)> + <#if (propertyMappingsByParameter(sourceParam)?size > 0)> if ( ${sourceParam.name} != null ) { - <#list propertyMappingsByParameter[sourceParam.name] as propertyMapping> + <#list propertyMappingsByParameter(sourceParam) as propertyMapping> <@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/> } <#list sourcePrimitiveParameters as sourceParam> - <#if (propertyMappingsByParameter[sourceParam.name]?size > 0)> - <#list propertyMappingsByParameter[sourceParam.name] as propertyMapping> + <#if (propertyMappingsByParameter(sourceParam)?size > 0)> + <#list propertyMappingsByParameter(sourceParam) as propertyMapping> <@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/> <#else> <#if mapNullToDefault>if ( ${sourceParameters[0].name} != null ) { - <#list propertyMappingsByParameter[sourceParameters[0].name] as propertyMapping> + <#list propertyMappingsByParameter(sourceParameters[0]) as propertyMapping> <@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/> <#if mapNullToDefault>} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/Issue1244Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/Issue1244Test.java new file mode 100644 index 000000000..42b8076f1 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/Issue1244Test.java @@ -0,0 +1,46 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * 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.ap.test.bugs._1244; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; +import org.mapstruct.factory.Mappers; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Filip Hrisafov + */ +@IssueKey("1244") +@RunWith(AnnotationProcessorTestRunner.class) +@WithClasses( SizeMapper.class ) +public class Issue1244Test { + + @Test + public void properlyCreatesMapperWithSizeAsParameterName() { + SizeMapper.SizeHolder sizeHolder = new SizeMapper.SizeHolder(); + sizeHolder.setSize( "size" ); + + SizeMapper.SizeHolderDto dto = Mappers.getMapper( SizeMapper.class ).convert( sizeHolder ); + assertThat( dto.getSize() ).isEqualTo( "size" ); + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/SizeMapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/SizeMapper.java new file mode 100644 index 000000000..8de1c6874 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_1244/SizeMapper.java @@ -0,0 +1,58 @@ +/** + * Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/) + * 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.ap.test.bugs._1244; + +import org.mapstruct.Mapper; + +/** + * @author Filip Hrisafov + */ +@Mapper +public interface SizeMapper { + + SizeHolderDto convert(SizeHolder size); + + SizeHolderDto convert(SizeHolder size, int test); + + SizeHolderDto convertOther(SizeHolder sizeHolder, int size); + + class SizeHolder { + private String size; + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + } + + class SizeHolderDto { + private String size; + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_909/ValuesMapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_909/ValuesMapper.java index 5198104be..2061e8d51 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/bugs/_909/ValuesMapper.java +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_909/ValuesMapper.java @@ -27,6 +27,10 @@ import org.mapstruct.Mapper; public interface ValuesMapper { ValuesHolderDto convert(ValuesHolder values); + ValuesHolderDto convert(ValuesHolder values, int test); + + ValuesHolderDto convertOther(ValuesHolder valuesHolder, int values); + class ValuesHolder { private String values;