Skip to content

Commit 501f0c8

Browse files
committed
DATACMNS-700 - ReflectionRepositoryInvoker now captures context for failed parameter conversions.
Introduced QueryMethodParameterConversionException to capture the context of a failed ConversionException to allow better error reporting. Cleaned up test cases to not use deprecated API anymore.
1 parent 1fb7466 commit 501f0c8

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository.support;
17+
18+
import org.springframework.core.MethodParameter;
19+
import org.springframework.core.convert.ConversionException;
20+
import org.springframework.util.Assert;
21+
22+
/**
23+
* Exception to represent a failed attempt to convert a source value into a query method parameter.
24+
*
25+
* @author Oliver Gierke
26+
* @soundtrack Dave Matthews Band - The Dreaming Tree (DMB 2009 Europe)
27+
* @since 1.11
28+
*/
29+
public class QueryMethodParameterConversionException extends RuntimeException {
30+
31+
private static final long serialVersionUID = -5818002272039533066L;
32+
33+
private final Object source;
34+
private final MethodParameter parameter;
35+
36+
/**
37+
* @param source can be {@literal null}.
38+
* @param parameter the {@link MethodParameter} the value should've been converted for, must not be {@literal null}..
39+
* @param cause the original {@link ConversionException}, must not be {@literal null}.
40+
*/
41+
public QueryMethodParameterConversionException(Object source, MethodParameter parameter, ConversionException cause) {
42+
43+
super("message", cause);
44+
45+
Assert.notNull(parameter, "Method parameter must not be null!");
46+
Assert.notNull(cause, "ConversionException must not be null!");
47+
48+
this.parameter = parameter;
49+
this.source = source;
50+
}
51+
52+
/**
53+
* Returns the source value that we failed converting.
54+
*
55+
* @return the source can be {@literal null}.
56+
*/
57+
public Object getSource() {
58+
return source;
59+
}
60+
61+
/**
62+
* Returns the {@link MethodParameter} we tried to convert the source value for.
63+
*
64+
* @return the parameter will never be {@literal null}.
65+
* @see #getSource()
66+
*/
67+
public MethodParameter getParameter() {
68+
return parameter;
69+
}
70+
}

src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map.Entry;
2424

2525
import org.springframework.core.MethodParameter;
26+
import org.springframework.core.convert.ConversionException;
2627
import org.springframework.core.convert.ConversionService;
2728
import org.springframework.core.convert.TypeDescriptor;
2829
import org.springframework.data.domain.Pageable;
@@ -234,14 +235,22 @@ private Object[] prepareParameters(Method method, MultiValueMap<String, ? extend
234235

235236
Object value = unwrapSingleElement(rawParameters.get(parameterName));
236237

237-
result[i] = targetType.isInstance(value) ? value : conversionService.convert(value,
238-
TypeDescriptor.forObject(value), new TypeDescriptor(param));
238+
result[i] = targetType.isInstance(value) ? value : convert(value, param);
239239
}
240240
}
241241

242242
return result;
243243
}
244244

