Skip to content

Commit

Permalink
Add test for incompatible ID value.
Browse files Browse the repository at this point in the history
By default, this produces:
```
The JSON value could not be converted to JsonApiDotNetCore.Serialization.Objects.SingleOrManyData`1[JsonApiDotNetCore.Serialization.Objects.ResourceObject]. Path: $.data | LineNumber: 3 | BytePositionInLine: 11.
```
which is totally unhelpful. Because this is so likely to hit users, we special-case here to produce a better error.
  • Loading branch information
Bart Koelman committed Sep 14, 2021
1 parent eaf9a33 commit 32f3af0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public override ResourceObject Read(ref Utf8JsonReader reader, Type typeToConver
{
case "id":
{
if (reader.TokenType != JsonTokenType.String)
{
// Newtonsoft.Json used to auto-convert number to strings, while System.Text.Json does not. This is so likely
// to hit users during upgrade that we special-case for this and produce a helpful error message.
var jsonElement = JsonConverterSupport.ReadSubTree<JsonElement>(ref reader, options);
throw new JsonException($"Failed to convert ID '{jsonElement}' of type '{jsonElement.ValueKind}' to type 'String'.");
}

resourceObject.Id = reader.GetString();
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,46 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
error.Detail.Should().StartWith("Resource ID is read-only. - Request body: <<");
}

[Fact]
public async Task Cannot_update_resource_with_incompatible_ID_value()
{
// Arrange
WorkItem existingWorkItem = _fakers.WorkItem.Generate();

await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.WorkItems.Add(existingWorkItem);
await dbContext.SaveChangesAsync();
});

var requestBody = new
{
data = new
{
type = "workItems",
id = existingWorkItem.Id,
attributes = new
{
}
}
};

string route = $"/workItems/{existingWorkItem.StringId}";

// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePatchAsync<Document>(route, requestBody);

// Assert
httpResponse.Should().HaveStatusCode(HttpStatusCode.UnprocessableEntity);

responseDocument.Errors.Should().HaveCount(1);

ErrorObject error = responseDocument.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
error.Title.Should().Be("Failed to deserialize request body.");
error.Detail.Should().StartWith($"Failed to convert ID '{existingWorkItem.Id}' of type 'Number' to type 'String'. - Request body: <<");
}

[Fact]
public async Task Cannot_update_resource_with_incompatible_attribute_value()
{
Expand Down

0 comments on commit 32f3af0

Please sign in to comment.