Skip to content
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

ReadMany: Fixes BadRequest when using Ids with single quotes #3732

Merged
merged 2 commits into from
Mar 1, 2023
Merged
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
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.GetClient().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