Skip to content

Commit 69865d6

Browse files
committed
Fix formatting in PropertyReferenceException message.
A message like, "No property 'creat' found for type 'User' Did you mean ''created''" is now properly formatted as: "No property 'creat' found for type 'User'; Did you mean 'created'". Closes spring-projectsgh-2603.
1 parent 7502f81 commit 69865d6

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@
3333
*
3434
* @author Oliver Gierke
3535
* @author Christoph Strobl
36+
* @author John Blum
3637
*/
3738
public class PropertyReferenceException extends RuntimeException {
3839

3940
private static final long serialVersionUID = -5254424051438976570L;
40-
private static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'";
41-
private static final String HINTS_TEMPLATE = " Did you mean '%s'";
41+
42+
static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'";
43+
static final String HINTS_TEMPLATE = "Did you mean %s";
4244

4345
private final String propertyName;
4446
private final TypeInformation<?> type;
@@ -105,13 +107,13 @@ public String getMessage() {
105107
Collection<String> potentialMatches = getPropertyMatches();
106108
if (!potentialMatches.isEmpty()) {
107109
String matches = StringUtils.collectionToDelimitedString(potentialMatches, ",", "'", "'");
110+
builder.append("; ");
108111
builder.append(String.format(HINTS_TEMPLATE, matches));
109112
}
110113

111114
if (!alreadyResolvedPath.isEmpty()) {
112-
builder.append(" Traversed path: ");
115+
builder.append("; Traversed path: ");
113116
builder.append(alreadyResolvedPath.get(0).toString());
114-
builder.append(".");
115117
}
116118

117119
return builder.toString();

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

+42-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.springframework.data.mapping;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
1920

2021
import java.util.Collection;
2122
import java.util.Collections;
@@ -31,6 +32,7 @@
3132
*
3233
* @author Oliver Gierke
3334
* @author Mark Paluch
35+
* @author John Blum
3436
*/
3537
public class PropertyReferenceExceptionUnitTests {
3638

@@ -68,6 +70,33 @@ public void exposesPotentialMatch() {
6870
assertThat(matches).containsExactly("name");
6971
}
7072

73+
@Test // GH-2750
74+
public void formatsMessageWithTypeInfoAndHintsCorrectly() {
75+
76+
PropertyReferenceException exception = new PropertyReferenceException("nme", TYPE_INFO, NO_PATHS);
77+
78+
String expectedMessage = String.format("%s; %s", PropertyReferenceException.ERROR_TEMPLATE,
79+
PropertyReferenceException.HINTS_TEMPLATE);
80+
81+
assertThat(exception)
82+
.hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'");
83+
}
84+
85+
@Test // GH-2750
86+
public void formatsMessageWithTypeInfoHintsAndPathCorrectly() {
87+
88+
TypeInformation<C> ctype = ClassTypeInformation.from(C.class);
89+
90+
PropertyReferenceException exception = new PropertyReferenceException("nme", TYPE_INFO,
91+
Collections.singletonList(PropertyPath.from("b.a", ctype)));
92+
93+
String expectedMessage = String.format("%s; %s; %s", PropertyReferenceException.ERROR_TEMPLATE,
94+
PropertyReferenceException.HINTS_TEMPLATE, "Traversed path: C.b.a");
95+
96+
assertThat(exception)
97+
.hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'");
98+
}
99+
71100
static class Sample {
72101

73102
String name;
@@ -80,4 +109,16 @@ public void setName(String name) {
80109
this.name = name;
81110
}
82111
}
112+
113+
static class A {
114+
115+
}
116+
117+
static class B {
118+
A a;
119+
}
120+
121+
static class C {
122+
B b;
123+
}
83124
}

0 commit comments

Comments
 (0)