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

[BUG] ValidationApiException.Content deserialization (ProblemDetails) bypasses IHttpContentSerializer #1197

Open
hakenr opened this issue Jul 14, 2021 · 1 comment
Labels

Comments

@hakenr
Copy link

hakenr commented Jul 14, 2021

Describe the bug
ValidationApiException.Content deserialization (ProblemDetails) is now hardcoded in ValidationApiException.Create() and uses the System.Text.Json.JsonSerializer.Deserialize<ProblemDetails>() no matter whether there is any IHttpContentSerializer set.

public static ValidationApiException Create(ApiException exception)
{
    if (exception is null)
        throw new ArgumentNullException(nameof(exception));
    if (string.IsNullOrWhiteSpace(exception.Content))
        throw new ArgumentException("Content must be an 'application/problem+json' compliant json string.");

    var ex = new ValidationApiException(exception);

    if(!string.IsNullOrWhiteSpace(exception.Content))
    {
        ex.Content = JsonSerializer.Deserialize<ProblemDetails>(exception.Content!, SerializerOptions);
    }

    return ex;
}

The original implementation used to use IHttpContentSerializer for ProblemDetails deserialization. We use the IHttpContentSerializer for some custom-transformations of the incoming ProblemDetails, which is no longer possible.

public static async Task<ValidationApiException> Create(ApiException exception)
{
    return new ValidationApiException(exception)
    {
        Content = await exception.GetContentAsAsync<ProblemDetails>().ConfigureAwait(false)
    };
}

Expected behavior
ValidationApiException.Content being deserialized using the configured IHttpContentSerializer.

Workaround
You can call validationException.GetContentAsAsync<ProblemDetails>() on your own. It uses the IHttpContentSerializer and can help you get the result you need.

@hakenr hakenr added the bug label Jul 14, 2021
@angularsen
Copy link

We just hit this too, I expected Newtonsoft to be used to deserialize problem details extensions when Newtonsoft was configured, but we got System.Text.Json.JsonElement types instead for the objects in the Dictionary<string, object> Extensions property.

It is fine once you know about it, but it is a pitfall and we now have to have code in place to handle both Newtonsoft and SystemJson in case Refit changes this behavior in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants