Skip to content

fix: unwrap PSObject-wrapped values when serializing Invoke-MgGraphRe… - #3717

Open
Ramses Sanchez-Hernandez (ramsessanchez) wants to merge 2 commits into
mainfrom
ramsess/unwrap-object
Open

fix: unwrap PSObject-wrapped values when serializing Invoke-MgGraphRe…#3717
Ramses Sanchez-Hernandez (ramsessanchez) wants to merge 2 commits into
mainfrom
ramsess/unwrap-object

Conversation

@ramsessanchez

Copy link
Copy Markdown
Contributor

…quest dictionary bodies (#3658)

  • fix: unwrap PSObject-wrapped values when serializing Invoke-MgGraphRequest dictionary bodies

Invoke-MgGraphRequest -Body serializes IDictionary bodies with Newtonsoft.Json. Values that came from the PowerShell pipeline (e.g. bare $_ in ForEach-Object) are PSObject wrappers, so Newtonsoft reflected over PowerShell adapted members (such as the Chars indexed property on strings) and failed with a self-referencing loop error before the request was sent.

Add a PSObjectJsonConverter that serializes the underlying BaseObject of PSObject-wrapped values at any depth, and projects pure PSCustomObjects into JSON objects, and pass it at the single JsonConvert.SerializeObject call site for dictionary bodies.

Also pin the test project's Microsoft.PowerShell.SDK reference to the latest 7.4.x release: 7.5.x targets net9.0 and contributes no assemblies to the net8.0 test build, which silently left the PowerShellStandard.Library stub (whose APIs return null) as the runtime System.Management.Automation, making PSObject-dependent tests impossible.

Fixes #3654

  • Add newline at end of PSObjectJsonConverter.cs

add space to kick off CI




Fixes #

Changes proposed in this pull request

Other links

…quest dictionary bodies (#3658)

* fix: unwrap PSObject-wrapped values when serializing Invoke-MgGraphRequest dictionary bodies

Invoke-MgGraphRequest -Body serializes IDictionary bodies with
Newtonsoft.Json. Values that came from the PowerShell pipeline (e.g.
bare $_ in ForEach-Object) are PSObject wrappers, so Newtonsoft
reflected over PowerShell adapted members (such as the Chars indexed
property on strings) and failed with a self-referencing loop error
before the request was sent.

Add a PSObjectJsonConverter that serializes the underlying BaseObject
of PSObject-wrapped values at any depth, and projects pure
PSCustomObjects into JSON objects, and pass it at the single
JsonConvert.SerializeObject call site for dictionary bodies.

Also pin the test project's Microsoft.PowerShell.SDK reference to the
latest 7.4.x release: 7.5.x targets net9.0 and contributes no
assemblies to the net8.0 test build, which silently left the
PowerShellStandard.Library stub (whose APIs return null) as the
runtime System.Management.Automation, making PSObject-dependent tests
impossible.

Fixes #3654

* Add newline at end of PSObjectJsonConverter.cs

add space to kick off CI

---------

Co-authored-by: Virgil <Virgil@example.com>
Co-authored-by: Ramses Sanchez-Hernandez <63934382+ramsessanchez@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invoke-MgGraphRequest -Body serialization fails on PSObject-wrapped pipeline strings

2 participants