Skip to content

Commit bcc0968

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 gh-2603.
1 parent f8d6c9f commit bcc0968

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
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;
@@ -101,13 +103,13 @@ public String getMessage() {
101103
Collection<String> potentialMatches = getPropertyMatches();
102104
if (!potentialMatches.isEmpty()) {
103105
String matches = StringUtils.collectionToDelimitedString(potentialMatches, ",", "'", "'");
106+
builder.append("; ");
104107
builder.append(String.format(HINTS_TEMPLATE, matches));
105108
}
106109

107110
if (!alreadyResolvedPath.isEmpty()) {
108-
builder.append(" Traversed path: ");
111+
builder.append("; Traversed path: ");
109112
builder.append(alreadyResolvedPath.get(0).toString());
110-
builder.append(".");
111113
}
112114

113115
return builder.toString();

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

+40
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Oliver Gierke
3030
* @author Mark Paluch
31+
* @author John Blum
3132
*/
3233
public class PropertyReferenceExceptionUnitTests {
3334

@@ -65,6 +66,33 @@ public void exposesPotentialMatch() {
6566
assertThat(matches).containsExactly("name");
6667
}
6768

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

7098
String name;
@@ -77,4 +105,16 @@ public void setName(String name) {
77105
this.name = name;
78106
}
79107
}
108+
109+
static class A {
110+
111+
}
112+
113+
static class B {
114+
A a;
115+
}
116+
117+
static class C {
118+
B b;
119+
}
80120
}

0 commit comments

Comments
 (0)