Skip to content

GH-3121 Throw PropertyReferenceException for whitespace-starting Prop… #3321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 6 additions & 39 deletions src/main/java/org/springframework/data/mapping/PropertyPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class PropertyPath implements Streamable<PropertyPath> {

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";

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

if (source.isEmpty() || Character.isWhitespace(source.charAt(0))) {
throw e;
}

exception = e;
}

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

private static final class Property {

private final TypeInformation<?> type;
private final String path;

private Property(TypeInformation<?> type, String path) {
this.type = type;
this.path = path;
}

@Override
public boolean equals(@Nullable Object obj) {

if (obj == this) {
return true;
}

if (!(obj instanceof Property that)) {
return false;
}

return Objects.equals(this.type, that.type) &&
Objects.equals(this.path, that.path);
}

@Override
public int hashCode() {
return Objects.hash(type, path);
}

@Override
public String toString() {

return "Key[" +
"type=" + type + ", " +
"path=" + path + ']';
}
}
private record Property(TypeInformation<?> type, String path) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.regex.Pattern;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.data.util.TypeInformation;

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

@ParameterizedTest
@ValueSource(strings = {"user_ name", "user_ Name", "user. Name", "user. name"})
void testPathStartedWithWhitespaceAreNotValid(String source) {
assertThatThrownBy(() -> PropertyPath.from(source, Sample.class)).isInstanceOf(PropertyReferenceException.class);
}

@Test
void handlesGenericsCorrectly() {

Expand Down