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

Query: Fixes "System.ArgumentException: Stream was not readable." when using WithParameterStream #3155

Merged
merged 7 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 8 additions & 5 deletions Microsoft.Azure.Cosmos/src/CosmosSqlQuerySpecJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
// if the SqlParameter has stream value we dont pass it through the custom serializer.
if (sqlParameter.Value is SerializedParameterValue serializedEncryptedData)
{
using (StreamReader streamReader = new StreamReader(serializedEncryptedData.valueStream))
{
string parameterValue = streamReader.ReadToEnd();
writer.WriteRawValue(parameterValue);
}
StreamReader streamReader = new StreamReader(serializedEncryptedData.valueStream);
kr-santosh marked this conversation as resolved.
Show resolved Hide resolved
string parameterValue = streamReader.ReadToEnd();
writer.WriteRawValue(parameterValue);

// once read need to move back the pointer to start.If it gets reused.
// Query plan will be serialized multiple times for cross partition queries and for scenarios where the query plan can not be generated locally.
// Note: this is a stream passed by user.
serializedEncryptedData.valueStream.Position = 0;
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,36 @@ public async Task QueryStreamValueTest()
Assert.AreEqual(pageCount, fromStreamCount);
}

// get result across pages,multiple requests by setting MaxItemCount to 1.
foreach (QueryDefinition queryDefinition in queryDefinitions)
{
toStreamCount = 0;
fromStreamCount = 0;

List<dynamic> allItems = new List<dynamic>();
int pageCount = 0;
using (FeedIterator<dynamic> feedIterator = containerSerializer.GetItemQueryIterator<dynamic>(
queryDefinition: queryDefinition,
requestOptions: new QueryRequestOptions { MaxItemCount = 1 }))
{
while (feedIterator.HasMoreResults)
{
// Only need once to verify correct serialization of the query definition
FeedResponse<dynamic> response = await feedIterator.ReadNextAsync(this.cancellationToken);
Assert.AreEqual(response.Count, response.Count());
allItems.AddRange(response);
pageCount++;
}
}

Assert.AreEqual(2, allItems.Count, $"missing query results. Only found: {allItems.Count} items for query:{queryDefinition.ToSqlQuerySpec().QueryText}");

// There should be no call to custom serializer since the parameter values are already serialized.
Assert.AreEqual(0, toStreamCount, $"missing to stream call. Expected: 0 , Actual: {toStreamCount} for query:{queryDefinition.ToSqlQuerySpec().QueryText}");
Assert.AreEqual(pageCount, fromStreamCount);
}


// Standard Cosmos Serializer Used

CosmosClient clientStandardSerializer = TestCommon.CreateCosmosClient(useCustomSeralizer: false);
Expand Down