mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#1452 support BeanMapping#ignoreByDefault for builders
This commit is contained in:
parent
cf19a6b637
commit
5c799b73ef
@ -30,6 +30,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mapstruct.ap.internal.model.common.Parameter;
|
||||
import org.mapstruct.ap.internal.model.common.Type;
|
||||
import org.mapstruct.ap.internal.model.common.TypeFactory;
|
||||
import org.mapstruct.ap.internal.prism.CollectionMappingStrategyPrism;
|
||||
import org.mapstruct.ap.internal.util.FormattingMessager;
|
||||
@ -298,7 +299,11 @@ public class MappingOptions {
|
||||
public void applyIgnoreAll(MappingOptions inherited, SourceMethod method, FormattingMessager messager,
|
||||
TypeFactory typeFactory ) {
|
||||
CollectionMappingStrategyPrism cms = method.getMapperConfiguration().getCollectionMappingStrategy();
|
||||
Map<String, Accessor> writeAccessors = method.getResultType().getPropertyWriteAccessors( cms );
|
||||
Type writeType = method.getResultType();
|
||||
if ( !method.isUpdateMethod() ) {
|
||||
writeType = writeType.getEffectiveType();
|
||||
}
|
||||
Map<String, Accessor> writeAccessors = writeType.getPropertyWriteAccessors( cms );
|
||||
List<String> mappedPropertyNames = new ArrayList<String>();
|
||||
for ( String targetMappingName : mappings.keySet() ) {
|
||||
mappedPropertyNames.add( targetMappingName.split( "\\." )[0] );
|
||||
|
@ -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.builder.ignore;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class BaseDto {
|
||||
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 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.builder.ignore;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class BaseEntity {
|
||||
|
||||
private final Long id;
|
||||
|
||||
public BaseEntity(Builder builder) {
|
||||
this.id = builder.id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public static Builder baseBuilder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private Long id;
|
||||
|
||||
public Builder id(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BaseEntity createBase() {
|
||||
return new BaseEntity( this );
|
||||
}
|
||||
}
|
||||
}
|
@ -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.builder.ignore;
|
||||
|
||||
import org.mapstruct.BeanMapping;
|
||||
import org.mapstruct.InheritConfiguration;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@Mapper(config = BuilderIgnoringMappingConfig.class)
|
||||
public interface BuilderIgnoringMapper {
|
||||
|
||||
BuilderIgnoringMapper INSTANCE = Mappers.getMapper( BuilderIgnoringMapper.class );
|
||||
|
||||
@InheritConfiguration(name = "mapBase")
|
||||
Person mapWithIgnoringBase(PersonDto source);
|
||||
|
||||
@BeanMapping(ignoreByDefault = true)
|
||||
@Mapping(target = "name", source = "name")
|
||||
Person mapOnlyWithExplicit(PersonDto source);
|
||||
|
||||
Person mapAll(PersonDto source);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* 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.builder.ignore;
|
||||
|
||||
import org.mapstruct.BeanMapping;
|
||||
import org.mapstruct.MapperConfig;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
@MapperConfig
|
||||
public interface BuilderIgnoringMappingConfig {
|
||||
|
||||
@BeanMapping(ignoreByDefault = true)
|
||||
BaseEntity mapBase(BaseDto dto);
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
/**
|
||||
* 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.builder.ignore;
|
||||
|
||||
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
|
||||
*/
|
||||
@RunWith(AnnotationProcessorTestRunner.class)
|
||||
@IssueKey("1452")
|
||||
@WithClasses({
|
||||
BaseDto.class,
|
||||
BaseEntity.class,
|
||||
BuilderIgnoringMapper.class,
|
||||
BuilderIgnoringMappingConfig.class,
|
||||
Person.class,
|
||||
PersonDto.class
|
||||
})
|
||||
public class BuilderIgnoringTest {
|
||||
|
||||
@Test
|
||||
public void shouldIgnoreBase() {
|
||||
PersonDto source = new PersonDto();
|
||||
source.setId( 100L );
|
||||
source.setName( "John" );
|
||||
source.setLastName( "Doe" );
|
||||
|
||||
Person target = BuilderIgnoringMapper.INSTANCE.mapWithIgnoringBase( source );
|
||||
|
||||
assertThat( target.getId() ).isNull();
|
||||
assertThat( target.getName() ).isEqualTo( "John" );
|
||||
assertThat( target.getLastName() ).isEqualTo( "Doe" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapOnlyExplicit() {
|
||||
PersonDto source = new PersonDto();
|
||||
source.setId( 100L );
|
||||
source.setName( "John" );
|
||||
source.setLastName( "Doe" );
|
||||
|
||||
Person target = BuilderIgnoringMapper.INSTANCE.mapOnlyWithExplicit( source );
|
||||
|
||||
assertThat( target.getId() ).isNull();
|
||||
assertThat( target.getName() ).isEqualTo( "John" );
|
||||
assertThat( target.getLastName() ).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapAll() {
|
||||
PersonDto source = new PersonDto();
|
||||
source.setId( 100L );
|
||||
source.setName( "John" );
|
||||
source.setLastName( "Doe" );
|
||||
|
||||
Person target = BuilderIgnoringMapper.INSTANCE.mapAll( source );
|
||||
|
||||
assertThat( target.getId() ).isEqualTo( 100L );
|
||||
assertThat( target.getName() ).isEqualTo( "John" );
|
||||
assertThat( target.getLastName() ).isEqualTo( "Doe" );
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/**
|
||||
* 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.builder.ignore;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class Person extends BaseEntity {
|
||||
|
||||
private final String name;
|
||||
private final String lastName;
|
||||
|
||||
public Person(Builder builder) {
|
||||
super( builder );
|
||||
this.name = builder.name;
|
||||
this.lastName = builder.lastName;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
|
||||
}
|
||||
|
||||
public static class Builder extends BaseEntity.Builder {
|
||||
private String name;
|
||||
private String lastName;
|
||||
|
||||
public Builder name(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder lastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Person create() {
|
||||
return new Person( this );
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/**
|
||||
* 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.builder.ignore;
|
||||
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public class PersonDto extends BaseDto {
|
||||
|
||||
private String name;
|
||||
private String lastName;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user