Skip to content

Commit 958cc99

Browse files
author
Bart Koelman
committed
Fixed: serializer issue where tests influence each other (they fail together, but succeed individually)
1 parent 57e07f9 commit 958cc99

File tree

7 files changed

+24
-75
lines changed

7 files changed

+24
-75
lines changed

src/JsonApiDotNetCore/Models/Annotation/HasManyAttribute.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,5 @@ public HasManyAttribute(string publicName = null, Link relationshipLinks = Link.
3030
{
3131
InverseNavigation = inverseNavigationProperty;
3232
}
33-
34-
/// <summary>
35-
/// Gets the value of the navigation property, defined by the relationshipName,
36-
/// on the provided instance.
37-
/// </summary>
38-
public override object GetValue(object entity)
39-
{
40-
return entity?.GetType()
41-
.GetProperty(PropertyInfo.Name)?
42-
.GetValue(entity);
43-
}
44-
45-
/// <summary>
46-
/// Sets the value of the property identified by this attribute
47-
/// </summary>
48-
/// <param name="entity">The target object</param>
49-
/// <param name="newValue">The new property value</param>
50-
public override void SetValue(object entity, object newValue)
51-
{
52-
var propertyInfo = entity
53-
.GetType()
54-
.GetProperty(PropertyInfo.Name);
55-
56-
propertyInfo.SetValue(entity, newValue);
57-
}
5833
}
5934
}

src/JsonApiDotNetCore/Models/Annotation/HasManyThroughAttribute.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ public HasManyThroughAttribute(string publicName, string internalThroughName, Li
7070
}
7171

7272
/// <summary>
73-
/// Traverses the through the provided entity and returns the
73+
/// Traverses through the provided entity and returns the
7474
/// value of the relationship on the other side of a join entity
7575
/// (e.g. Articles.ArticleTags.Tag).
7676
/// </summary>
7777
public override object GetValue(object entity)
7878
{
7979
var throughNavigationProperty = entity.GetType()
8080
.GetProperties()
81-
.SingleOrDefault(p => p.Name == InternalThroughName);
81+
.Single(p => p.Name == InternalThroughName);
8282

8383
var throughEntities = throughNavigationProperty.GetValue(entity);
8484

@@ -88,17 +88,13 @@ public override object GetValue(object entity)
8888

8989
// the right entities are included on the navigation/through entities. Extract and return them.
9090
var rightEntities = new List<IIdentifiable>();
91-
foreach (var rightEntity in (IList)throughEntities)
91+
foreach (var rightEntity in (IEnumerable)throughEntities)
9292
rightEntities.Add((IIdentifiable)RightProperty.GetValue(rightEntity));
9393

9494
return rightEntities.Cast(RightType);
9595
}
9696

