#1244 Fix problems with special word for FreeMarker in some cases

This commit is contained in:
Filip Hrisafov 2017-07-10 20:37:09 +02:00 committed by Andreas Gudian
parent acdab55604
commit 3ebd09eec9
6 changed files with 117 additions and 9 deletions

View File

@ -760,8 +760,9 @@ public class BeanMappingMethod extends NormalTypeMappingMethod {
return constantMappings;
}
public Map<String, List<PropertyMapping>> getPropertyMappingsByParameter() {
return mappingsByParameter;
public List<PropertyMapping> 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

View File

@ -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;

View File

@ -45,24 +45,24 @@
</#list>
<#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>
}
</#if>
</#list>
<#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/>
</#list>
</#if>
</#list>
<#else>
<#if mapNullToDefault>if ( ${sourceParameters[0].name} != null ) {</#if>
<#list propertyMappingsByParameter[sourceParameters[0].name] as propertyMapping>
<#list propertyMappingsByParameter(sourceParameters[0]) as propertyMapping>
<@includeModel object=propertyMapping targetBeanName=resultName existingInstanceMapping=existingInstanceMapping defaultValueAssignment=propertyMapping.defaultValueAssignment/>
</#list>
<#if mapNullToDefault>}</#if>

View File

@ -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" );
}
}

View File

@ -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;
}
}
}

View File

@ -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;