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

feat: Add support for ProblemDetails extensions #788

Merged
merged 2 commits into from
Nov 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,11 @@ try
}
catch (ValidationApiException validationException)
{
// handle validation here by using validationException.Content,
// handle validation here by using validationException.Content,
// which is type of ProblemDetails according to RFC 7807

// If the response contains additional properties on the problem details,
// they will be added to the validationException.Content.Extensions collection.
}
catch (ApiException exception)
{
Expand Down
36 changes: 36 additions & 0 deletions Refit.Tests/ResponseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,42 @@ public async Task ThrowsValidationException()
Assert.Equal("type", actualException.Content.Type);
}

[Fact]
public async Task WhenProblemDetailsResponseContainsExtensions_ShouldHydrateExtensions()
{
var expectedContent = new
{
Detail = "detail",
Instance = "instance",
Status = 1,
Title = "title",
Type = "type",
Foo = "bar",
Baz = 123d,
};

var expectedResponse = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent(JsonConvert.SerializeObject(expectedContent))
};

expectedResponse.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/problem+json");
mockHandler.Expect(HttpMethod.Get, "http://api/aliasTest")
.Respond(req => expectedResponse);

var actualException = await Assert.ThrowsAsync<ValidationApiException>(() => fixture.GetTestObject());
Assert.NotNull(actualException.Content);
Assert.Equal("detail", actualException.Content.Detail);
Assert.Equal("instance", actualException.Content.Instance);
Assert.Equal(1, actualException.Content.Status);
Assert.Equal("title", actualException.Content.Title);
Assert.Equal("type", actualException.Content.Type);

Assert.Collection(actualException.Content.Extensions,
kvp => Assert.Equal(new KeyValuePair<string, object>(nameof(expectedContent.Foo), expectedContent.Foo), kvp),
kvp => Assert.Equal(new KeyValuePair<string, object>(nameof(expectedContent.Baz), expectedContent.Baz), kvp));
}

[Fact]
public async Task BadRequestWithEmptyContent_ShouldReturnApiException()
{
Expand Down
10 changes: 9 additions & 1 deletion Refit/ProblemDetails.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Refit
{
Expand All @@ -12,6 +14,12 @@ public class ProblemDetails
/// </summary>
public Dictionary<string, string[]> Errors { get; set; } = new Dictionary<string, string[]>();

/// <summary>
/// Collection of ProblemDetails extensions
/// </summary>
[JsonExtensionData]
public IDictionary<string, object> Extensions { get; } = new Dictionary<string, object>(StringComparer.Ordinal);

/// <summary>
/// A URI reference that identifies the problem type.
/// </summary>
Expand Down