-
Notifications
You must be signed in to change notification settings - Fork 8k
Description
Follow-up from #10654.
The problem is presumably related to the fact that the Newtonsoft.Json.Linq.JObject
Json.NET type implements IDynamicMetaDataProvider
, in which case PowerShell delegates binding to the target object.
It is unclear if the problem is specific to Json.NET and perhaps to be fixed there or whether it affects other libraries too.
The following issues are likely related:
- Working directly with certain .NET types, such as Newtonsoft.Json's, is broken: output formatting #10650
- Working directly with certain .NET types, such as Newtonsoft.Json's, is broken: implicit stringification #10652
- Types that implement IDynamicMetaObjectProvider do not have the standard hidden PSObject members such as .psobject and .pstypenames (intrinsic members) #10668
Quoting @SeeminglyScience from #10654 (comment) (lightly edited):
A fix is needed for sure, what's unclear is who needs to fix what. It seems bizarre to me that their convert binder wouldn't account for interfaces.
Maybe PowerShell should swallow the exception depending on where it happens. Maybe it should approach conversions for
IDynamicMetaDataProvider
more delicately, but imo that's kind of the point ofIDynamicMetaDataProvider
, especially in PowerShell.
My gut reaction is that the fix should be in Json.NET but I haven't looked closely enough at the PowerShell side to see if there's something better it could do there.
Note that the interface being cast to below is an explicit interface implementation in JObject
.
Steps to reproduce
using namespace System.Collections.Generic
using namespace Newtonsoft.Json.Linq
# Casting to [IList[JToken]] should allow *numeric* indexing and
# index 0 should return the first JProperty instance that makes up the JObject
([IList[JToken]] [JObject]::Parse('{"foo":1}'))[0] | Should -BeOfType ([JProperty])
Expected behavior
The test should succeed.
Actual behavior
The test fails:
Expected the value to have type [Newtonsoft.Json.Linq.JProperty] or any of its subtypes, but got $null with type $null.
Environment data
PowerShell Core 7.0.0-preview.4