Skip to content

Commit 8fe35df

Browse files
author
Bart Koelman
committed
Even more fixes for resources with parameterized constructors
1 parent 606f627 commit 8fe35df

File tree

7 files changed

+30
-13
lines changed

7 files changed

+30
-13
lines changed

src/Examples/JsonApiDotNetCoreExample/Models/Tag.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.ComponentModel.DataAnnotations;
23
using JsonApiDotNetCore.Models;
4+
using JsonApiDotNetCoreExample.Data;
35

46
namespace JsonApiDotNetCoreExample.Models
57
{
@@ -8,5 +10,10 @@ public class Tag : Identifiable
810
[Attr]
911
[RegularExpression(@"^\W$")]
1012
public string Name { get; set; }
13+
14+
public Tag(AppDbContext appDbContext)
15+
{
16+
if (appDbContext == null) throw new ArgumentNullException(nameof(appDbContext));
17+
}
1118
}
1219
}

src/JsonApiDotNetCore/Serialization/Client/ResponseDeserializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected override void AfterProcessField(IIdentifiable entity, IResourceField f
8080
/// </summary>
8181
private IIdentifiable ParseIncludedRelationship(RelationshipAttribute relationshipAttr, ResourceIdentifierObject relatedResourceIdentifier)
8282
{
83-
var relatedInstance = (IIdentifiable)TypeHelper.CreateInstance(relationshipAttr.RightType);
83+
var relatedInstance = (IIdentifiable)_resourceFactory.CreateInstance(relationshipAttr.RightType);
8484
relatedInstance.StringId = relatedResourceIdentifier.Id;
8585

8686
var includedResource = GetLinkedResource(relatedResourceIdentifier);

src/JsonApiDotNetCore/Serialization/Common/BaseDocumentParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private void SetHasManyRelationship(
233233
{ // if the relationship is set to null, no need to set the navigation property to null: this is the default value.
234234
var relatedResources = relationshipData.ManyData.Select(rio =>
235235
{
236-
var relatedInstance = (IIdentifiable)TypeHelper.CreateInstance(attr.RightType);
236+
var relatedInstance = (IIdentifiable)_resourceFactory.CreateInstance(attr.RightType);
237237
relatedInstance.StringId = rio.Id;
238238
return relatedInstance;
239239
});

test/JsonApiDotNetCoreExampleTests/Acceptance/ManyToManyTests.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,21 @@ namespace JsonApiDotNetCoreExampleTests.Acceptance
2121
[Collection("WebHostCollection")]
2222
public sealed class ManyToManyTests
2323
{
24-
private static readonly Faker<Article> _articleFaker = new Faker<Article>()
24+
private readonly Faker<Article> _articleFaker = new Faker<Article>()
2525
.RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10))
2626
.RuleFor(a => a.Author, f => new Author());
2727

28-
private static readonly Faker<Tag> _tagFaker = new Faker<Tag>().RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
28+
private readonly Faker<Tag> _tagFaker;
2929

3030
private readonly TestFixture<TestStartup> _fixture;
31+
3132
public ManyToManyTests(TestFixture<TestStartup> fixture)
3233
{
3334
_fixture = fixture;
35+
36+
_tagFaker = new Faker<Tag>()
37+
.CustomInstantiator(f => new Tag(_fixture.GetService<AppDbContext>()))
38+
.RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
3439
}
3540

3641
[Fact]

test/JsonApiDotNetCoreExampleTests/Acceptance/ModelStateValidationTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ModelStateValidationTests(StandardApplicationFactory factory)
2424
public async Task When_posting_tag_with_invalid_name_it_must_fail()
2525
{
2626
// Arrange
27-
var tag = new Tag
27+
var tag = new Tag(_dbContext)
2828
{
2929
Name = "!@#$%^&*().-"
3030
};
@@ -60,7 +60,7 @@ public async Task When_posting_tag_with_invalid_name_it_must_fail()
6060
public async Task When_posting_tag_with_invalid_name_without_model_state_validation_it_must_succeed()
6161
{
6262
// Arrange
63-
var tag = new Tag
63+
var tag = new Tag(_dbContext)
6464
{
6565
Name = "!@#$%^&*().-"
6666
};
@@ -88,7 +88,7 @@ public async Task When_posting_tag_with_invalid_name_without_model_state_validat
8888
public async Task When_patching_tag_with_invalid_name_it_must_fail()
8989
{
9090
// Arrange
91-
var existingTag = new Tag
91+
var existingTag = new Tag(_dbContext)
9292
{
9393
Name = "Technology"
9494
};
@@ -97,7 +97,7 @@ public async Task When_patching_tag_with_invalid_name_it_must_fail()
9797
context.Tags.Add(existingTag);
9898
context.SaveChanges();
9999

100-
var updatedTag = new Tag
100+
var updatedTag = new Tag(_dbContext)
101101
{
102102
Id = existingTag.Id,
103103
Name = "!@#$%^&*().-"
@@ -134,7 +134,7 @@ public async Task When_patching_tag_with_invalid_name_it_must_fail()
134134
public async Task When_patching_tag_with_invalid_name_without_model_state_validation_it_must_succeed()
135135
{
136136
// Arrange
137-
var existingTag = new Tag
137+
var existingTag = new Tag(_dbContext)
138138
{
139139
Name = "Technology"
140140
};
@@ -143,7 +143,7 @@ public async Task When_patching_tag_with_invalid_name_without_model_state_valida
143143
context.Tags.Add(existingTag);
144144
context.SaveChanges();
145145

146-
var updatedTag = new Tag
146+
var updatedTag = new Tag(_dbContext)
147147
{
148148
Id = existingTag.Id,
149149
Name = "!@#$%^&*().-"

test/JsonApiDotNetCoreExampleTests/Acceptance/ResourceDefinitions/ResourceDefinitionTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public sealed class ResourceDefinitionTests
2626
private readonly Faker<User> _userFaker;
2727
private readonly Faker<TodoItem> _todoItemFaker;
2828
private readonly Faker<Person> _personFaker;
29-
private static readonly Faker<Article> _articleFaker = new Faker<Article>()
29+
private readonly Faker<Article> _articleFaker = new Faker<Article>()
3030
.RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10))
3131
.RuleFor(a => a.Author, f => new Author());
3232

33-
private static readonly Faker<Tag> _tagFaker = new Faker<Tag>().RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
33+
private readonly Faker<Tag> _tagFaker;
3434

3535
public ResourceDefinitionTests(TestFixture<TestStartup> fixture)
3636
{
@@ -47,6 +47,9 @@ public ResourceDefinitionTests(TestFixture<TestStartup> fixture)
4747
_personFaker = new Faker<Person>()
4848
.RuleFor(p => p.FirstName, f => f.Name.FirstName())
4949
.RuleFor(p => p.LastName, f => f.Name.LastName());
50+
_tagFaker = new Faker<Tag>()
51+
.CustomInstantiator(f => new Tag(_context))
52+
.RuleFor(a => a.Name, f => f.Random.AlphaNumeric(10));
5053
}
5154

5255
[Fact]

test/UnitTests/ResourceHooks/ResourceHooksTestsSetup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public HooksDummyData()
5757
_articleFaker = new Faker<Article>().Rules((f, i) => i.Id = f.UniqueIndex + 1);
5858
_articleTagFaker = new Faker<ArticleTag>().CustomInstantiator(f => new ArticleTag(appDbContext));
5959
_identifiableArticleTagFaker = new Faker<IdentifiableArticleTag>().Rules((f, i) => i.Id = f.UniqueIndex + 1);
60-
_tagFaker = new Faker<Tag>().Rules((f, i) => i.Id = f.UniqueIndex + 1);
60+
_tagFaker = new Faker<Tag>()
61+
.CustomInstantiator(f => new Tag(appDbContext))
62+
.Rules((f, i) => i.Id = f.UniqueIndex + 1);
6163

6264
_passportFaker = new Faker<Passport>()
6365
.CustomInstantiator(f => new Passport(appDbContext))

0 commit comments

Comments
 (0)