CreateJsonResultAndETag( ) should return a consistent result.
See original GitHub issueWhat is wrong?
When an entity does not implement IEtag, given 2 different calls that produce the same collection, CreateJsonResultAndETag( ) function from Beef.AspNetCore.WebApi.WebApiActionBase class returns different ETag values when API is called with ExcludeFields and returns the same ETag when API is called without ExcludeFields.
What is expected?
EDIT:
Expected to return the same ETag for the collection when entity does not implement IETag and returns a calculated ETag when entity implements IETag.
or
WebApiGet<TResult, TColl, TEntity> should not call CreateJsonResultAndETag( ) and should not set ETag header when items from a Collection do not implement IETag.
How to reproduce:
codegen xml:
<Entity Name="Contact" Collection="true" CollectionResult="true" WebApiRoutePrefix="api/v1/contacts"
AutoImplement="Database" Implements="Changelog">
<Property Name="Id" Type="Guid" UniqueKey="true"/>
<Property Name="Name" Type="string" DataName="ContactTypeCode" />
<Operation Name="GetByArgs" OperationType="GetColl" PagingArgs="true">
<Parameter Name="search" Type="string"/>
</Operation>
</Entity>
unit test:
Assume the database has 2 contacts: “Mary Flower” and “Mary Smith”.
[Test]
public void A152_GetByArgs_CheckETagWithoutAddingPatient_WithExcludeFields()
{
var contactArgs = "Mary" ;
var pagingArgs = new Beef.Entities.PagingArgs { IsGetCount = true };
var webReqOptions = new Beef.WebApi.WebApiRequestOptions().Exclude("changeLog");
var originalEtag = AgentTester.Create<ContactAgent, ContactCollectionResult>()
.ExpectStatusCode(HttpStatusCode.OK)
.Run((a) => a.Agent.GetByArgsAsync(contactArgs, pagingArgs, webReqOptions)).Response.Headers.ETag;
/****
Update contact name to "Mary Anne"
(details hidden for brevity)
******/
var newEtag = AgentTester.Create<ContactAgent, ContactCollectionResult>()
.ExpectStatusCode(HttpStatusCode.OK)
.Run((a) => a.Agent.GetByArgsAsync(contactArgs, pagingArgs, webReqOptions)).Response.Headers.ETag;
//This test passes
Assert.AreNotEqual(originalEtag, newEtag, "ETag should not be the same after an update.");
}
[Test]
public void A153_GetByArgs_CheckETagWithoutAddingPatient_WithAllFields()
{
var contactArgs = "Mary" ;
var pagingArgs = new Beef.Entities.PagingArgs { IsGetCount = true };
var originalEtag = AgentTester.Create<ContactAgent, ContactCollectionResult>()
.ExpectStatusCode(HttpStatusCode.OK)
.Run((a) => a.Agent.GetByArgsAsync(contactArgs, pagingArgs)).Response.Headers.ETag;
/****
Update contact name to "Mary Anne"
(details hidden for brevity)
******/
var newEtag = AgentTester.Create<ContactAgent, ContactCollectionResult>()
.ExpectStatusCode(HttpStatusCode.OK)
.Run((a) => a.Agent.GetByArgsAsync(contactArgs, pagingArgs)).Response.Headers.ETag;
//This test fails
Assert.AreNotEqual(originalEtag, newEtag, "ETag should not be the same after an update.");
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
@leandrodotec and @edjo23, thanks. I have fixed the issue, and have gone down the
GetHashCode
route. A new version of theBeef.AspNetCore.WebApi
nuget package has been published. As an FYI, I am wanting to avoid doing any JSON serialization unless absolutely necessary and let ASP.NET Core framework handle. Eventually, I will make using the native or Newtonsoft serializer configurable, versus just Newtonsoft as it is now and this helps enable.Hi @chullybun, using GetHashCode makes sense.