Skip to content

Commit d859540

Browse files
committed
DATACMNS-703 - PropertyReferenceException now exposes potential matches.
We now use Spring's PropertyMatches to build up the exception message and include a list of potentially meant properties. Removed unused config file for tests along the way. Original pull request: #125.
1 parent 2cb2830 commit d859540

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

src/main/java/org/springframework/data/mapping/PropertyReferenceException.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,14 @@
1515
*/
1616
package org.springframework.data.mapping;
1717

18+
import java.util.ArrayList;
19+
import java.util.Arrays;
1820
import java.util.List;
1921

22+
import org.springframework.beans.PropertyMatches;
2023
import org.springframework.data.util.TypeInformation;
2124
import org.springframework.util.Assert;
25+
import org.springframework.util.StringUtils;
2226

2327
/**
2428
* Exception being thrown when creating {@link PropertyPath} instances.
@@ -29,10 +33,12 @@ public class PropertyReferenceException extends RuntimeException {
2933

3034
private static final long serialVersionUID = -5254424051438976570L;
3135
private static final String ERROR_TEMPLATE = "No property %s found for type %s!";
36+
private static final String HINTS_TEMPLATE = " Did you mean %s?";
3237

3338
private final String propertyName;
3439
private final TypeInformation<?> type;
3540
private final List<PropertyPath> alreadyResolvedPath;
41+
private final List<String> propertyMatches;
3642

3743
/**
3844
* Creates a new {@link PropertyReferenceException}.
@@ -41,14 +47,16 @@ public class PropertyReferenceException extends RuntimeException {
4147
* @param type the type the property could not be found on.
4248
* @param alreadyResolvedPah the previously calculated {@link PropertyPath}s.
4349
*/
44-
public PropertyReferenceException(String propertyName, TypeInformation<?> type, List<PropertyPath> alreadyResolvedPah) {
50+
public PropertyReferenceException(String propertyName, TypeInformation<?> type,
51+
List<PropertyPath> alreadyResolvedPah) {
4552

4653
Assert.hasText(propertyName);
4754
Assert.notNull(type);
4855

4956
this.propertyName = propertyName;
5057
this.type = type;
5158
this.alreadyResolvedPath = alreadyResolvedPah;
59+
this.propertyMatches = detectPotentialMatches(propertyName, type.getType());
5260
}
5361

5462
/**
@@ -76,11 +84,18 @@ public TypeInformation<?> getType() {
7684
@Override
7785
public String getMessage() {
7886

79-
StringBuilder builder = new StringBuilder(String.format(ERROR_TEMPLATE, propertyName, type.getType()
80-
.getSimpleName()));
87+
StringBuilder builder = new StringBuilder(
88+
String.format(ERROR_TEMPLATE, propertyName, type.getType().getSimpleName()));
89+
90+
if (!propertyMatches.isEmpty()) {
91+
String matches = StringUtils.collectionToDelimitedString(propertyMatches, ",", "'", "'");
92+
builder.append(String.format(HINTS_TEMPLATE, matches));
93+
}
8194

8295
if (!alreadyResolvedPath.isEmpty()) {
83-
builder.append(" Traversed path: ").append(alreadyResolvedPath.get(0).toString()).append(".");
96+
builder.append(" Traversed path: ");
97+
builder.append(alreadyResolvedPath.get(0).toString());
98+
builder.append(".");
8499
}
85100

86101
return builder.toString();
@@ -105,4 +120,20 @@ public PropertyPath getBaseProperty() {
105120
public boolean hasDeeperResolutionDepthThan(PropertyReferenceException exception) {
106121
return this.alreadyResolvedPath.size() > exception.alreadyResolvedPath.size();
107122
}
123+
124+
/**
125+
* Detects all potential matches for the given property name and type.
126+
*
127+
* @param propertyName must not be {@literal null} or empty.
128+
* @param type must not be {@literal null}.
129+
* @return
130+
*/
131+
private static List<String> detectPotentialMatches(String propertyName, Class<?> type) {
132+
133+
List<String> result = new ArrayList<String>();
134+
result.addAll(Arrays.asList(PropertyMatches.forField(propertyName, type).getPossibleMatches()));
135+
result.addAll(Arrays.asList(PropertyMatches.forProperty(propertyName, type).getPossibleMatches()));
136+
137+
return result;
138+
}
108139
}

src/test/java/org/springframework/data/mapping/PropertyPathUnitTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2014 the original author or authors.
2+
* Copyright 2011-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -353,6 +353,9 @@ public void rejectsNullTypeInformation() {
353353
from("foo", (TypeInformation<?>) null);
354354
}
355355

356+
/**
357+
* @see DATACMNS-546
358+
*/
356359
@Test
357360
public void returnsCompletePathIfResolutionFailedCompletely() {
358361

@@ -362,8 +365,11 @@ public void returnsCompletePathIfResolutionFailedCompletely() {
362365
from("somethingDifferent", Foo.class);
363366
}
364367

368+
/**
369+
* @see DATACMNS-546
370+
*/
365371
@Test
366-
public void foobar() {
372+
public void includesResolvedPathInExceptionMessage() {
367373

368374
exception.expect(PropertyReferenceException.class);
369375
exception.expectMessage("fooName");
@@ -373,6 +379,18 @@ public void foobar() {
373379
from("userFooName", Bar.class);
374380
}
375381

382+
/**
383+
* @see DATACMNS-703
384+
*/
385+
@Test
386+
public void includesPropertyHintsOnTypos() {
387+
388+
exception.expect(PropertyReferenceException.class);
389+
exception.expectMessage("userName");
390+
391+
from("userAme", Foo.class);
392+
}
393+
376394
private class Foo {
377395

378396
String userName;

src/test/resources/org/springframework/data/repository/config/populators.xml

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)