|
| 1 | +using System.Net; |
| 2 | +using System.Text.Json; |
| 3 | +using FluentAssertions; |
| 4 | +using JetBrains.Annotations; |
| 5 | +using JsonApiDotNetCore.Configuration; |
| 6 | +using JsonApiDotNetCore.Errors; |
| 7 | +using JsonApiDotNetCore.Resources; |
| 8 | +using JsonApiDotNetCore.Serialization.JsonConverters; |
| 9 | +using JsonApiDotNetCore.Serialization.Objects; |
| 10 | +using JsonApiDotNetCore.Serialization.Request; |
| 11 | +using Microsoft.AspNetCore.Http; |
| 12 | +using Microsoft.Extensions.Logging.Abstractions; |
| 13 | +using TestBuildingBlocks; |
| 14 | +using Xunit; |
| 15 | + |
| 16 | +namespace JsonApiDotNetCoreTests.UnitTests.Serialization.Extensions; |
| 17 | + |
| 18 | +public sealed class SourcePointerInExceptionTests |
| 19 | +{ |
| 20 | + private const string RequestBody = """ |
| 21 | + { |
| 22 | + "data": { |
| 23 | + "type": "testResources", |
| 24 | + "attributes": { |
| 25 | + "ext-namespace:ext-name": "ignored" |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + """; |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public async Task Adds_source_pointer_to_JsonApiException_thrown_from_JsonConverter() |
| 33 | + { |
| 34 | + // Arrange |
| 35 | + const string? relativeSourcePointer = null; |
| 36 | + |
| 37 | + var options = new JsonApiOptions(); |
| 38 | + IResourceGraph resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add<TestResource, long>().Build(); |
| 39 | + var converter = new ThrowingResourceObjectConverter(resourceGraph, relativeSourcePointer); |
| 40 | + var reader = new FakeJsonApiReader(RequestBody, options, converter); |
| 41 | + var httpContext = new DefaultHttpContext(); |
| 42 | + |
| 43 | + // Act |
| 44 | + Func<Task> action = async () => await reader.ReadAsync(httpContext.Request); |
| 45 | + |
| 46 | + // Assert |
| 47 | + JsonApiException? exception = (await action.Should().ThrowExactlyAsync<JsonApiException>()).Which; |
| 48 | + |
| 49 | + exception.StackTrace.Should().Contain(nameof(ThrowingResourceObjectConverter)); |
| 50 | + exception.Errors.ShouldHaveCount(1); |
| 51 | + |
| 52 | + ErrorObject error = exception.Errors[0]; |
| 53 | + error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity); |
| 54 | + error.Title.Should().Be("Extension error"); |
| 55 | + error.Source.ShouldNotBeNull(); |
| 56 | + error.Source.Pointer.Should().Be("/data"); |
| 57 | + } |
| 58 | + |
| 59 | + [Fact] |
| 60 | + public async Task Makes_source_pointer_absolute_in_JsonApiException_thrown_from_JsonConverter() |
| 61 | + { |
| 62 | + // Arrange |
| 63 | + const string relativeSourcePointer = "relative/path"; |
| 64 | + |
| 65 | + var options = new JsonApiOptions(); |
| 66 | + IResourceGraph resourceGraph = new ResourceGraphBuilder(options, NullLoggerFactory.Instance).Add<TestResource, long>().Build(); |
| 67 | + var converter = new ThrowingResourceObjectConverter(resourceGraph, relativeSourcePointer); |
| 68 | + var reader = new FakeJsonApiReader(RequestBody, options, converter); |
| 69 | + var httpContext = new DefaultHttpContext(); |
| 70 | + |
| 71 | + // Act |
| 72 | + Func<Task> action = async () => await reader.ReadAsync(httpContext.Request); |
| 73 | + |
| 74 | + // Assert |
| 75 | + JsonApiException? exception = (await action.Should().ThrowExactlyAsync<JsonApiException>()).Which; |
| 76 | + |
| 77 | + exception.StackTrace.Should().Contain(nameof(ThrowingResourceObjectConverter)); |
| 78 | + exception.Errors.ShouldHaveCount(1); |
| 79 | + |
| 80 | + ErrorObject error = exception.Errors[0]; |
| 81 | + error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity); |
| 82 | + error.Title.Should().Be("Extension error"); |
| 83 | + error.Source.ShouldNotBeNull(); |
| 84 | + error.Source.Pointer.Should().Be("/data/relative/path"); |
| 85 | + } |
| 86 | + |
| 87 | + [UsedImplicitly(ImplicitUseTargetFlags.Members)] |
| 88 | + private sealed class TestResource : Identifiable<long>; |
| 89 | + |
| 90 | + private sealed class ThrowingResourceObjectConverter(IResourceGraph resourceGraph, string? relativeSourcePointer) |
| 91 | + : ResourceObjectConverter(resourceGraph) |
| 92 | + { |
| 93 | + private readonly string? _relativeSourcePointer = relativeSourcePointer; |
| 94 | + |
| 95 | + private protected override void ValidateExtensionInAttributes(string extensionNamespace, string extensionName, ResourceType resourceType, |
| 96 | + Utf8JsonReader reader) |
| 97 | + { |
| 98 | + var exception = new JsonApiException(new ErrorObject(HttpStatusCode.UnprocessableEntity) |
| 99 | + { |
| 100 | + Title = "Extension error" |
| 101 | + }); |
| 102 | + |
| 103 | + if (_relativeSourcePointer != null) |
| 104 | + { |
| 105 | + exception.Errors[0].Source = new ErrorSource |
| 106 | + { |
| 107 | + Pointer = _relativeSourcePointer |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + CapturedThrow(exception); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private sealed class FakeJsonApiReader : IJsonApiReader |
| 116 | + { |
| 117 | + private readonly string _requestBody; |
| 118 | + |
| 119 | + private readonly JsonSerializerOptions _serializerOptions; |
| 120 | + |
| 121 | + public FakeJsonApiReader(string requestBody, JsonApiOptions options, ResourceObjectConverter converter) |
| 122 | + { |
| 123 | + _requestBody = requestBody; |
| 124 | + |
| 125 | + _serializerOptions = new JsonSerializerOptions(options.SerializerOptions); |
| 126 | + _serializerOptions.Converters.Add(converter); |
| 127 | + } |
| 128 | + |
| 129 | + public Task<object?> ReadAsync(HttpRequest httpRequest) |
| 130 | + { |
| 131 | + try |
| 132 | + { |
| 133 | + JsonSerializer.Deserialize<Document>(_requestBody, _serializerOptions); |
| 134 | + } |
| 135 | + catch (NotSupportedException exception) when (exception.HasJsonApiException()) |
| 136 | + { |
| 137 | + throw exception.EnrichSourcePointer(); |
| 138 | + } |
| 139 | + |
| 140 | + return Task.FromResult<object?>(null); |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments