diff --git a/ShopifySharp.Tests/Services/Graph/GraphService.PostAsyncTests.cs b/ShopifySharp.Tests/Services/Graph/GraphService.PostAsyncTests.cs index 4e322db3..b946340c 100644 --- a/ShopifySharp.Tests/Services/Graph/GraphService.PostAsyncTests.cs +++ b/ShopifySharp.Tests/Services/Graph/GraphService.PostAsyncTests.cs @@ -769,6 +769,47 @@ public async Task PostAsync_WithReturnTypeParameter_WhenGivenInvalidJson_ShouldT .Path.Should().BeNull(exn.Which.JsonPropertyName); } + [Fact(DisplayName = "PostAsync(GraphRequest graphRequest, Type returnType) should throw a ShopifyJsonParseException when the json serializer throws an exception during final deserialization to the desired return type")] + public async Task PostAsync_WithReturnTypeParameter_WhenTheJsonSerializerThrowsAnExceptionDuringFinalDeserializationToTheDesiredReturnType_ShouldThrowAShopifyJsonParseException() + { + // Setup + const string responseJson = + """ + { + "data": { + "fooOperation": { + "foo": "some-foo", + "bar": "some-bar", + "baz": { + "bat": "some-bat", + "qux": null + } + } + } + } + """; + const string expectedRequestId = "some-expected-request-id"; + var expectedReturnType = typeof(TestGraphOperation); + var graphRequest = GraphServiceTestUtils.MakeGraphRequest(); + + var expectedDeserializeCall = A.CallTo(() => + _jsonSerializer.DeserializeAsync(A._, expectedReturnType, CancellationToken.None)); + expectedDeserializeCall.Throws(); + + A.CallTo(_policy) + .WithReturnType>>() + .Returns(Utils.MakeRequestResult(responseJson, x => x.RequestId = expectedRequestId)); + + // Act + var act = async () => await _sut.PostAsync(graphRequest, expectedReturnType); + + // Assert + await act.Should().ThrowAsync() + .Where(x => x.RequestId == expectedRequestId) + .WithInnerExceptionExactly(typeof(TestException)); + expectedDeserializeCall.MustHaveHappenedOnceExactly(); + } + [Theory(DisplayName = "PostAsync(GraphRequest graphRequest, Type returnType) should throw a ShopifyJsonParseException when the data object is null, missing or does not match the expected primitive type")] [InlineData(""" "data": null """, JsonValueKind.Null)] [InlineData(""" "data": true """, JsonValueKind.True)] diff --git a/ShopifySharp/Services/Graph/GraphService.cs b/ShopifySharp/Services/Graph/GraphService.cs index bebff1de..214069fd 100644 --- a/ShopifySharp/Services/Graph/GraphService.cs +++ b/ShopifySharp/Services/Graph/GraphService.cs @@ -105,7 +105,6 @@ public virtual async Task> PostAsync(GraphRequest graphReque try { - // TODO: add a test for this data = await _jsonSerializer.DeserializeAsync(dataElement, resultType, cancellationToken); } catch (Exception exn)