Skip to content

Commit

Permalink
add default filter provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
ksprider committed Jul 28, 2021
1 parent b4a4d9b commit 350be53
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>Surgical</artifactId>
<packaging>jar</packaging>
<name>Surgical</name>
<version>0.0.2</version>
<version>0.0.3</version>
<description>A tool for dynamically filtering entity properties based on Jackson</description>
<url>https://github.com/ksprider/Surgical</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class CustomFilterProvider<T> extends FilterProvider {

private final CustomPropertyFilter customPropertyFilter;

public CustomFilterProvider() {
this.customPropertyFilter = new CustomPropertyFilter<>(null, null, null, true);
}

public CustomFilterProvider(T root, SerializationHandler<T> serializerHandler, String location, boolean isAll) {
this.customPropertyFilter = new CustomPropertyFilter<>(root, serializerHandler, location, isAll);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public void serializeAsField(Object pojo, JsonGenerator gen, SerializerProvider
writer.serializeAsField(pojo, gen, prov);
return;
}

String key = getKey(gen.getOutputContext(), writer.getName());
boolean serialize = isAll || cache.computeIfAbsent(key, it -> {
boolean serialize = isAll || cache.computeIfAbsent(getKey(gen.getOutputContext(), writer.getName()), it -> {
T ot = (T) BeanUtils.instantiateClass(t.getClass());
BeanUtils.copyProperties(t, ot);
return serializerHandler.isSerialize(gen.getOutputContext(), writer.getName(), ot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public boolean supports(MethodParameter methodParameter, Class<? extends HttpMes
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
JSON jsonAnnotation = methodParameter.getMethod().getAnnotation(JSON.class);
String key = methodParameter.getExecutable().toString();
String key = methodParameter.getMethod().toString();
CustomFilterProvider customFilterProvider = cache.computeIfAbsent(key, (it) -> {
boolean isAll = null == jsonAnnotation;
String treeStr = isAll ? "*" : jsonAnnotation.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.ksprider.surgical.CustomFilterProvider;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;
Expand All @@ -18,9 +20,15 @@ public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@Bean
public CustomFilterProvider<?> defaultFilterProvider() {
return new CustomFilterProvider<>();
}

@Override
public void afterPropertiesSet() throws Exception {
objectMapper.addMixIn(Serializable.class, CustomPropertyFilterMixIn.class);
objectMapper.setFilterProvider(defaultFilterProvider());
}

@JsonFilter("customPropertyFilter")
Expand Down

0 comments on commit 350be53

Please sign in to comment.