Skip to content

Commit

Permalink
#1011 exclude other mappings (name and parameter base) from nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaakd committed Jan 24, 2017
1 parent f1d9e22 commit 1819e6d
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 9 deletions.
Expand Up @@ -141,11 +141,14 @@ public BeanMappingMethod build() {
return null;
}

// map properties without a mapping
applyPropertyNameBasedMapping();
if ( !method.getMappingOptions().isRestrictToDefinedMappings() ) {

// map parameters without a mapping
applyParameterNameBasedMapping();
// map properties without a mapping
applyPropertyNameBasedMapping();

// map parameters without a mapping
applyParameterNameBasedMapping();
}

// report errors on unmapped properties
reportErrorForUnmappedTargetPropertiesIfRequired();
Expand Down
Expand Up @@ -46,14 +46,16 @@ public class MappingOptions {
private BeanMapping beanMapping;
private List<ValueMapping> valueMappings;
private boolean fullyInitialized;
private final boolean restrictToDefinedMappings;

public MappingOptions(Map<String, List<Mapping>> mappings, IterableMapping iterableMapping, MapMapping mapMapping,
BeanMapping beanMapping, List<ValueMapping> valueMappings ) {
BeanMapping beanMapping, List<ValueMapping> valueMappings, boolean restrictToDefinedMappings ) {
this.mappings = mappings;
this.iterableMapping = iterableMapping;
this.mapMapping = mapMapping;
this.beanMapping = beanMapping;
this.valueMappings = valueMappings;
this.restrictToDefinedMappings = restrictToDefinedMappings;
}

/**
Expand All @@ -63,7 +65,7 @@ public MappingOptions(Map<String, List<Mapping>> mappings, IterableMapping itera
*/
public static MappingOptions empty() {
return new MappingOptions( Collections.<String, List<Mapping>>emptyMap(), null, null, null,
Collections.<ValueMapping>emptyList() );
Collections.<ValueMapping>emptyList(), false );
}

/**
Expand All @@ -73,7 +75,7 @@ public static MappingOptions empty() {
* @return MappingOptions with only regular mappings
*/
public static MappingOptions forMappingsOnly( Map<String, List<Mapping>> mappings ) {
return new MappingOptions( mappings, null, null, null, Collections.<ValueMapping>emptyList() );
return new MappingOptions( mappings, null, null, null, Collections.<ValueMapping>emptyList(), true );

}

Expand Down Expand Up @@ -389,4 +391,8 @@ private void filterNestedTargetIgnores( Map<String, List<Mapping>> mappings) {

}

public boolean isRestrictToDefinedMappings() {
return restrictToDefinedMappings;
}

}
Expand Up @@ -185,7 +185,7 @@ public Builder setDefininingType(Type definingType) {
public SourceMethod build() {

MappingOptions mappingOptions =
new MappingOptions( mappings, iterableMapping, mapMapping, beanMapping, valueMappings );
new MappingOptions( mappings, iterableMapping, mapMapping, beanMapping, valueMappings, false );

SourceMethod sourceMethod = new SourceMethod(
declaringMapper,
Expand Down
@@ -0,0 +1,38 @@
/**
* 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.nestedtargetproperties;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishTankDto;
import org.mapstruct.ap.test.nestedtargetproperties.source.FishTank;
import org.mapstruct.factory.Mappers;

/**
*
* @author Sjaak Derksen
*/
@Mapper
public interface FishTankMapper {

FishTankMapper INSTANCE = Mappers.getMapper( FishTankMapper.class );

@Mapping(target = "fish.kind", source = "fish.type")
FishTankDto map( FishTank source );
}
Expand Up @@ -27,6 +27,12 @@
import org.mapstruct.ap.test.nestedsourceproperties.source.Label;
import org.mapstruct.ap.test.nestedsourceproperties.source.Song;
import org.mapstruct.ap.test.nestedsourceproperties.source.Studio;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.FishTankDto;
import org.mapstruct.ap.test.nestedtargetproperties._target.WaterPlantDto;
import org.mapstruct.ap.test.nestedtargetproperties.source.Fish;
import org.mapstruct.ap.test.nestedtargetproperties.source.FishTank;
import org.mapstruct.ap.test.nestedtargetproperties.source.WaterPlant;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
Expand All @@ -35,7 +41,19 @@
*
* @author Sjaak Derksen
*/
@WithClasses({Song.class, Artist.class, Chart.class, Label.class, Studio.class, ChartEntry.class})
@WithClasses({
Song.class,
Artist.class,
Chart.class,
Label.class,
Studio.class,
ChartEntry.class,
FishDto.class,
FishTankDto.class,
WaterPlantDto.class,
Fish.class,
FishTank.class,
WaterPlant.class})
@IssueKey("389")
@RunWith(AnnotationProcessorTestRunner.class)
public class NestedTargetPropertiesTest {
Expand Down Expand Up @@ -159,4 +177,33 @@ public void shouldMapNestedTargetWitUpdate() {
assertThat( result.getSong().getPositions().get( 0 ) ).isEqualTo( 1 );

}

@Test
@WithClasses({FishTankMapper.class})
public void automappingAndTargetNestingDemonstrator() {

FishTank source = new FishTank();
Fish fish = new Fish();
fish.setType( "Carp" );
fish.setName( "Wanda" );
WaterPlant waterplant = new WaterPlant();
waterplant.setKind( "Water Hyacinth" );
source.setFish( fish );
source.setPlant( waterplant );

FishTankDto target = FishTankMapper.INSTANCE.map( source );

// the nested property "fish" stops further mapping based on property name level deeper down. A property mapping
// has been found at top level. TODO: we need to discuss this. Is this desired or do we need to ignore all
// properties at deep nesting level (can be a lot)?? This means that nested target property definitions cannot
// be used to 'fix' issues at deeper nesting level.
assertThat( target.getFish() ).isNotNull();
assertThat( target.getFish().getKind() ).isEqualTo( "Carp" );
assertThat( target.getFish().getName() ).isNull();

// automapping takes care of mapping property "waterPlant".
assertThat( target.getPlant() ).isNotNull();
assertThat( target.getPlant().getKind() ).isEqualTo( "Water Hyacinth" );
}

}
@@ -0,0 +1,47 @@
/**
* 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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class FishDto {

private String kind;

// make sure that mapping on name does not happen based on name mapping
private String name;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
@@ -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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class FishTankDto {

private FishDto fish;
private WaterPlantDto plant;

public FishDto getFish() {
return fish;
}

public void setFish(FishDto fish) {
this.fish = fish;
}

public WaterPlantDto getPlant() {
return plant;
}

public void setPlant(WaterPlantDto plant) {
this.plant = plant;
}

}
@@ -0,0 +1,37 @@
/**
* 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.nestedtargetproperties._target;

/**
*
* @author Sjaak Derksen
*/
public class WaterPlantDto {

private String kind;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

}
@@ -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.nestedtargetproperties.source;

/**
*
* @author Sjaak Derksen
*/
public class Fish {

private String type;
private String name;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

0 comments on commit 1819e6d

Please sign in to comment.