Skip to content

Commit 213313e

Browse files
author
Bart Koelman
committed
Updated error message for unknown resource type
1 parent 4d05ee7 commit 213313e

23 files changed

+35
-31
lines changed

src/JsonApiDotNetCore/Serialization/RequestAdapters/ResourceIdentityAdapter.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,7 @@ state.Request.WriteOperation is WriteOperationKind.CreateResource or WriteOperat
143143

144144
ResourceContext resourceContext = _resourceGraph.TryGetResourceContext(identity.Type);
145145

146-
if (resourceContext == null)
147-
{
148-
throw new DeserializationException(state.Position, "Request body includes unknown resource type.",
149-
$"Resource type '{identity.Type}' does not exist.");
150-
}
146+
AssertIsKnownResourceType(resourceContext, identity.Type, state);
151147

152148
if (requirements?.ResourceContext != null && !requirements.ResourceContext.ResourceType.IsAssignableFrom(resourceContext.ResourceType))
153149
{
@@ -185,6 +181,14 @@ state.Request.WriteOperation is WriteOperationKind.CreateResource or WriteOperat
185181
return resourceContext;
186182
}
187183

184+
private static void AssertIsKnownResourceType(ResourceContext resourceContext, string typeName, RequestAdapterState state)
185+
{
186+
if (resourceContext == null)
187+
{
188+
throw new ModelConversionException(state.Position, "Unknown resource type found.", $"Resource type '{typeName}' does not exist.");
189+
}
190+
}
191+
188192
private void AssignStringId(IResourceIdentity identity, IIdentifiable resource, RequestAdapterState state)
189193
{
190194
if (identity.Id != null)

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Creating/AtomicCreateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ public async Task Cannot_create_resource_for_unknown_type()
634634

635635
ErrorObject error = responseDocument.Errors[0];
636636
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
637-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
637+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
638638
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
639639
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/type");
640640
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Creating/AtomicCreateResourceWithToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public async Task Cannot_create_for_unknown_relationship_type()
282282

283283
ErrorObject error = responseDocument.Errors[0];
284284
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
285-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
285+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
286286
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
287287
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/relationships/performers/data[0]/type");
288288
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Creating/AtomicCreateResourceWithToOneRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public async Task Cannot_create_for_unknown_relationship_type()
344344

345345
ErrorObject error = responseDocument.Errors[0];
346346
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
347-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
347+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
348348
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
349349
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/relationships/lyric/data/type");
350350
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Deleting/AtomicDeleteResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public async Task Cannot_delete_resource_for_unknown_type()
461461

462462
ErrorObject error = responseDocument.Errors[0];
463463
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
464-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
464+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
465465
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
466466
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/type");
467467
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Relationships/AtomicAddToToManyRelationshipTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public async Task Cannot_add_for_unknown_type_in_ref()
338338

339339
ErrorObject error = responseDocument.Errors[0];
340340
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
341-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
341+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
342342
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
343343
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/type");
344344
}
@@ -683,7 +683,7 @@ public async Task Cannot_add_for_unknown_type_in_data()
683683

684684
ErrorObject error = responseDocument.Errors[0];
685685
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
686-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
686+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
687687
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
688688
error.Source.Pointer.Should().Be("/atomic:operations[0]/data[0]/type");
689689
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Relationships/AtomicRemoveFromToManyRelationshipTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public async Task Cannot_remove_for_unknown_type_in_ref()
338338

339339
ErrorObject error = responseDocument.Errors[0];
340340
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
341-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
341+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
342342
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
343343
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/type");
344344
}
@@ -646,7 +646,7 @@ public async Task Cannot_remove_for_unknown_type_in_data()
646646

647647
ErrorObject error = responseDocument.Errors[0];
648648
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
649-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
649+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
650650
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
651651
error.Source.Pointer.Should().Be("/atomic:operations[0]/data[0]/type");
652652
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Relationships/AtomicReplaceToManyRelationshipTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public async Task Cannot_replace_for_unknown_type_in_ref()
374374

375375
ErrorObject error = responseDocument.Errors[0];
376376
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
377-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
377+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
378378
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
379379
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/type");
380380
}
@@ -738,7 +738,7 @@ public async Task Cannot_replace_for_unknown_type_in_data()
738738

739739
ErrorObject error = responseDocument.Errors[0];
740740
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
741-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
741+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
742742
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
743743
error.Source.Pointer.Should().Be("/atomic:operations[0]/data[0]/type");
744744
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Relationships/AtomicUpdateToOneRelationshipTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ public async Task Cannot_create_for_unknown_type_in_ref()
621621

622622
ErrorObject error = responseDocument.Errors[0];
623623
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
624-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
624+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
625625
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
626626
error.Source.Pointer.Should().Be("/atomic:operations[0]/ref/type");
627627
}
@@ -978,7 +978,7 @@ public async Task Cannot_create_for_unknown_type_in_data()
978978

