Skip to content

Commit

Permalink
ReadMany: Fixes BadRequest when using Ids with single quotes (#3732)
Browse files Browse the repository at this point in the history
* Use parameters

* Emulator tests
  • Loading branch information
ealsur committed Mar 1, 2023
1 parent 8a537ce commit 131b78e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
8 changes: 6 additions & 2 deletions Microsoft.Azure.Cosmos/src/ReadManyQueryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,18 +249,22 @@ private QueryDefinition CreateReadManyQueryDefinitionForId(List<(string, Partiti
{
int totalItemCount = Math.Min(items.Count, startIndex + this.maxItemsPerQuery);
StringBuilder queryStringBuilder = new StringBuilder();
SqlParameterCollection sqlParameters = new SqlParameterCollection();
queryStringBuilder.Append("SELECT * FROM c WHERE c.id IN ( ");
for (int i = startIndex; i < totalItemCount; i++)
{
queryStringBuilder.Append($"'{items[i].Item1}'");
string idParamName = "@param_id" + i;
sqlParameters.Add(new SqlParameter(idParamName, items[i].Item1));
queryStringBuilder.Append(idParamName);
if (i < totalItemCount - 1)
{
queryStringBuilder.Append(",");
}
}
queryStringBuilder.Append(" )");

return new QueryDefinition(queryStringBuilder.ToString());
return QueryDefinition.CreateFromQuerySpec(new SqlQuerySpec(queryStringBuilder.ToString(),
sqlParameters));
}

private QueryDefinition CreateReadManyQueryDefinitionForOther(List<(string, PartitionKey)> items,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,27 @@
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Fluent;
using Microsoft.Azure.Cosmos.Query.Core;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class CosmosReadManyItemsTests : BaseCosmosClientHelper
{
private Container Container = null;
private ContainerProperties containerSettings = null;

[TestInitialize]
public async Task TestInitialize()
{
await base.TestInit();
string PartitionKey = "/pk";
this.containerSettings = new ContainerProperties(id: Guid.NewGuid().ToString(), partitionKeyPath: PartitionKey);
ContainerProperties containerSettings = new ContainerProperties(id: Guid.NewGuid().ToString(), partitionKeyPath: PartitionKey);
ContainerResponse response = await this.database.CreateContainerAsync(
this.containerSettings,
containerSettings,
throughput: 20000,
cancellationToken: this.cancellationToken);
Assert.IsNotNull(response);
Expand Down Expand Up @@ -122,23 +116,24 @@ public async Task ReadManyDoesNotFetchQueryPlan()
[TestMethod]
public async Task ReadManyWithIdasPk()
{
string PartitionKey = "/id";
ContainerProperties containerSettings = new ContainerProperties(id: Guid.NewGuid().ToString(), partitionKeyPath: PartitionKey);
Container container = await this.database.CreateContainerAsync(containerSettings);
Container container = await this.database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id");

List<(string, PartitionKey)> itemList = new List<(string, PartitionKey)>();
for (int i = 0; i < 5; i++)
{
itemList.Add((i.ToString(), new PartitionKey(i.ToString())));
}

// Create items with different pk values
for (int i = 0; i < 5; i++)
{
ToDoActivity item = ToDoActivity.CreateRandomToDoActivity();
item.id = i.ToString();
ItemResponse<ToDoActivity> itemResponse = await container.CreateItemAsync(item);
Assert.AreEqual(HttpStatusCode.Created, itemResponse.StatusCode);

itemList.Add((item.id, new PartitionKey(item.id)));

ToDoActivity itemWithSingleQuotes = ToDoActivity.CreateRandomToDoActivity(id: item.id + "'singlequote");
ItemResponse<ToDoActivity> itemResponseWithSingleQuotes = await container.CreateItemAsync(itemWithSingleQuotes);
Assert.AreEqual(HttpStatusCode.Created, itemResponseWithSingleQuotes.StatusCode);

itemList.Add((itemWithSingleQuotes.id, new PartitionKey(itemWithSingleQuotes.id)));
}

using (ResponseMessage responseMessage = await container.ReadManyItemsStreamAsync(itemList))
Expand All @@ -149,12 +144,12 @@ public async Task ReadManyWithIdasPk()

ToDoActivity[] items = this.cosmosClient.ClientContext.SerializerCore.FromFeedStream<ToDoActivity>(
CosmosFeedResponseSerializer.GetStreamWithoutServiceEnvelope(responseMessage.Content));
Assert.AreEqual(items.Length, 5);
Assert.AreEqual(items.Length, 10);
}

FeedResponse<ToDoActivity> feedResponse = await container.ReadManyItemsAsync<ToDoActivity>(itemList);
Assert.IsNotNull(feedResponse);
Assert.AreEqual(feedResponse.Count, 5);
Assert.AreEqual(feedResponse.Count, 10);
Assert.IsTrue(feedResponse.Headers.RequestCharge > 0);
Assert.IsNotNull(feedResponse.Diagnostics);
}
Expand Down

0 comments on commit 131b78e

Please sign in to comment.