245+
private Object convert(Object value, MethodParameter parameter) {
246+
247+
try {
248+
return conversionService.convert(value, TypeDescriptor.forObject(value), new TypeDescriptor(parameter));
249+
} catch (ConversionException o_O) {
250+
throw new QueryMethodParameterConversionException(value, parameter, o_O);
251+
}
252+
}
253+
245254
/**
246255
* Invokes the given method with the given arguments on the backing repository.
247256
*

src/test/java/org/springframework/data/repository/support/ReflectionRepositoryInvokerUnitTests.java

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
import java.util.Collection;
2626
import java.util.Collections;
2727
import java.util.Date;
28-
import java.util.HashMap;
2928
import java.util.List;
30-
import java.util.Map;
3129

3230
import org.junit.Before;
3331
import org.junit.Test;
3432
import org.junit.runner.RunWith;
3533
import org.mockito.runners.MockitoJUnitRunner;
34+
import org.springframework.core.convert.ConversionFailedException;
3635
import org.springframework.core.convert.ConversionService;
3736
import org.springframework.core.convert.support.GenericConversionService;
3837
import org.springframework.data.domain.Page;
@@ -44,10 +43,13 @@
4443
import org.springframework.data.repository.Repository;
4544
import org.springframework.data.repository.core.RepositoryMetadata;
4645
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
46+
import org.springframework.data.repository.query.Param;
4747
import org.springframework.data.repository.support.CrudRepositoryInvokerUnitTests.Person;
4848
import org.springframework.data.repository.support.CrudRepositoryInvokerUnitTests.PersonRepository;
4949
import org.springframework.data.repository.support.RepositoryInvocationTestUtils.VerifyingMethodInterceptor;
5050
import org.springframework.format.support.DefaultFormattingConversionService;
51+
import org.springframework.util.LinkedMultiValueMap;
52+
import org.springframework.util.MultiValueMap;
5153

5254
/**
5355
* Integration tests for {@link ReflectionRepositoryInvoker}.
@@ -155,8 +157,8 @@ public void invokesFindAllWithPageableCorrectly() throws Exception {
155157
@Test
156158
public void invokesQueryMethod() throws Exception {
157159

158-
HashMap<String, String[]> parameters = new HashMap<String, String[]>();
159-
parameters.put("firstName", new String[] { "John" });
160+
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
161+
parameters.add("firstName", "John");
160162

161163
Method method = PersonRepository.class.getMethod("findByFirstName", String.class, Pageable.class);
162164
PersonRepository repository = mock(PersonRepository.class);
@@ -170,8 +172,8 @@ public void invokesQueryMethod() throws Exception {
170172
@Test
171173
public void considersFormattingAnnotationsOnQueryMethodParameters() throws Exception {
172174

173-
Map<String, String[]> parameters = Collections.singletonMap("date",
174-
new String[] { "2013-07-18T10:49:00.000+02:00" });
175+
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
176+
parameters.add("date", "2013-07-18T10:49:00.000+02:00");
175177

176178
Method method = PersonRepository.class.getMethod("findByCreatedUsingISO8601Date", Date.class, Pageable.class);
177179
PersonRepository repository = mock(PersonRepository.class);
@@ -247,7 +249,8 @@ public void translatesCollectionRequestParametersCorrectly() throws Exception {
247249

248250
for (String[] ids : Arrays.asList(new String[] { "1,2" }, new String[] { "1", "2" })) {
249251

250-
Map<String, String[]> parameters = Collections.singletonMap("ids", ids);
252+
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
253+
parameters.put("ids", Arrays.asList(ids));
251254

252255
Method method = PersonRepository.class.getMethod("findByIdIn", Collection.class);
253256
PersonRepository repository = mock(PersonRepository.class);
@@ -256,6 +259,29 @@ public void translatesCollectionRequestParametersCorrectly() throws Exception {
256259
}
257260
}
258261

262+
/**
263+
* @see DATACMNS-700
264+
*/
265+
@Test
266+
public void failedParameterConversionCapturesContext() throws Exception {
267+
268+
RepositoryInvoker invoker = getInvokerFor(mock(SimpleRepository.class));
269+
270+
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<String, Object>();
271+
parameters.add("value", "value");
272+
273+
Method method = SimpleRepository.class.getMethod("findByClass", int.class);
274+
275+
try {
276+
invoker.invokeQueryMethod(method, parameters, null, null);
277+
} catch (QueryMethodParameterConversionException o_O) {
278+
279+
assertThat(o_O.getParameter(), is(new MethodParameters(method).getParameters().get(0)));
280+
assertThat(o_O.getSource(), is((Object) "value"));
281+
assertThat(o_O.getCause(), is(instanceOf(ConversionFailedException.class)));
282+
}
283+
}
284+
259285
private static RepositoryInvoker getInvokerFor(Object repository) {
260286

261287
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repository.getClass().getInterfaces()[0]);
@@ -310,4 +336,9 @@ interface RepoWithDomainDeleteAndFindOne extends Repository<Domain, Long> {
310336

311337
void delete(Domain entity);
312338
}
339+
340+
interface SimpleRepository extends Repository<Domain, Long> {
341+
342+
Domain findByClass(@Param("value") int value);
343+
}
313344
}

0 commit comments

Comments
 (0)