Describe the bug
An exception is raised when trying to call a generated proxy method with an Optional<IReadOnlyList<string>> parameter.
It is a json exception coming up when a http json formatting operation is performed.
It seems (just my opinion) that it is not able to serialize a list (as it has no IValueSerializer for lists and each string list variable is treated as a simple string).
I report the key part of the stack trace:
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource, Int32 currentDepth, Byte token, JsonTokenType tokenType)
at System.Text.Json.Utf8JsonWriter.WriteStringByOptionsPropertyName(ReadOnlySpan`1 propertyName)
at System.Text.Json.Utf8JsonWriter.WritePropertyName(ReadOnlySpan`1 propertyName)
at System.Text.Json.Utf8JsonWriter.WritePropertyName(String propertyName)
at StrawberryShake.Http.JsonOperationFormatter.WriteValue(Object value, Utf8JsonWriter writer)
at StrawberryShake.Http.JsonOperationFormatter.WriteJsonRequest(IOperation operation, Utf8JsonWriter writer, IReadOnlyDictionary`2 extensions, Boolean includeId, Boolean includeDocument, CancellationToken cancellationToken)
at StrawberryShake.Http.JsonOperationFormatter.SerializeInternal(IOperation operation, IBufferWriter`1 writer, IReadOnlyDictionary`2 extensions, Boolean includeId, Boolean includeDocument, CancellationToken cancellationToken)
To Reproduce
Steps to reproduce the behavior:
I have the following query to be generated:
query myQuery($list: [String]!) {
someActualQuery(list: $list)
}
with the following GraphQL (with HotChocolate) query:
``` c#
public string someActualQuery(IList
return "Hello World!";
}
Strawberry Shake generates and expects the following property:
``` c#
public Optional<IReadOnlyList<string>> Memberships { get; set; }
The berry.json contains the following info:
{
"Schemas": [
{
"Name": "Test",
"Type": "http",
"File": "Test.graphql",
"Url": "http://localhost:5000/graphql"
}
],
"ClientName": "TestClient"
}
The server at http://localhost:5000 is running and exposing the someActualQuery query, which works smoothly.
Try to call the method by the means of the generated client:
c#
var myList = new List<string>() { "Hello", "World!" };
ITestClient someClientName;
someClientName.myQuery(myList);
Expected behavior
The proxy is expected to correctly handle the request and call the GraphQL endpoint.
Desktop (please complete the following information):
Additional context
Found the bug at StrawberryShake.Http.JsonOperationFormatter:
c#
private object SerializeVariable(object obj, IValueSerializer serializer)
{
IList list = obj as IList;
if (list != null)
{
List<object> serialized = new List<object>();
{
foreach (object element in list)
{
serialized.Add(SerializeVariable(element, serializer));
}
// ---------------------------------------------------> Begin BUG
return serializer; // -> return serialized; It should return the serialized list instead of the last serializer used to serialize
// --------------------------------------------------- End Bug
}
}
return serializer.Serialize(obj);
}
Thanks for reporting ...
we are at the moment working on the next preview version. We will fix this I the process.
@sangelastro , is this fixed in the latest version anyway? You're on 11.0.0-preview.58 but there's 11.0.0-preview.103.
Looks fixed on main, but not sure how long ago a build was done there :P
https://github.com/ChilliCream/hotchocolate/blob/main/src/StrawberryShake/Client/src/Http/JsonOperationFormatter.cs#L162-180
Would seem like the nuget package for 103 was published after the last commit that modified that file though.
Confirmed with ILSpy too on the 103 dll, so you should be fine to use that version.
Ok, thanks, currently I am on 11.0.0-preview.137 and everything seems to work.