Skip to content

Commit edead43

Browse files
committed
PR feedback
1 parent 5a07e55 commit edead43

File tree

5 files changed

+26
-29
lines changed

5 files changed

+26
-29
lines changed

src/JsonApiDotNetCore/Builders/DocumentBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private Dictionary<string, object> GetMeta(IIdentifiable entity)
7575
if (entity is IHasMeta metaEntity)
7676
builder.Add(metaEntity.GetMeta(_jsonApiContext));
7777

78-
if (_jsonApiContext.Options.IncludeTotalRecordCount)
78+
if (_jsonApiContext.Options.IncludeTotalRecordCount && _jsonApiContext.PageManager.TotalRecords != null)
7979
builder.Add("total-records", _jsonApiContext.PageManager.TotalRecords);
8080

8181
if (_requestMeta != null)

src/JsonApiDotNetCore/Internal/PageManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace JsonApiDotNetCore.Internal
66
{
77
public class PageManager
88
{
9-
public int TotalRecords { get; set; }
9+
public int? TotalRecords { get; set; }
1010
public int PageSize { get; set; }
1111
public int DefaultPageSize { get; set; }
1212
public int CurrentPage { get; set; }
1313
public bool IsPaginated => PageSize > 0;
14-
public int TotalPages => (TotalRecords == 0) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords, PageSize));
14+
public int TotalPages => (TotalRecords == 0 || TotalRecords == null) ? -1 : (int)Math.Ceiling(decimal.Divide(TotalRecords.Value, PageSize));
1515

1616
public RootLinks GetPageLinks(LinkBuilder linkBuilder)
1717
{

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<VersionPrefix>2.3.0</VersionPrefix>
3+
<VersionPrefix>2.3.1</VersionPrefix>
44
<TargetFrameworks>$(NetStandardVersion)</TargetFrameworks>
55
<AssemblyName>JsonApiDotNetCore</AssemblyName>
66
<PackageId>JsonApiDotNetCore</PackageId>

src/JsonApiDotNetCore/Services/JsonApiContext.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ internal static bool PathIsRelationship(string requestPath)
8686
const char pathSegmentDelimiter = '/';
8787

8888
var span = requestPath.AsSpan();
89-
89+
9090
// we need to iterate over the string, from the end,
9191
// checking whether or not the 2nd to last path segment
9292
// is "relationships"
9393
// -2 is chosen in case the path ends with '/'
94-
for(var i = requestPath.Length - 2; i >= 0; i--)
94+
for (var i = requestPath.Length - 2; i >= 0; i--)
9595
{
9696
// if there are not enough characters left in the path to
9797
// contain "relationships"
98-
if(i < relationships.Length)
98+
if (i < relationships.Length)
9999
return false;
100100

101101
// we have found the first instance of '/'
102-
if(span[i] == pathSegmentDelimiter)
102+
if (span[i] == pathSegmentDelimiter)
103103
{
104104
// in the case of a "relationships" route, the next
105105
// path segment will be "relationships"
@@ -112,20 +112,18 @@ internal static bool PathIsRelationship(string requestPath)
112112

113113
return false;
114114
}
115-
115+
116116
private PageManager GetPageManager()
117117
{
118118
if (Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
119119
return new PageManager();
120120

121121
var query = QuerySet?.PageQuery ?? new PageQuery();
122-
var requestMethod = _httpContextAccessor.HttpContext.Request.Method;
123122

124123
return new PageManager
125124
{
126125
DefaultPageSize = Options.DefaultPageSize,
127126
CurrentPage = query.PageOffset,
128-
TotalRecords = (requestMethod == "POST" || requestMethod == "PATCH") ? 1 : 0,
129127
PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize
130128
};
131129
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/DocumentTests/Meta.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public async Task Total_Record_Count_Included()
4848
var response = await client.SendAsync(request);
4949
var responseBody = await response.Content.ReadAsStringAsync();
5050
var documents = JsonConvert.DeserializeObject<Documents>(responseBody);
51-
51+
5252
// assert
5353
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
5454
Assert.NotNull(documents.Meta);
5555
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
5656
}
5757

5858
[Fact]
59-
public async Task Total_Record_Count_Not_Included_When_None()
59+
public async Task Total_Record_Count_Included_When_None()
6060
{
6161
// arrange
6262
_context.TodoItems.RemoveRange(_context.TodoItems);
@@ -75,14 +75,15 @@ public async Task Total_Record_Count_Not_Included_When_None()
7575
var response = await client.SendAsync(request);
7676
var responseBody = await response.Content.ReadAsStringAsync();
7777
var documents = JsonConvert.DeserializeObject<Documents>(responseBody);
78-
78+
7979
// assert
8080
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
81-
Assert.Null(documents.Meta);
81+
Assert.NotNull(documents.Meta);
82+
Assert.Equal(0, (long)documents.Meta["total-records"]);
8283
}
8384

8485
[Fact]
85-
public async Task Total_Record_Count_Included_POST()
86+
public async Task Total_Record_Count_Not_Included_In_POST_Response()
8687
{
8788
// arrange
8889
_context.TodoItems.RemoveRange(_context.TodoItems);
@@ -116,15 +117,14 @@ public async Task Total_Record_Count_Included_POST()
116117
var response = await client.SendAsync(request);
117118
var responseBody = await response.Content.ReadAsStringAsync();
118119
var documents = JsonConvert.DeserializeObject<Document>(responseBody);
119-
120+
120121
// assert
121122
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
122-
Assert.NotNull(documents.Meta);
123-
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
123+
Assert.Null(documents.Meta);
124124
}
125125

126126
[Fact]
127-
public async Task Total_Record_Count_Included_PATCH()
127+
public async Task Total_Record_Count_Not_Included_In_PATCH_Response()
128128
{
129129
// arrange
130130
_context.TodoItems.RemoveRange(_context.TodoItems);
@@ -161,11 +161,10 @@ public async Task Total_Record_Count_Included_PATCH()
161161
var response = await client.SendAsync(request);
162162
var responseBody = await response.Content.ReadAsStringAsync();
163163
var documents = JsonConvert.DeserializeObject<Document>(responseBody);
164-
164+
165165
// assert
166166
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
167-
Assert.NotNull(documents.Meta);
168-
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
167+
Assert.Null(documents.Meta);
169168
}
170169

171170
[Fact]
@@ -187,26 +186,26 @@ public async Task EntityThatImplements_IHasMeta_Contains_MetaData()
187186
// act
188187
var response = await client.SendAsync(request);
189188
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
190-
189+
191190
// assert
192191
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
193192
Assert.NotNull(documents.Meta);
194193
Assert.NotNull(expectedMeta);
195194
Assert.NotEmpty(expectedMeta);
196-
197-
foreach(var hash in expectedMeta)
195+
196+
foreach (var hash in expectedMeta)
198197
{
199-
if(hash.Value is IList)
198+
if (hash.Value is IList)
200199
{
201200
var listValue = (IList)hash.Value;
202-
for(var i=0; i < listValue.Count; i++)
201+
for (var i = 0; i < listValue.Count; i++)
203202
Assert.Equal(listValue[i].ToString(), ((IList)documents.Meta[hash.Key])[i].ToString());
204203
}
205204
else
206205
{
207206
Assert.Equal(hash.Value, documents.Meta[hash.Key]);
208207
}
209-
}
208+
}
210209
}
211210
}
212211
}

0 commit comments

Comments
 (0)