Skip to content

Commit

Permalink
Merge pull request #223 from PHOENIXCONTACT/fix/proxy-null-reference
Browse files Browse the repository at this point in the history
Fix NullReference exceptions in ResourceProxy
  • Loading branch information
Toxantron authored Feb 20, 2024
2 parents 9af4aca + ceeff28 commit 752897d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Moryx.Resources.Management/Resources/ResourceProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public override string ToString()
protected internal TResource Convert<TResource>(IResource instance)
where TResource : IResource
{
if (instance is null) return default;

return (TResource)_typeController.GetProxy((Resource)instance);
}

Expand All @@ -67,6 +69,8 @@ protected internal TResource Convert<TResource>(IResource instance)
protected internal TResource[] ConvertMany<TResource>(IEnumerable<IResource> instances)
where TResource : IResource
{
if (instances is null) return default;

return instances.Select(Convert<TResource>).ToArray();
}

Expand All @@ -76,6 +80,8 @@ protected internal TResource[] ConvertMany<TResource>(IEnumerable<IResource> ins
protected internal static TResource Extract<TResource>(IResource instance)
where TResource : IResource
{
if (instance is null) return default;

var proxy = (ResourceProxy)instance;
return (TResource)proxy.Target;
}
Expand All @@ -86,6 +92,8 @@ protected internal static TResource Extract<TResource>(IResource instance)
protected internal static TResource[] ExtractMany<TResource>(IEnumerable<IResource> instances)
where TResource : IResource
{
if (instances is null) return default;

return instances.Select(Extract<TResource>).ToArray();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ public interface IReferenceResource : IPublicResource
{
ISimpleResource Reference { get; set; }

DerivedResource Reference2 { get; set; }

IEnumerable<ISimpleResource> MoreReferences { get; }

IEnumerable<ISimpleResource> EvenMoreReferences { get; set; }

INonPublicResource NonPublic { get; }

ISimpleResource GetReference();
Expand Down Expand Up @@ -72,6 +76,8 @@ public ISimpleResource Reference

IEnumerable<ISimpleResource> IReferenceResource.MoreReferences => References;

public IEnumerable<ISimpleResource> EvenMoreReferences { get; set; }

public ISimpleResource GetReference()
{
return Reference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public void ReplaceWithProxy()
{
Id = 8,
Reference = ref1,
Reference2 = null,
EvenMoreReferences = null,
NonPublic = nonPub
};
instance.References = new ReferenceCollection<ISimpleResource>(instance,
Expand Down Expand Up @@ -265,6 +267,11 @@ public void ReplaceWithProxy()
Assert.AreEqual(instance.Reference, ref2);
Assert.AreEqual(instance.References.Count, 3);
Assert.AreEqual(instance.References.ElementAt(1), ref1);
// Make sure null references work
Assert.DoesNotThrow(() => _ = proxy.Reference2);
Assert.DoesNotThrow(() => proxy.Reference2 = null);
Assert.DoesNotThrow(() => _ = proxy.EvenMoreReferences);
Assert.DoesNotThrow(() => proxy.EvenMoreReferences = null);
}

}
Expand Down

0 comments on commit 752897d

Please sign in to comment.