Skip to content

Trying to parse non-json responses - 'U' is an invalid start of a value. LineNumber: 0 #554

@mikocot

Description

@mikocot

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 (#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:

    public Func<HttpResponseMessage, bool> 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions