Skip to content

Fixing issue where total-records was 0 on POST and PATCH calls. #308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ private PageManager GetPageManager()
return new PageManager();

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

return new PageManager
{
DefaultPageSize = Options.DefaultPageSize,
CurrentPage = query.PageOffset,
TotalRecords = (requestMethod == "POST" || requestMethod == "PATCH") ? 1 : 0,
Copy link
Contributor

@jaredcnance jaredcnance Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this changes the semantics of TotalRecords a bit. For GET requests, TotalRecords indicates the number of records in the database and not the number of records in the response, this would change the meaning of the field for POST and PATCH requests.

Do you find this field useful for create/update requests? I wonder if it should simply be excluded.

PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCoreExample;
Expand Down Expand Up @@ -54,6 +55,119 @@ public async Task Total_Record_Count_Included()
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
}

[Fact]
public async Task Total_Record_Count_Not_Included_When_None()
{
// arrange
_context.TodoItems.RemoveRange(_context.TodoItems);
_context.SaveChanges();
var builder = new WebHostBuilder()
.UseStartup<MetaStartup>();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Documents>(responseBody);

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Null(documents.Meta);
}

[Fact]
public async Task Total_Record_Count_Included_POST()
{
// arrange
_context.TodoItems.RemoveRange(_context.TodoItems);
_context.SaveChanges();
var expectedCount = 1;
var builder = new WebHostBuilder()
.UseStartup<MetaStartup>();

var httpMethod = new HttpMethod("POST");
var route = $"/api/v1/todo-items";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);
var content = new
{
data = new
{
type = "todo-items",
attributes = new
{
description = "New Description",
}
}
};

request.Content = new StringContent(JsonConvert.SerializeObject(content));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

// act
var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Document>(responseBody);

// assert
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
Assert.NotNull(documents.Meta);
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
}

[Fact]
public async Task Total_Record_Count_Included_PATCH()
{
// arrange
_context.TodoItems.RemoveRange(_context.TodoItems);
TodoItem todoItem = new TodoItem();
_context.TodoItems.Add(todoItem);
_context.SaveChanges();
var expectedCount = 1;
var builder = new WebHostBuilder()
.UseStartup<MetaStartup>();

var httpMethod = new HttpMethod("PATCH");
var route = $"/api/v1/todo-items/{todoItem.Id}";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);
var content = new
{
data = new
{
type = "todo-items",
id = todoItem.Id,
attributes = new
{
description = "New Description",
}
}
};

request.Content = new StringContent(JsonConvert.SerializeObject(content));
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");

// act
var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
var documents = JsonConvert.DeserializeObject<Document>(responseBody);

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(documents.Meta);
Assert.Equal((long)expectedCount, (long)documents.Meta["total-records"]);
}

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