Skip to content

Add convenience method SimpleBeanPropertyFilter.filterOutAll() as symmetric counterpart of serializeAll() #3819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public static SimpleBeanPropertyFilter serializeAll() {
return SerializeExceptFilter.INCLUDE_ALL;
}

/**
* Convenience factory method that will return a filter that will
* simply filter out everything.
*
* @since 2.15
*/
public static SimpleBeanPropertyFilter filterOutAll() {
return FilterExceptFilter.EXCLUDE_ALL;
}

/**
* Factory method that was accidentally added in 2.5 with arguments; basically
* works just as an alias of {@link #filterOutAllExcept(Set)} which is not
Expand Down Expand Up @@ -258,11 +268,17 @@ public static class FilterExceptFilter
{
private static final long serialVersionUID = 1L;

static final FilterExceptFilter EXCLUDE_ALL = new FilterExceptFilter();

/**
* Set of property names to serialize.
*/
protected final Set<String> _propertiesToInclude;

FilterExceptFilter() {
_propertiesToInclude = Collections.emptySet();
}

public FilterExceptFilter(Set<String> properties) {
_propertiesToInclude = properties;
}
Expand Down Expand Up @@ -313,4 +329,4 @@ protected boolean include(PropertyWriter writer) {
return !_propertiesToExclude.contains(writer.getName());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.*;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Tests for verifying that bean property filtering using JsonFilter
* works as expected.
Expand Down Expand Up @@ -40,6 +44,20 @@ static class C {
}
}

@JsonFilter("filterB")
@JsonPropertyOrder({ "a", "b", "c"})
static class BeanB {
public String a;
public String b;
public String c;

public BeanB(String a, String b, String c) {
this.a = a;
this.b = b;
this.c = c;
}
}

static class CheckSiblingContextFilter extends SimpleBeanPropertyFilter {
@Override
public void serializeAsField(Object bean, JsonGenerator jgen, SerializerProvider prov, PropertyWriter writer) throws Exception {
Expand Down Expand Up @@ -132,6 +150,13 @@ public void testIncludeAllFilter() throws Exception
assertEquals("{\"a\":\"a\",\"b\":\"b\"}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}

public void testExcludeAllFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter",
SimpleBeanPropertyFilter.filterOutAll());
assertEquals("{}", MAPPER.writer(prov).writeValueAsString(new Bean()));
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is testIncludeAllFilter() test right above this one. So this test case seems to belong here.

public void testSimpleExclusionFilter() throws Exception
{
FilterProvider prov = new SimpleFilterProvider().addFilter("RootFilter",
Expand Down Expand Up @@ -191,4 +216,44 @@ public void testFilterOnProperty() throws Exception
assertEquals("{\"first\":{\"a\":\"a\"},\"second\":{\"b\":\"b\"}}",
MAPPER.writer(prov).writeValueAsString(new FilteredProps()));
}

public void testAllFiltersWithSameOutput() throws Exception
{
// Setup
SimpleBeanPropertyFilter[] allPossibleFilters = new SimpleBeanPropertyFilter[]{
// Parent class : SimpleBeanPropertyFilter
SimpleBeanPropertyFilter.filterOutAllExcept("a", "b"),
SimpleBeanPropertyFilter.filterOutAllExcept(setOf("a", "b")),
SimpleBeanPropertyFilter.serializeAllExcept("c"),
SimpleBeanPropertyFilter.serializeAllExcept(setOf("c")),
// Subclass : SerializeExceptFilter
new SimpleBeanPropertyFilter.SerializeExceptFilter(setOf("c")),
SimpleBeanPropertyFilter.SerializeExceptFilter.serializeAllExcept("c"),
SimpleBeanPropertyFilter.SerializeExceptFilter.serializeAllExcept(setOf("c")),
SimpleBeanPropertyFilter.SerializeExceptFilter.filterOutAllExcept("a", "b"),
SimpleBeanPropertyFilter.SerializeExceptFilter.filterOutAllExcept(setOf("a", "b")),
// Subclass : FilterExceptFilter
new SimpleBeanPropertyFilter.FilterExceptFilter(setOf("a", "b")),
SimpleBeanPropertyFilter.FilterExceptFilter.serializeAllExcept("c"),
SimpleBeanPropertyFilter.FilterExceptFilter.serializeAllExcept(setOf("c")),
SimpleBeanPropertyFilter.FilterExceptFilter.filterOutAllExcept(setOf("a", "b")),
SimpleBeanPropertyFilter.FilterExceptFilter.filterOutAllExcept("a", "b")
};

// Tests
for (SimpleBeanPropertyFilter filter : allPossibleFilters) {
BeanB beanB = new BeanB("aa", "bb", "cc");
SimpleFilterProvider prov = new SimpleFilterProvider().addFilter("filterB", filter);

String jsonStr = MAPPER.writer(prov).writeValueAsString(beanB);

assertEquals(a2q("{'a':'aa','b':'bb'}"), jsonStr);
}
}

private Set<String> setOf(String... properties) {
Set<String> set = new HashSet<>(properties.length);
set.addAll(Arrays.asList(properties));
return set;
}
}