#1155 Field accessors should be considered when resolving nested target properties

This commit is contained in:
Filip Hrisafov 2017-03-25 08:44:21 +01:00 committed by GitHub
parent 01d9997ed6
commit c465dd27c6
4 changed files with 131 additions and 2 deletions

View File

@ -149,8 +149,9 @@ public class TargetReference {
break;
}
if ( (i == entryNames.length - 1) || (Executables.isSetterMethod( targetWriteAccessor ) ) ) {
// only intermediate nested properties when they are a true setter
if ( ( i == entryNames.length - 1 ) || ( Executables.isSetterMethod( targetWriteAccessor )
|| Executables.isFieldAccessor( targetWriteAccessor ) ) ) {
// only intermediate nested properties when they are a true setter or field accessor
// the last may be other readAccessor (setter / getter / adder).
if ( Executables.isGetterMethod( targetWriteAccessor ) ||

View File

@ -0,0 +1,43 @@
/**
* 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._1155;
/**
* @author Filip Hrisafov
*/
class Entity {
static class Client {
//CHECKSTYLE:OFF
public long id;
//CHECKSTYLE:ON
}
static class Dto {
//CHECKSTYLE:OFF
public long clientId;
//CHECKSTYLE:ON
}
//CHECKSTYLE:OFF
public Client client;
//CHECKSTYLE:ON
}

View File

@ -0,0 +1,35 @@
/**
* 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._1155;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue1155Mapper {
Issue1155Mapper INSTANCE = Mappers.getMapper( Issue1155Mapper.class );
@Mapping(source = "clientId", target = "client.id")
Entity toEntity(Entity.Dto dto);
}

View File

@ -0,0 +1,50 @@
/**
* 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._1155;
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 static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@WithClasses({
Entity.class,
Issue1155Mapper.class
})
@RunWith(AnnotationProcessorTestRunner.class)
@IssueKey("1155")
public class Issue1155Test {
@Test
public void shouldCompile() throws Exception {
Entity.Dto dto = new Entity.Dto();
dto.clientId = 10;
Entity entity = Issue1155Mapper.INSTANCE.toEntity( dto );
assertThat( entity.client ).isNotNull();
assertThat( entity.client.id ).isEqualTo( 10 );
}
}