Skip to content

Commit f889c90

Browse files
author
Bart Koelman
committed
Various small fixes
1 parent a5dddad commit f889c90

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ public CustomArticleService(
2626
public override async Task<Article> GetAsync(int id)
2727
{
2828
var newEntity = await base.GetAsync(id);
29-
30-
if (newEntity != null)
31-
{
32-
newEntity.Name = "None for you Glen Coco";
33-
}
34-
29+
newEntity.Name = "None for you Glen Coco";
3530
return newEntity;
3631
}
3732
}

src/JsonApiDotNetCore/Exceptions/InvalidRequestBodyException.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,44 @@ namespace JsonApiDotNetCore.Exceptions
99
/// </summary>
1010
public sealed class InvalidRequestBodyException : JsonApiException
1111
{
12+
private readonly string _details;
13+
private string _requestBody;
14+
1215
public InvalidRequestBodyException(string reason, string details, string requestBody, Exception innerException = null)
1316
: base(new Error(HttpStatusCode.UnprocessableEntity)
1417
{
1518
Title = reason != null
1619
? "Failed to deserialize request body: " + reason
1720
: "Failed to deserialize request body.",
18-
Detail = FormatDetails(details, requestBody, innerException)
1921
}, innerException)
2022
{
23+
_details = details;
24+
_requestBody = requestBody;
25+
26+
UpdateErrorDetail();
2127
}
2228

23-
private static string FormatDetails(string details, string requestBody, Exception innerException)
29+
private void UpdateErrorDetail()
2430
{
25-
string text = details ?? innerException?.Message;
31+
string text = _details ?? InnerException?.Message;
2632

27-
if (requestBody != null)
33+
if (_requestBody != null)
2834
{
2935
if (text != null)
3036
{
3137
text += " - ";
3238
}
3339

34-
text += "Request body: <<" + requestBody + ">>";
40+
text += "Request body: <<" + _requestBody + ">>";
3541
}
3642

37-
return text;
43+
Error.Detail = text;
44+
}
45+
46+
public void SetRequestBody(string requestBody)
47+
{
48+
_requestBody = requestBody;
49+
UpdateErrorDetail();
3850
}
3951
}
4052
}

src/JsonApiDotNetCore/Formatters/JsonApiReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ public async Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
4545
{
4646
model = _deserializer.Deserialize(body);
4747
}
48-
catch (InvalidRequestBodyException)
48+
catch (InvalidRequestBodyException exception)
4949
{
50+
exception.SetRequestBody(body);
5051
throw;
5152
}
5253
catch (Exception exception)

src/JsonApiDotNetCore/Services/DefaultResourceService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public virtual async Task UpdateRelationshipsAsync(TId id, string relationshipNa
236236

237237
protected virtual async Task<IEnumerable<TResource>> ApplyPageQueryAsync(IQueryable<TResource> entities)
238238
{
239-
if (_pageService.PageSize <= 0)
239+
if (_pageService.PageSize == 0)
240240
{
241241
_logger.LogDebug("Fetching complete result set.");
242242

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public async Task CreateResource_UnknownEntityType_Fails()
266266
Assert.Equal(HttpStatusCode.UnprocessableEntity, errorDocument.Errors[0].StatusCode);
267267
Assert.Equal("Failed to deserialize request body: Payload includes unknown resource type.", errorDocument.Errors[0].Title);
268268
Assert.StartsWith("The resource 'something' is not registered on the resource graph.", errorDocument.Errors[0].Detail);
269+
Assert.Contains("Request body: <<", errorDocument.Errors[0].Detail);
269270
}
270271

271272
[Fact]

0 commit comments

Comments
 (0)