Skip to content

Commit 786c2e5

Browse files
Add property path tests & update reference documentation.
See: #1851 Original Pull Request: #2940
1 parent e0b2f1e commit 786c2e5

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/antora/modules/ROOT/pages/repositories/query-methods-details.adoc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,34 @@ So our method name would be as follows:
119119
List<Person> findByAddress_ZipCode(ZipCode zipCode);
120120
----
121121

122-
Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).
122+
[NOTE]
123+
====
124+
Because we treat underscores (`_`) as a reserved character, we strongly advise to follow standard Java naming conventions (that is, not using underscores in property names but applying camel case instead).
125+
====
126+
127+
[CAUTION]
128+
====
129+
.Field Names starting with underscore:
130+
Field names may start with underscores like `String \_name`.
131+
Make sure to preserve the `_` as in `\_name` and use double `_` to split nested paths like `user__name`.
132+
133+
.Upper Case Field Names:
134+
Field names that are all uppercase can be used as such.
135+
Nested paths if applicable require splitting via `_` as in `USER_name`.
136+
137+
.Field Names with 2nd uppercase letter:
138+
Field names that consist of a starting lower case letter followed by an uppercase one like `String qCode` can be resolved by starting with two upper case letters as in `QCode`.
139+
Please be aware of potential path ambiguities.
140+
141+
.Path Ambiguities:
142+
In the following sample the arrangement of properties `qCode` and `q`, with `q` containing a property called `code`, creates an ambiguity for the path `QCode`.
143+
```java
144+
record Container(String qCode, Code q) {}
145+
record Code(String code) {}
146+
```
147+
Since a direct match on a property is considered first, any potential nested paths will not be considered and the algorithm picks the `qCode` field.
148+
In order to select the `code` field in `q` the underscore notation `Q_Code` is required.
149+
====
123150

124151
[[repositories.collections-and-iterables]]
125152
== Repository Methods Returning Collections or Iterables

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,19 @@ void findsSecondLetterUpperCaseProperty() {
309309
assertThat(PropertyPath.from("QCode", Foo.class).toDotPath()).isEqualTo("qCode");
310310
assertThat(PropertyPath.from("zIndex", MyRecord.class).toDotPath()).isEqualTo("zIndex");
311311
assertThat(PropertyPath.from("ZIndex", MyRecord.class).toDotPath()).isEqualTo("zIndex");
312+
assertThat(PropertyPath.from("_foo.QCode", Sample2.class).toDotPath()).isEqualTo("_foo.qCode");
313+
assertThat(PropertyPath.from("_fooQCode", Sample2.class).toDotPath()).isEqualTo("_foo.qCode");
314+
}
315+
316+
@Test // GH-1851
317+
void favoursPropertyHitOverNestedPath() {
318+
319+
assertThat(PropertyPath.from("qCode", NameAmbiguities.class).toDotPath()).isEqualTo("qCode");
320+
assertThat(PropertyPath.from("QCode", NameAmbiguities.class).toDotPath()).isEqualTo("qCode");
321+
assertThat(PropertyPath.from("Q_Code", NameAmbiguities.class).toDotPath()).isEqualTo("q.code");
322+
assertThat(PropertyPath.from("q.code", NameAmbiguities.class).toDotPath()).isEqualTo("q.code");
323+
assertThat(PropertyPath.from("Q.Code", NameAmbiguities.class).toDotPath()).isEqualTo("q.code");
324+
assertThat(PropertyPath.from("q_code", NameAmbiguities.class).toDotPath()).isEqualTo("q.code");
312325
}
313326

314327
@Test // DATACMNS-257
@@ -458,6 +471,16 @@ public void setqCode(String qCode) {
458471
}
459472
}
460473

474+
private static class NameAmbiguities {
475+
476+
String qCode;
477+
Code q;
478+
}
479+
480+
private static class Code {
481+
String code;
482+
}
483+
461484
private class Bar {
462485

463486
private FooBar user;

0 commit comments

Comments
 (0)