orika-mapper / orika

Simpler, better and faster Java bean mapping framework
http://orika-mapper.github.io/orika-docs/
Apache License 2.0
1.29k stars 268 forks source link

Filters are not applied for Collection 2 Collection Mapping #93

Open temas-hub opened 8 years ago

temas-hub commented 8 years ago

SourceCodeContext.getDestFilter and SourceCodeContext.getSourceFilter return VariableRef with overriden setter() and getter() but ArrayOrCollectionToCollection.generateMappingCode uses MultiOccurrenceVariableRef decorators which dont call these setter/getter

all above causes that Filter.filterSource and Filter.filterDestination are not invoked in the generated code for collection field mapping.

ndr-brt commented 8 years ago

Can you post a test case?

temas-hub commented 8 years ago

The goal is to exclude some items inside the collection during mapping. Collection with name attributes is a field of both Product and ProductBinding classes

`

    public ResponseBuilder() {
            mapperFactory = new PlatformMapperFactoryImpl.Builder().enableWarmUp(false).build();
            mapperFactory.classMap(Product.class, ProductBinding.class)
                       .byDefault()
                      .register();
            mapperFactory.registerFilter(new AttributeFilter());
     }

private class AttributeFilter extends NullFilter<Collection<com.in.AttributeBinding>,                                              Collection<com.out.AttributeBinding>> {

    @Override
    public boolean filtersSource() {
        return true;
    }

    @Override
    public boolean appliesTo(final Property source, final Property destination) {
        return super.appliesTo(source, destination) && destination.getName().equals("attributes");
    }

    @Override
    public <S extends Collection<com.in.AttributeBinding>> S filterSource(final S sourceValue, final Type<S> sourceType, final String sourceName,
                                                                 final Type<?> destType, final String destName, final MappingContext mappingContext) {
        Collection<com.in.AttributeBinding> filteredAttributes = new ArrayList<com.in.AttributeBinding>();
        if (CollectionUtils.isNotEmpty(sourceValue)) {
            for (com.in.AttributeBinding attribute : sourceValue) {
                if (allowedAttributeNames.contains(attribute.getName())) {
                    filteredAttributes.add(attribute);
                }
            }
        }
        return (S)filteredAttributes;
    }

`