Skip to content

Commit f919013

Browse files
sokomishalovmp911de
authored andcommitted
Accept JsonProvider upon JsonProjectingMethodInterceptorFactory creation.
We now configure explicitly the JsonProvider that is used by JSONPath through JsonProjectingMethodInterceptorFactory to avoid misconfiguration due to JSONPath defaulting attempts. Accepting JsonProvider allows for improved flexibility during configuration. Closes #2403
1 parent 9ca1c7e commit f919013

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java

+26-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.springframework.lang.Nullable;
3939
import org.springframework.util.Assert;
4040

41-
import com.fasterxml.jackson.databind.ObjectMapper;
4241
import com.jayway.jsonpath.Configuration;
4342
import com.jayway.jsonpath.DocumentContext;
4443
import com.jayway.jsonpath.JsonPath;
@@ -47,34 +46,49 @@
4746
import com.jayway.jsonpath.PathNotFoundException;
4847
import com.jayway.jsonpath.TypeRef;
4948
import com.jayway.jsonpath.spi.mapper.MappingProvider;
49+
import com.jayway.jsonpath.spi.json.JsonProvider;
5050

5151
/**
5252
* {@link MethodInterceptorFactory} to create a {@link MethodInterceptor} that will
5353
*
5454
* @author Oliver Gierke
5555
* @author Mark Paluch
56+
* @author Mikhael Sokolov
5657
* @soundtrack Jeff Coffin - Fruitcake (The Inside Of The Outside)
5758
* @since 1.13
5859
*/
5960
public class JsonProjectingMethodInterceptorFactory implements MethodInterceptorFactory {
6061

6162
private final ParseContext context;
6263

64+
/**
65+
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link MappingProvider} and {@link JsonProvider}.
66+
*
67+
* @param mappingProvider must not be {@literal null}.
68+
* @param jsonProvider must not be {@literal null}.
69+
*/
70+
public JsonProjectingMethodInterceptorFactory(MappingProvider mappingProvider, JsonProvider jsonProvider) {
71+
72+
Assert.notNull(mappingProvider, "MappingProvider must not be null!");
73+
Assert.notNull(jsonProvider, "JsonProvider must not be null!");
74+
75+
Configuration configuration = Configuration.builder()//
76+
.options(Option.ALWAYS_RETURN_LIST)//
77+
.mappingProvider(mappingProvider)//
78+
.jsonProvider(jsonProvider)//
79+
.build();
80+
81+
this.context = JsonPath.using(configuration);
82+
}
83+
6384
/**
64-
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link ObjectMapper}.
85+
* Creates a new {@link JsonProjectingMethodInterceptorFactory} using the given {@link MappingProvider}.
6586
*
66-
* @param mapper must not be {@literal null}.
87+
* @param mappingProvider must not be {@literal null}.
6788
*/
89+
@Deprecated
6890
public JsonProjectingMethodInterceptorFactory(MappingProvider mappingProvider) {
69-
70-
Assert.notNull(mappingProvider, "MappingProvider must not be null!");
71-
72-
Configuration build = Configuration.builder()//
73-
.options(Option.ALWAYS_RETURN_LIST)//
74-
.mappingProvider(mappingProvider)//
75-
.build();
76-
77-
this.context = JsonPath.using(build);
91+
this(mappingProvider, Configuration.defaultConfiguration().jsonProvider());
7892
}
7993

8094
/*

src/main/java/org/springframework/data/web/ProjectingJackson2HttpMessageConverter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Type;
2020
import java.util.Map;
2121

22+
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
2223
import org.springframework.beans.BeansException;
2324
import org.springframework.beans.factory.BeanClassLoaderAware;
2425
import org.springframework.beans.factory.BeanFactory;
@@ -85,7 +86,7 @@ private static SpelAwareProxyProjectionFactory initProjectionFactory(ObjectMappe
8586

8687
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
8788
projectionFactory
88-
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper)));
89+
.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(new JacksonMappingProvider(mapper), new JacksonJsonProvider(mapper)));
8990

9091
return projectionFactory;
9192
}

src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

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

20+
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
21+
import com.jayway.jsonpath.spi.json.JsonProvider;
2022
import lombok.AllArgsConstructor;
2123
import lombok.Data;
2224
import lombok.NoArgsConstructor;
@@ -59,7 +61,8 @@ void setUp() {
5961
SpelAwareProxyProjectionFactory projectionFactory = new SpelAwareProxyProjectionFactory();
6062

6163
MappingProvider mappingProvider = new JacksonMappingProvider(new ObjectMapper());
62-
projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(mappingProvider));
64+
JsonProvider jsonProvider = new JacksonJsonProvider(new ObjectMapper());
65+
projectionFactory.registerMethodInvokerFactory(new JsonProjectingMethodInterceptorFactory(mappingProvider, jsonProvider));
6366

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

0 commit comments

Comments
 (0)