Skip to content

Commit

Permalink
Merge pull request #383 from PHOENIXCONTACT/fix/proxy-null-reference
Browse files Browse the repository at this point in the history
Port #382 to release 8
  • Loading branch information
Toxantron authored Feb 20, 2024
2 parents 005aabd + ec301a7 commit 1ca1e9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
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 @@ -70,6 +70,8 @@ private void OnCapabilitiesChanged(object sender, ICapabilities e)
protected internal TResource Convert<TResource>(IResource instance)
where TResource : IResource
{
if (instance is null) return default;

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

Expand All @@ -79,6 +81,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 @@ -88,6 +92,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 @@ -98,6 +104,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 @@ -12,8 +12,12 @@ public interface IReferenceResource : IResource
{
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 @@ -71,6 +75,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 @@ -257,6 +257,8 @@ public void ReplaceWithProxy()
{
Id = 8,
Reference = ref1,
Reference2 = null,
EvenMoreReferences = null,
NonPublic = nonPub
};
instance.References = new ReferenceCollection<ISimpleResource>(instance,
Expand Down Expand Up @@ -302,6 +304,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
2 changes: 1 addition & 1 deletion src/Tests/Moryx.Tests/Collections/DelayQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void StoppedQueueThrowsException()
Assert.AreEqual(0, _times.Count);
}

[Test(Description = "")]
[Test(Description = ""), Explicit]
public void InterruptQueue()
{
// Arrange
Expand Down

0 comments on commit 1ca1e9d

Please sign in to comment.