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

Error with ToStream using a custom CosmosSerializer #575

Closed
jaegermike opened this issue Jul 19, 2019 · 5 comments · Fixed by #576
Closed

Error with ToStream using a custom CosmosSerializer #575

jaegermike opened this issue Jul 19, 2019 · 5 comments · Fixed by #576
Labels
bug Something isn't working QUERY

Comments

@jaegermike
Copy link

Describe the bug
When using a custom serializer, calling a query method results in an error in the ToStream override, I think because Microsoft.Azure.Cosmos.SqlQuerySpec is internal and not accessible.

To Reproduce
Code is available here:
https://stackoverflow.com/questions/57100837/error-with-getitemqueryiterator-when-using-a-custom-cosmosserializer

Expected behavior
I would think that either the SqlQuerySpec should be available for serialization in this matter, or the call should be routed differently so as to not go through custom serialization.

Environment summary
SDK Version: 3.0
OS Version (e.g. Windows, Linux, MacOSX) Windows

Additional context
Add any other context about the problem here (for example, complete stack traces or logs).

Error message:
System.NotSupportedException: 'The collection type 'Microsoft.Azure.Documents.SqlParameterCollection' on 'Microsoft.Azure.Documents.SqlQuerySpec.Parameters' is not supported.'

@j82w j82w added bug Something isn't working QUERY labels Jul 19, 2019
@j82w
Copy link
Contributor

j82w commented Jul 19, 2019

The easiest fix is for it to use the default serializer for the query text generation. Is there any scenario where a user would need to do custom serialization of the query text?

@jaegermike
Copy link
Author

How do I choose when the client uses the default versus a custom serializer?

@j82w
Copy link
Contributor

j82w commented Jul 19, 2019

There isn't a way exposed to users. It's set in the CosmosQueryClientCore in the SDK internally. I'm working on a PR now to change it.

@j82w
Copy link
Contributor

j82w commented Jul 19, 2019

We are planning on doing a release early next week as long as we don't hit any major issues. Also we didn't hit this issue with our test because our test have access to internal types so we can unit test different scenarios.

@dustensalinas
Copy link

Microsoft.Azure.Cosmos 3.5.1 appears to have a similar issue:

The following is encountered when performing a query with parameters:
Microsoft.Azure.Cosmos.CosmosException: 'Response status code does not indicate success: 400 Substatus: 0 Reason: (Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: 400 Substatus: 0 Reason: (Microsoft.Azure.Documents.DocumentClientException: Gateway Failed to Retrieve Query Plan: Unexpected character encountered while parsing value: }. Path 'parameters[0].value', line 1, position 132

Sample serializer using newtonsoft
` public class NewtonsoftCosmosSerializer : CosmosSerializer
{
public override T FromStream(Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
JsonSerializer ser = JsonSerializer.CreateDefault();
return ser.Deserialize(jsonReader);
}
}

    public override Stream ToStream<T>(T input)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        JsonTextWriter jsonWriter = new JsonTextWriter(writer);
        JsonSerializer ser = JsonSerializer.CreateDefault();
        ser.Serialize(jsonWriter, input);
        jsonWriter.Flush();

        return stream;
    }
}`

Query with the template string returns properly.

Query with Parameters fails with the above exception
QueryDefinition query = new QueryDefinition("select * from auth a where LOWER(a.email) = @Email and a.tenant= @tenant") .WithParameter("@Email", email) .WithParameter("@Tenant", tenant);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working QUERY
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants