From bcc09685b11aed2a55ae8a97dc3447d25a5e6f65 Mon Sep 17 00:00:00 2001 From: John Blum Date: Mon, 12 Dec 2022 12:31:08 -0800 Subject: [PATCH] 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. --- .../mapping/PropertyReferenceException.java | 10 +++-- .../PropertyReferenceExceptionUnitTests.java | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java b/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java index c84570af7a..b623b1db87 100644 --- a/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java +++ b/src/main/java/org/springframework/data/mapping/PropertyReferenceException.java @@ -33,12 +33,14 @@ * * @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 String getMessage() { Collection 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(); diff --git a/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java b/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java index 49f722c6ef..c2b9e0667e 100755 --- a/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/PropertyReferenceExceptionUnitTests.java @@ -28,6 +28,7 @@ * * @author Oliver Gierke * @author Mark Paluch + * @author John Blum */ public class PropertyReferenceExceptionUnitTests { @@ -65,6 +66,33 @@ public void exposesPotentialMatch() { 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 void setName(String name) { this.name = name; } } + + static class A { + + } + + static class B { + A a; + } + + static class C { + B b; + } }