979979
ErrorObject error = responseDocument.Errors[0];
980980
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
981-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
981+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
982982
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
983983
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/type");
984984
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Resources/AtomicReplaceToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ public async Task Cannot_replace_for_unknown_type_in_relationship_data()
439439

440440
ErrorObject error = responseDocument.Errors[0];
441441
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
442-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
442+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
443443
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
444444
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/relationships/performers/data[0]/type");
445445
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Resources/AtomicUpdateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ public async Task Cannot_update_resource_for_unknown_type()
13331333

13341334
ErrorObject error = responseDocument.Errors[0];
13351335
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
1336-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
1336+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
13371337
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
13381338
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/type");
13391339
}

test/JsonApiDotNetCoreTests/IntegrationTests/AtomicOperations/Updating/Resources/AtomicUpdateToOneRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ public async Task Cannot_create_for_unknown_type_in_relationship_data()
712712

713713
ErrorObject error = responseDocument.Errors[0];
714714
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
715-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
715+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
716716
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
717717
error.Source.Pointer.Should().Be("/atomic:operations[0]/data/relationships/lyric/data/type");
718718
}

test/JsonApiDotNetCoreTests/IntegrationTests/ExceptionHandling/ExceptionHandlerTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public async Task Logs_and_produces_error_response_on_deserialization_failure()
108108

109109
ErrorObject error = responseDocument.Errors[0];
110110
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
111-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
111+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
112112
error.Detail.Should().Be("Resource type '' does not exist.");
113113

114114
IEnumerable<string> stackTraceLines = ((JsonElement)error.Meta["stackTrace"]).EnumerateArray().Select(token => token.GetString());

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ public async Task Cannot_create_resource_for_unknown_type()
609609

610610
ErrorObject error = responseDocument.Errors[0];
611611
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
612-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
612+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
613613
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
614614
error.Source.Pointer.Should().Be("/data/type");
615615

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public async Task Cannot_create_for_unknown_relationship_type()
402402

403403
ErrorObject error = responseDocument.Errors[0];
404404
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
405-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
405+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
406406
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
407407
error.Source.Pointer.Should().Be("/data/relationships/subscribers/data[0]/type");
408408

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Creating/CreateResourceWithToOneRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public async Task Cannot_create_for_unknown_relationship_type()
359359

360360
ErrorObject error = responseDocument.Errors[0];
361361
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
362-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
362+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
363363
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
364364
error.Source.Pointer.Should().Be("/data/relationships/assignee/data/type");
365365

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Relationships/AddToToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
286286

287287
ErrorObject error = responseDocument.Errors[0];
288288
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
289-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
289+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
290290
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
291291
error.Source.Pointer.Should().Be("/data[0]/type");
292292

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Relationships/RemoveFromToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
402402

403403
ErrorObject error = responseDocument.Errors[0];
404404
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
405-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
405+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
406406
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
407407
error.Source.Pointer.Should().Be("/data[0]/type");
408408

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Relationships/ReplaceToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
317317

318318
ErrorObject error = responseDocument.Errors[0];
319319
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
320-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
320+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
321321
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
322322
error.Source.Pointer.Should().Be("/data[0]/type");
323323

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Relationships/UpdateToOneRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
344344

345345
ErrorObject error = responseDocument.Errors[0];
346346
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
347-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
347+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
348348
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
349349
error.Source.Pointer.Should().Be("/data/type");
350350

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/ReplaceToManyRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
495495

496496
ErrorObject error = responseDocument.Errors[0];
497497
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
498-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
498+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
499499
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
500500
error.Source.Pointer.Should().Be("/data/relationships/subscribers/data[0]/type");
501501

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateResourceTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
817817

818818
ErrorObject error = responseDocument.Errors[0];
819819
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
820-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
820+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
821821
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
822822
error.Source.Pointer.Should().Be("/data/type");
823823

test/JsonApiDotNetCoreTests/IntegrationTests/ReadWrite/Updating/Resources/UpdateToOneRelationshipTests.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ await _testContext.RunOnDatabaseAsync(async dbContext =>
530530

531531
ErrorObject error = responseDocument.Errors[0];
532532
error.StatusCode.Should().Be(HttpStatusCode.UnprocessableEntity);
533-
error.Title.Should().Be("Failed to deserialize request body: Request body includes unknown resource type.");
533+
error.Title.Should().Be("Failed to deserialize request body: Unknown resource type found.");
534534
error.Detail.Should().Be($"Resource type '{Unknown.ResourceType}' does not exist.");
535535
error.Source.Pointer.Should().Be("/data/relationships/assignee/data/type");
536536

0 commit comments

Comments
 (0)