Skip to content

Commit 153fa5a

Browse files
committed
GH-3121 Throw PropertyReferenceException for whitespace-starting PropertyPath
Signed-off-by: mipo256 <[email protected]>
1 parent 26709fd commit 153fa5a

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

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

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class PropertyPath implements Streamable<PropertyPath> {
4747

4848
private static final String PARSE_DEPTH_EXCEEDED = "Trying to parse a path with depth greater than 1000; This has been disabled for security reasons to prevent parsing overflows";
4949

50-
private static final String DELIMITERS = "_\\.";
50+
private static final String DELIMITERS = "_."; // dot not need to be escaped in the character group
5151
private static final Pattern SPLITTER = Pattern.compile("(?:[%s]?([%s]*?[^%s]+))".replaceAll("%s", DELIMITERS));
5252
private static final Pattern SPLITTER_FOR_QUOTED = Pattern.compile("(?:[%s]?([%s]*?[^%s]+))".replaceAll("%s", "\\."));
5353
private static final Pattern NESTED_PROPERTY_PATTERN = Pattern.compile("\\p{Lu}[\\p{Ll}\\p{Nd}]*$");
@@ -471,6 +471,10 @@ private static PropertyPath create(String source, TypeInformation<?> type, Strin
471471
throw e;
472472
}
473473

474+
if (source.isEmpty() || Character.isWhitespace(source.charAt(0))) {
475+
throw e;
476+
}
477+
474478
exception = e;
475479
}
476480

@@ -497,42 +501,5 @@ public String toString() {
497501
return String.format("%s.%s", owningType.getType().getSimpleName(), toDotPath());
498502
}
499503

500-
private static final class Property {
501-
502-
private final TypeInformation<?> type;
503-
private final String path;
504-
505-
private Property(TypeInformation<?> type, String path) {
506-
this.type = type;
507-
this.path = path;
508-
}
509-
510-
@Override
511-
public boolean equals(@Nullable Object obj) {
512-
513-
if (obj == this) {
514-
return true;
515-
}
516-
517-
if (!(obj instanceof Property that)) {
518-
return false;
519-
}
520-
521-
return Objects.equals(this.type, that.type) &&
522-
Objects.equals(this.path, that.path);
523-
}
524-
525-
@Override
526-
public int hashCode() {
527-
return Objects.hash(type, path);
528-
}
529-
530-
@Override
531-
public String toString() {
532-
533-
return "Key[" +
534-
"type=" + type + ", " +
535-
"path=" + path + ']';
536-
}
537-
}
504+
private record Property(TypeInformation<?> type, String path) { }
538505
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.regex.Pattern;
2626

2727
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.ValueSource;
2830
import org.springframework.data.util.TypeInformation;
2931

3032
/**
@@ -93,6 +95,12 @@ void prefersExplicitPaths() {
9395
assertThat(reference.next()).isEqualTo(new PropertyPath("name", FooBar.class));
9496
}
9597

98+
@ParameterizedTest
99+
@ValueSource(strings = {"user_ name", "user_ Name", "user. Name", "user. name"})
100+
void testPathStartedWithWhitespaceAreNotValid(String source) {
101+
assertThatThrownBy(() -> PropertyPath.from(source, Sample.class)).isInstanceOf(PropertyReferenceException.class);
102+
}
103+
96104
@Test
97105
void handlesGenericsCorrectly() {
98106

0 commit comments

Comments
 (0)