Skip to content

Commit f3d5e45

Browse files
committed
fix: e2e test Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_Configured
1 parent 2c33407 commit f3d5e45

File tree

2 files changed

+30
-50
lines changed

2 files changed

+30
-50
lines changed

src/JsonApiDotNetCore/Serialization/Common/BaseDocumentParser.cs

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ protected IIdentifiable SetRelationships(IIdentifiable entity, Dictionary<string
9696
var entityProperties = entity.GetType().GetProperties();
9797
foreach (var attr in relationshipAttributes)
9898
{
99+
relationshipsValues.TryGetValue(attr.PublicRelationshipName, out RelationshipData relationshipData);
100+
101+
if (relationshipData == null || !relationshipData.HasData)
102+
continue;
103+
99104
if (attr is HasOneAttribute hasOne)
100-
SetHasOneRelationship(entity, entityProperties, (HasOneAttribute)attr, relationshipsValues);
105+
SetHasOneRelationship(entity, entityProperties, (HasOneAttribute)attr, relationshipData);
101106
else
102-
SetHasManyRelationship(entity, (HasManyAttribute)attr, relationshipsValues);
107+
SetHasManyRelationship(entity, (HasManyAttribute)attr, relationshipData);
103108

104109
}
105110
return entity;
@@ -152,16 +157,13 @@ private IIdentifiable ParseResourceObject(ResourceObject data)
152157
/// <param name="entity"></param>
153158
/// <param name="entityProperties"></param>
154159
/// <param name="attr"></param>
155-
/// <param name="relationships"></param>
160+
/// <param name="relationshipData"></param>
156161
/// <returns></returns>
157162
private object SetHasOneRelationship(IIdentifiable entity,
158163
PropertyInfo[] entityProperties,
159164
HasOneAttribute attr,
160-
Dictionary<string, RelationshipData> relationships)
165+
RelationshipData relationshipData)
161166
{
162-
if (relationships.TryGetValue(attr.PublicRelationshipName, out RelationshipData relationshipData) == false)
163-
return entity;
164-
165167
var rio = (ResourceIdentifierObject)relationshipData.Data;
166168
var relatedId = rio?.Id ?? null;
167169

@@ -173,13 +175,10 @@ private object SetHasOneRelationship(IIdentifiable entity,
173175
/// i.e. we're populating the relationship from the dependent side.
174176
SetForeignKey(entity, foreignKeyProperty, attr, relatedId);
175177

176-
177178
SetNavigation(entity, attr, relatedId);
178-
179-
180-
181-
// allow for additional processing of relationships as required for the
182-
// serializer class that implements this abstract class.
179+
180+
/// depending on if this base parser is used client-side or server-side,
181+
/// different additional processing per field needs to be executed.
183182
AfterProcessField(entity, attr, relationshipData);
184183

185184
return entity;
@@ -226,24 +225,18 @@ private void SetNavigation(IIdentifiable entity, HasOneAttribute attr, string re
226225
/// </summary>
227226
private object SetHasManyRelationship(IIdentifiable entity,
228227
HasManyAttribute attr,
229-
Dictionary<string, RelationshipData> relationships)
228+
RelationshipData relationshipData)
230229
{
231-
if (relationships.TryGetValue(attr.PublicRelationshipName, out RelationshipData relationshipData))
230+
var relatedResources = relationshipData.ManyData.Select(rio =>
232231
{
233-
if (!relationshipData.IsManyData)
234-
return entity;
232+
var relatedInstance = attr.DependentType.New<IIdentifiable>();
233+
relatedInstance.StringId = rio.Id;
234+
return relatedInstance;
235+
});
235236

236-
var relatedResources = relationshipData.ManyData.Select(rio =>
237-
{
238-
var relatedInstance = attr.DependentType.New<IIdentifiable>();
239-
relatedInstance.StringId = rio.Id;
240-
return relatedInstance;
241-
});
242-
243-
var convertedCollection = TypeHelper.ConvertCollection(relatedResources, attr.DependentType);
244-
attr.SetValue(entity, convertedCollection);
245-
AfterProcessField(entity, attr, relationshipData);
246-
}
237+
var convertedCollection = TypeHelper.ConvertCollection(relatedResources, attr.DependentType);
238+
attr.SetValue(entity, convertedCollection);
239+
AfterProcessField(entity, attr, relationshipData);
247240

248241
return entity;
249242
}

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public async Task Can_Create_Entity_With_Client_Defined_Id_If_Configured()
164164
Assert.Equal(clientDefinedId, deserializedBody.Id);
165165
}
166166

167-
168167
[Fact]
169168
public async Task Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_Configured()
170169
{
@@ -176,34 +175,22 @@ public async Task Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_
176175
var client = server.CreateClient();
177176

178177
var context = _fixture.GetService<AppDbContext>();
178+
context.RemoveRange(context.TodoItemCollections);
179+
await context.SaveChangesAsync();
179180

180-
var owner = new JsonApiDotNetCoreExample.Models.Person();
181+
var owner = new Person();
181182
context.People.Add(owner);
182183
await context.SaveChangesAsync();
183184

184185
var route = "/api/v1/todo-collections";
185186
var request = new HttpRequestMessage(httpMethod, route);
186187
var clientDefinedId = Guid.NewGuid();
187-
var content = new
188-
{
189-
data = new
190-
{
191-
type = "todo-collections",
192-
id = $"{clientDefinedId}",
193-
relationships = new
194-
{
195-
owner = new
196-
{
197-
data = new
198-
{
199-
type = "people",
200-
id = owner.Id.ToString()
201-
}
202-
}
203-
}
204-
}
205-
};
206-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
188+
189+
var serializer = _fixture.GetSerializer<TodoItemCollection>(tic => new { }, tic => new { tic.Owner });
190+
var todoItemCollection = new TodoItemCollection { Owner = owner, OwnerId = owner.Id, Id = clientDefinedId };
191+
var content = serializer.Serialize(todoItemCollection);
192+
193+
request.Content = new StringContent(content);
207194
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
208195

209196
// act

0 commit comments

Comments
 (0)