Skip to content

Commit

Permalink
Merge pull request #315 from yupengj/master
Browse files Browse the repository at this point in the history
Issue314TestCase and override exclude method
  • Loading branch information
elaatifi authored Mar 16, 2019
2 parents 86ce371 + ffeb700 commit 19ed073
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,23 @@ public ClassMapBuilderForMaps<A, B> byDefault(MappingDirection direction, Defaul
return self();
}

/**
* Exclude the specified field from bean mapping
*
* @param fieldName
* the name of the field/property to exclude
* @return this ClassMapBuilder
*/
@Override
public ClassMapBuilder<A, B> exclude(String fieldName) {
Type<?> type = isATypeBean() ? getAType() : getBType();
if(getPropertyResolver().existsProperty(type, fieldName)) {
return fieldMap(fieldName).exclude().add();
}else {
return this;
}
}

/**
* Gets the parent expression from this nested expression
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ma.glasnost.orika.test.community;

import static org.junit.Assert.assertNull;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import ma.glasnost.orika.impl.DefaultMapperFactory;

/**
* ClassMapBuilderForMaps class exclude method Does not work
* <p>
*
* @see <a href=
* "https://github.com/orika-mapper/orika/issues/314">https://github.com/orika-mapper/orika/issues</a>
*/
public class Issue314TestCase {

@Test
public void exclude_classMapBuilderForMaps() {

DefaultMapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
mapperFactory.classMap(Map.class, B.class).exclude("b").byDefault().register();

Map<String, Object> map = new HashMap<String, Object>();
map.put("a", "a");
map.put("b", "b");

B b = mapperFactory.getMapperFacade().map(map, B.class);
assertNull(b.getB());
}

public static class B {
private String a;
private String b;

public String getA() {
return a;
}

public void setA(String a) {
this.a = a;
}

public String getB() {
return b;
}

public void setB(String b) {
this.b = b;
}
}

}

0 comments on commit 19ed073

Please sign in to comment.