Skip to content

Casting to an interface for indexer overload resolution is broken with Json.NET types #10688

@mklement0

Description

@mklement0

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:

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 of IDynamicMetaDataProvider, 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions