Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mklement0 opened this issue Oct 2, 2019 · 0 comments

Comments

@mklement0
Copy link
Contributor

commented Oct 2, 2019

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
@mklement0 mklement0 changed the title Casting to an interface for indexer overload resolution is broken with the Json.NET types Casting to an interface for indexer overload resolution is broken with Json.NET types Oct 2, 2019
@iSazonov iSazonov added the Area-Engine label Oct 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.