Skip to content

Accept JsonProvider upon JsonProjectingMethodInterceptorFactory creation #2403

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

Closed
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 @@ -38,7 +38,6 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
Expand All @@ -47,34 +46,49 @@
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;

/**
* {@link MethodInterceptorFactory} to create a {@link MethodInterceptor} that will
*
* @author Oliver Gierke
* @author Mark Paluch
* @author Mikhael Sokolov
* @soundtrack Jeff Coffin - Fruitcake (The Inside Of The Outside)
* @since 1.13
*/
public class JsonProjectingMethodInterceptorFactory implements MethodInterceptorFactory {

private final ParseContext context;

/**
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link MappingProvider} and {@link JsonProvider}.
*
* @param mappingProvider must not be {@literal null}.
* @param jsonProvider must not be {@literal null}.
*/
public JsonProjectingMethodInterceptorFactory(MappingProvider mappingProvider, JsonProvider jsonProvider) {

Assert.notNull(mappingProvider, "MappingProvider must not be null!");
Assert.notNull(jsonProvider, "JsonProvider must not be null!");

Configuration configuration = Configuration.builder()//
.options(Option.ALWAYS_RETURN_LIST)//
.mappingProvider(mappingProvider)//
.jsonProvider(jsonProvider)//
.build();

this.context = JsonPath.using(configuration);
}

/**
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link ObjectMapper}.
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link MappingProvider}.
*
* @param mapper must not be {@literal null}.
* @param mappingProvider must not be {@literal null}.
*/
@Deprecated
public JsonProjectingMethodInterceptorFactory(MappingProvider mappingProvider) {

Assert.notNull(mappingProvider, "MappingProvider must not be null!");

Configuration build = Configuration.builder()//
.options(Option.ALWAYS_RETURN_LIST)//
.mappingProvider(mappingProvider)//
.build();

this.context = JsonPath.using(build);
this(mappingProvider, Configuration.defaultConfiguration().jsonProvider());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.Type;
import java.util.Map;

import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
Expand Down Expand Up @@ -85,7 +86,7 @@ private static SpelAwareProxyProjectionFactory initProjectionFactory(ObjectMappe

SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
projectionFactory
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper)));
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper), new JacksonJsonProvider(mapper)));

return projectionFactory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import static org.assertj.core.api.Assertions.*;

import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -59,7 +61,8 @@ void setUp() {
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();

MappingProvider mappingProvider = new JacksonMappingProvider(new ObjectMapper());
projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(mappingProvider));
JsonProvider jsonProvider = new JacksonJsonProvider(new ObjectMapper());
projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(mappingProvider, jsonProvider));

this.projectionFactory = projectionFactory;
this.customer = projectionFactory.createProjection(Customer.class, new ByteArrayInputStream(json.getBytes()));
Expand Down