Skip to content

Commit f719c59

Browse files
committed
fix: e2e test Can_Create_Entity_With_Client_Defined_Id_If_Configured
1 parent 060da88 commit f719c59

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

src/JsonApiDotNetCore/Serialization/Common/DocumentParser.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,6 @@ private object SetHasOneRelationship(IIdentifiable entity,
168168
// this does not make sense in the following case: if we're setting the dependent of a one-to-one relationship, IdentifiablePropertyName should be null.
169169
var foreignKeyProperty = entityProperties.FirstOrDefault(p => p.Name == attr.IdentifiablePropertyName);
170170

171-
//if (foreignKeyProperty == null)
172-
//{ /// there is no FK from the current entity pointing to the related object,
173-
// /// i.e. means we're populating the relationship from the principal side.
174-
// SetNavigation(entity, attr, relatedId);
175-
//}
176-
//else
177-
//{
178-
// /// there is a FK from the current entity pointing to the related object,
179-
// /// i.e. we're populating the relationship from the dependent side.
180-
// SetDependentSide(entity, foreignKeyProperty, attr, relatedId);
181-
//}
182-
183171
if (foreignKeyProperty != null)
184172
/// there is a FK from the current entity pointing to the related object,
185173
/// i.e. we're populating the relationship from the dependent side.

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
using System.Net.Http.Headers;
77
using System.Threading.Tasks;
88
using Bogus;
9+
using JsonApiDotNetCore.Builders;
10+
using JsonApiDotNetCore.Serialization.Client;
911
using JsonApiDotNetCoreExample;
1012
using JsonApiDotNetCoreExample.Data;
1113
using JsonApiDotNetCoreExample.Models;
14+
using JsonApiDotNetCoreExampleTests.Helpers.Models;
1215
using JsonApiDotNetCoreExampleTests.Startups;
1316
using Microsoft.AspNetCore.Hosting;
1417
using Microsoft.AspNetCore.TestHost;
@@ -130,6 +133,8 @@ public async Task Can_Create_Entity_With_Client_Defined_Id_If_Configured()
130133
{
131134
// arrange
132135
var context = _fixture.GetService<AppDbContext>();
136+
context.RemoveRange(context.TodoItems);
137+
await context.SaveChangesAsync();
133138
var builder = new WebHostBuilder()
134139
.UseStartup<ClientGeneratedIdsStartup>();
135140
var httpMethod = new HttpMethod("POST");
@@ -139,28 +144,20 @@ public async Task Can_Create_Entity_With_Client_Defined_Id_If_Configured()
139144
var request = new HttpRequestMessage(httpMethod, route);
140145
var todoItem = _todoItemFaker.Generate();
141146
const int clientDefinedId = 9999;
142-
var content = new
143-
{
144-
data = new
145-
{
146-
type = "todo-items",
147-
id = $"{clientDefinedId}",
148-
attributes = new
149-
{
150-
description = todoItem.Description,
151-
ordinal = todoItem.Ordinal,
152-
createdDate = DateTime.Now
153-
}
154-
}
155-
};
147+
var serializer = _fixture.GetSerializer<TodoItem>(ti => new { ti.CreatedDate, ti.Description, ti.Ordinal });
148+
todoItem.Id = clientDefinedId;
149+
var content = serializer.Serialize(todoItem);
156150

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

154+
var graph = new ResourceGraphBuilder().AddResource<TodoItemClient>("todo-items").Build();
155+
var deserializer = new ResponseDeserializer(graph);
156+
160157
// act
161158
var response = await client.SendAsync(request);
162159
var body = await response.Content.ReadAsStringAsync();
163-
var deserializedBody = _fixture.GetDeserializer().DeserializeSingle<TodoItem>(body).Data;
160+
var deserializedBody = deserializer.DeserializeSingle<TodoItemClient>(body).Data;
164161

165162
// assert
166163
Assert.Equal(HttpStatusCode.Created, response.StatusCode);

0 commit comments

Comments
 (0)