Skip to content

Commit 4f0f350

Browse files
authored
fixed escaping of array brackets (#209)
1 parent 7e94858 commit 4f0f350

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/main/java/com/arangodb/springframework/core/util/AqlUtils.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
package com.arangodb.springframework.core.util;
2222

23-
import java.util.StringJoiner;
24-
2523
import org.springframework.data.domain.Pageable;
2624
import org.springframework.data.domain.Sort;
2725
import org.springframework.lang.Nullable;
2826
import org.springframework.util.StringUtils;
2927

28+
import java.util.StringJoiner;
29+
3030
/**
3131
*
3232
* @author Christian Lechner
@@ -155,16 +155,12 @@ else if (currChar == '`') {
155155
throw new IllegalArgumentException(
156156
"Sort properties must only contain backticks at beginning/end of attributes or when escaped.");
157157
}
158-
}
159-
160-
else if (currChar == '.') {
158+
} else if (currChar == '.') {
161159
// the dot is part of an attribute name when inside escaped sequence
162160
if (inEscapedSeq) {
163161
// add dot without escaping
164162
escaped.append('.');
165-
}
166-
167-
else {
163+
} else {
168164
// properties can only contain 2+ dots in escaped sequences
169165
if (nextChar == '.') {
170166
throw new IllegalArgumentException(
@@ -179,6 +175,21 @@ else if (nextChar == '`') {
179175
// close previous escape sequence and open new one
180176
escaped.append("`.`");
181177
}
178+
} else if (currChar == '[' || currChar == ']') {
179+
// square brackets are part of an attribute name when inside escaped sequence
180+
if (inEscapedSeq) {
181+
escaped.append(currChar);
182+
} else {
183+
if (currChar == '[') {
184+
// close previous escape sequence
185+
escaped.append("`");
186+
}
187+
escaped.append(currChar);
188+
if (nextChar == '.') {
189+
escaped.append(".`");
190+
i++;
191+
}
192+
}
182193
}
183194

184195
// keep others

src/test/java/com/arangodb/springframework/core/util/AqlUtilsTest.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020

2121
package com.arangodb.springframework.core.util;
2222

23-
import static org.hamcrest.MatcherAssert.assertThat;
24-
import static org.hamcrest.core.Is.is;
25-
2623
import org.junit.Assert;
2724
import org.junit.Test;
2825
import org.springframework.data.domain.PageRequest;
2926
import org.springframework.data.domain.Pageable;
3027
import org.springframework.data.domain.Sort;
3128
import org.springframework.data.domain.Sort.Direction;
3229

30+
import static org.hamcrest.MatcherAssert.assertThat;
31+
import static org.hamcrest.core.Is.is;
32+
3333
/**
3434
*
3535
* @author Christian Lechner
@@ -119,15 +119,21 @@ public void sortClauseEscapingTest() {
119119
is("SORT `property`.`\\\\\\``.`property` ASC"));
120120

121121
assertThat(AqlUtils.buildSortClause(Sort.by("`property.\\`.property`")),
122-
is("SORT `property.\\`.property` ASC"));
122+
is("SORT `property.\\`.property` ASC"));
123123

124124
assertThat(AqlUtils.buildSortClause(Sort.by("`property.\\``.property")),
125-
is("SORT `property.\\``.`property` ASC"));
125+
is("SORT `property.\\``.`property` ASC"));
126126

127127
assertThat(AqlUtils.buildSortClause(Sort.by("`property..property`")), is("SORT `property..property` ASC"));
128128

129129
assertThat(AqlUtils.buildSortClause(Sort.by("property\\. REMOVE doc IN collection //")),
130-
is("SORT `property\\\\`.` REMOVE doc IN collection //` ASC"));
130+
is("SORT `property\\\\`.` REMOVE doc IN collection //` ASC"));
131+
132+
assertThat(AqlUtils.buildSortClause(Sort.by("values[0].date")),
133+
is("SORT `values`[0].`date` ASC"));
134+
135+
assertThat(AqlUtils.buildSortClause(Sort.by("`values[0]`.date")),
136+
is("SORT `values[0]`.`date` ASC"));
131137

132138
// Illegal sort properties
133139

0 commit comments

Comments
 (0)