97-
/// <summary>
98-
/// Sets the value of the property identified by this attribute
99-
/// </summary>
100-
/// <param name="entity">The target object</param>
101-
/// <param name="newValue">The new property value</param>
97+
/// <inheritdoc />
10298
public override void SetValue(object entity, object newValue)
10399
{
104100
var propertyInfo = entity

src/JsonApiDotNetCore/Models/Annotation/HasOneAttribute.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ public HasOneAttribute(string publicName = null, Link links = Link.NotConfigured
3737
InverseNavigation = inverseNavigationProperty;
3838
}
3939

40-
41-
public override object GetValue(object entity)
42-
{
43-
return entity?.GetType()
44-
.GetProperty(PropertyInfo.Name)?
45-
.GetValue(entity);
46-
}
47-
4840
private readonly string _explicitIdentifiablePropertyName;
4941

5042
/// <summary>
@@ -54,11 +46,7 @@ public override object GetValue(object entity)
5446
? JsonApiOptions.RelatedIdMapper.GetRelatedIdPropertyName(PropertyInfo.Name)
5547
: _explicitIdentifiablePropertyName;
5648

57-
/// <summary>
58-
/// Sets the value of the property identified by this attribute
59-
/// </summary>
60-
/// <param name="entity">The target object</param>
61-
/// <param name="newValue">The new property value</param>
49+
/// <inheritdoc />
6250
public override void SetValue(object entity, object newValue)
6351
{
6452
string propertyName = PropertyInfo.Name;

src/JsonApiDotNetCore/Models/Annotation/RelationshipAttribute.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,21 @@ protected RelationshipAttribute(string publicName, Link relationshipLinks, bool
5151
public Link RelationshipLinks { get; }
5252
public bool CanInclude { get; }
5353

54-
public abstract void SetValue(object entity, object newValue);
54+
/// <summary>
55+
/// Gets the value of the resource property this attributes was declared on.
56+
/// </summary>
57+
public virtual object GetValue(object entity)
58+
{
59+
return PropertyInfo.GetValue(entity);
60+
}
5561

56-
public abstract object GetValue(object entity);
62+
/// <summary>
63+
/// Sets the value of the resource property this attributes was declared on.
64+
/// </summary>
65+
public virtual void SetValue(object entity, object newValue)
66+
{
67+
PropertyInfo.SetValue(entity, newValue);
68+
}
5769

5870
public override string ToString()
5971
{

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/EndToEndTest.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq.Expressions;
34
using System.Net;
45
using System.Net.Http;
@@ -63,14 +64,8 @@ protected IRequestSerializer GetSerializer<TResource>(Expression<Func<TResource,
6364
{
6465
var serializer = GetService<IRequestSerializer>();
6566
var graph = GetService<IResourceGraph>();
66-
if (attributes != null)
67-
{
68-
serializer.AttributesToSerialize = graph.GetAttributes(attributes);
69-
}
70-
if (relationships != null)
71-
{
72-
serializer.RelationshipsToSerialize = graph.GetRelationships(relationships);
73-
}
67+
serializer.AttributesToSerialize = attributes != null ? graph.GetAttributes(attributes) : null;
68+
serializer.RelationshipsToSerialize = relationships != null ? graph.GetRelationships(relationships) : null;
7469
return serializer;
7570
}
7671

test/JsonApiDotNetCoreExampleTests/Acceptance/TestFixture.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,12 @@ public TestFixture()
3535
public HttpClient Client { get; set; }
3636
public AppDbContext Context { get; private set; }
3737

38-
3938
public IRequestSerializer GetSerializer<TResource>(Expression<Func<TResource, dynamic>> attributes = null, Expression<Func<TResource, dynamic>> relationships = null) where TResource : class, IIdentifiable
4039
{
4140
var serializer = GetService<IRequestSerializer>();
4241
var graph = GetService<IResourceGraph>();
43-
if (attributes != null)
44-
serializer.AttributesToSerialize = graph.GetAttributes(attributes);
45-
if (relationships != null)
46-
serializer.RelationshipsToSerialize = graph.GetRelationships(relationships);
42+
serializer.AttributesToSerialize = attributes != null ? graph.GetAttributes(attributes) : null;
43+
serializer.RelationshipsToSerialize = relationships != null ? graph.GetRelationships(relationships) : null;
4744
return serializer;
4845
}
4946

test/NoEntityFrameworkTests/TestFixture.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using JsonApiDotNetCore.Builders;
2-
using JsonApiDotNetCore.Internal.Contracts;
3-
using JsonApiDotNetCore.Models;
42
using JsonApiDotNetCore.Serialization.Client;
53
using Microsoft.AspNetCore.Hosting;
64
using Microsoft.AspNetCore.TestHost;
75
using NoEntityFrameworkExample.Data;
86
using NoEntityFrameworkExample.Models;
97
using System;
10-
using System.Linq.Expressions;
118
using JsonApiDotNetCore.Configuration;
129
using Microsoft.Extensions.Logging.Abstractions;
1310
using NoEntityFrameworkExample;
@@ -28,17 +25,6 @@ public TestFixture()
2825
_services = Server.Host.Services;
2926
}
3027

31-
public IRequestSerializer GetSerializer<TResource>(Expression<Func<TResource, dynamic>> attributes = null, Expression<Func<TResource, dynamic>> relationships = null) where TResource : class, IIdentifiable
32-
{
33-
var serializer = GetService<IRequestSerializer>();
34-
var graph = GetService<IResourceGraph>();
35-
if (attributes != null)
36-
serializer.AttributesToSerialize = graph.GetAttributes(attributes);
37-
if (relationships != null)
38-
serializer.RelationshipsToSerialize = graph.GetRelationships(relationships);
39-
return serializer;
40-
}
41-
4228
public IResponseDeserializer GetDeserializer()
4329
{
4430
var options = GetService<IJsonApiOptions>();

0 commit comments

Comments
 (0)