graphql-dotnet / graphql-dotnet/graphql-client
Trying to parse non-json responses - 'U' is an invalid start of a value. LineNumber: 0
- Dominant language
- C#
- Stars
- 648
- Forks
- 136
- PR merge metrics
- No merged PRs in 30d
Description
When using persisted queries the server is expected to return BadRequest whenever hash isn't correct or wasn't registered yet.
Such responses are part of the process and come as plain text (html/text). This means they obviously can't be deserialized to any response type using neither System.Text.Json nor Newtonsoft, not even to an object.
At the same time the default `IsValidResponseToDeserialize` seems to be perfectly fine with passing such responses to the serializer.
I can understand originally this behavior was introduced to allow for parsing validation errors (https://github.com/graphql-dotnet/graphql-client/pull/419), but I don't think it was meant to be as permissive (disregard content type).
A workaround is obviously to provide an alternative implementation, but I simply don't see a reasony why one would ever pass something that is not json-like to a json serializer, while the method is very precisely called: `IsValidResponseToDeserialize`. BadRequest html/text is neither valid nor possible to deserialize.
Current implementation:
```c#
public Func IsValidResponseToDeserialize { get; set; } = r =>
// Why not application/json? See https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#processing-the-response
r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.BadRequest || r.Content.Headers.ContentType?.MediaType == "application/graphql+json";
```
NOTE: this is just one of the cases, but such errors can be returned in many other situations.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.