Skip to content

Commit

Permalink
fix query parameter name generator
Browse files Browse the repository at this point in the history
Replace any non-alphanumeric characters in the generated query parameter name with an underscore
so that criteria with references to nested properties such as user['first name'] do not result
in a bad request due to invalid query parameter name.

Resolves Azure#23084
  • Loading branch information
gogopavl committed Aug 4, 2021
1 parent e747987 commit 81e1ba2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,19 @@ public void testBetweenCriteria() {
assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testEqualCriteriaWithNestedProperty() {
String postalCode = ADDRESSES.get(0).getPostalCode();
String subjectWithNestedProperty = "shippingAddresses[0]['postalCode']";
Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, subjectWithNestedProperty,
Collections.singletonList(postalCode), Part.IgnoreCaseType.NEVER);

List<Person> people = TestUtils.toList(cosmosTemplate.find(new CosmosQuery(criteria), Person.class,
containerName));

assertThat(people).containsExactly(TEST_PERSON);
}

@Test
public void testRunQueryWithSimpleReturnType() {
Criteria ageBetween = Criteria.getInstance(CriteriaType.BETWEEN, "age", Arrays.asList(AGE - 1, AGE + 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ protected AbstractQueryGenerator() {
}

private String generateQueryParameter(@NonNull String subject) {
// user.name is not valid sql parameter identifier.
return subject.replaceAll("\\.", "_") + UUID.randomUUID().toString().replaceAll("-", "_");
// user.name, user['name'] or user["first name"] are not valid sql parameter identifiers.
return subject.replaceAll("[^a-zA-Z\\d]", "_") + UUID.randomUUID().toString().replaceAll("-", "_");
}

private String generateUnaryQuery(@NonNull Criteria criteria) {
Expand Down

0 comments on commit 81e1ba2

Please sign in to comment.