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 #2750.
This commit is contained in:
John Blum
2022-12-12 12:31:08 -08:00
parent f8d6c9f026
commit bdfb1f8a7f
2 changed files with 46 additions and 4 deletions

View File

@@ -33,12 +33,14 @@ import org.springframework.util.StringUtils;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author John Blum
*/
public class PropertyReferenceException extends RuntimeException {
private static final long serialVersionUID = -5254424051438976570L;
private static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'";
private static final String HINTS_TEMPLATE = " Did you mean '%s'";
static final String ERROR_TEMPLATE = "No property '%s' found for type '%s'";
static final String HINTS_TEMPLATE = "Did you mean %s";
private final String propertyName;
private final TypeInformation<?> type;
@@ -101,13 +103,13 @@ public class PropertyReferenceException extends RuntimeException {
Collection<String> potentialMatches = getPropertyMatches();
if (!potentialMatches.isEmpty()) {
String matches = StringUtils.collectionToDelimitedString(potentialMatches, ",", "'", "'");
builder.append("; ");
builder.append(String.format(HINTS_TEMPLATE, matches));
}
if (!alreadyResolvedPath.isEmpty()) {
builder.append(" Traversed path: ");
builder.append("; Traversed path: ");
builder.append(alreadyResolvedPath.get(0).toString());
builder.append(".");
}
return builder.toString();

View File

@@ -28,6 +28,7 @@ import org.springframework.data.util.TypeInformation;
*
* @author Oliver Gierke
* @author Mark Paluch
* @author John Blum
*/
public class PropertyReferenceExceptionUnitTests {
@@ -65,6 +66,33 @@ public class PropertyReferenceExceptionUnitTests {
assertThat(matches).containsExactly("name");
}
@Test // GH-2750
public void formatsMessageWithTypeInfoAndHintsCorrectly() {
var exception = new PropertyReferenceException("nme", TYPE_INFO, NO_PATHS);
String expectedMessage = String.format("%s; %s", PropertyReferenceException.ERROR_TEMPLATE,
PropertyReferenceException.HINTS_TEMPLATE);
assertThat(exception)
.hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'");
}
@Test // GH-2750
public void formatsMessageWithTypeInfoHintsAndPathCorrectly() {
var ctype = TypeInformation.of(C.class);
var exception = new PropertyReferenceException("nme", TYPE_INFO,
Collections.singletonList(PropertyPath.from("b.a", ctype)));
String expectedMessage = String.format("%s; %s; %s", PropertyReferenceException.ERROR_TEMPLATE,
PropertyReferenceException.HINTS_TEMPLATE, "Traversed path: C.b.a");
assertThat(exception)
.hasMessage(expectedMessage,"nme", TYPE_INFO.getType().getSimpleName(), "'name'");
}
static class Sample {
String name;
@@ -77,4 +105,16 @@ public class PropertyReferenceExceptionUnitTests {
this.name = name;
}
}
static class A {
}
static class B {
A a;
}
static class C {
B b;
}
}