GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix xml nesting bug in CustomSerializer.WriteMemberInfoCollection() by IISResetMe · Pull Request #8476 · PowerShell/PowerShell

Conversation

@IISResetMe

@IISResetMe Mathias R. Jessen (IISResetMe) commented Dec 16, 2018

Copy link
Copy Markdown
Collaborator

PR Summary

Fix #8308

WriteMemberInfoCollection() incorrectly nests sibling elements at depth==0
The reason seems to be that WriteMemberInfoCollection() calls WriteEndElement() at most once, even if WriteStartElement() has been called more than once.

This PR moves the WriteEndElement() call up immediately after the elements text value has been written.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge and is not Work in Progress.
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready.
  • User-facing changes
    • Not Applicable
    • OR
    • User-facing Documentation needed
      • Issue filed - Issue link:
  • Testing - New and feature
    • Not Applicable or can only be tested interactively
    • OR
    • Make sure you've added a new test if existing tests do not effectively test the code changed
      • Add [feature] if the change is significant or affects feature tests

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vexx32 reacted with thumbs up emoji vexx32 reacted with hooray emoji vexx32 reacted with heart emoji
All reactions
  • 👍 1 reaction
  • 🎉 1 reaction
  • ❤️ 1 reaction
Mathias R. Jessen (IISResetMe) added 4 commits December 9, 2018 22:21
@IISResetMe
Remove incorrect modification of depth parameter
56e07a0
Decrementing `depth` makes no sense here and causes nested PSObject instances with more than one property to generate invalid xml
@IISResetMe
Revert "Remove incorrect modification of depth parameter"
3e3a215
This reverts commit 56e07a0.
@IISResetMe
Fix PowerShell#8308
b8db532
WriteMemberInfoCollection() incorrectly nests sibling elements at depth==0
The reason seems to be that WriteMemberInfoCollection() calls WriteEndElement() at most once, even if `WriteStartElement()` has been called more than once
@IISResetMe
Add tests for with ConvertTo-Xml with nested PSObjects
8a2908c
@IISResetMe
Mathias R. Jessen (IISResetMe) requested review from James Truher (JamesWTruher) and Paul Higinbotham (PaulHigin) as code owners December 16, 2018 21:52
iSazonov
Ilya (iSazonov) reviewed Dec 17, 2018
View reviewed changes
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Xml.Tests.ps1 Resolved

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov @IISResetMe
Update test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo…
3cab291
…-Xml.Tests.ps1

Co-Authored-By: IISResetMe <[email protected]>
PaulHigin
Paul Higinbotham (PaulHigin) suggested changes Dec 17, 2018
View reviewed changes
Comment thread src/Microsoft.PowerShell.Commands.Utility/commands/utility/CustomSerialization.cs
@@ -691,7 +691,6 @@ private void WriteMemberInfoCollection(
{

@PaulHigin Paul Higinbotham (PaulHigin) Dec 17, 2018

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look right to me ... not sure what was originally intended. But can you also remove the depth and writeEnclosingememberSetElementTag parameters? They are not used within the method and it would help clean up the code by removing them (also please remove the xml comments for the parameters).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@IISResetMe Mathias R. Jessen (IISResetMe) Dec 17, 2018

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I renamed the first parameter to members as well

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Mathias R. Jessen (IISResetMe) added 2 commits December 17, 2018 22:20
@IISResetMe
Clean up WriteMemberInfoCollection()
39e7b20
Removed unused parameters `depth` and `writeEnclosingMemberSetElementTag`, renamed `me` to `members`
@IISResetMe
Merge branch 'patch/invalid-xml-CustomSerialization' of https://githu…
bd936da
…b.com/IISResetMe/PowerShell into patch/invalid-xml-CustomSerialization
PaulHigin
Paul Higinbotham (PaulHigin) approved these changes Dec 17, 2018
View reviewed changes

@PaulHigin Paul Higinbotham (PaulHigin) left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iSazonov

Ilya (iSazonov) commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator

The original code is obviously broken.
As for the depth, I am afraid that this fix is not entirely correct. I assume that recursion was originally meant to serialize members.
Michael Klement (@mklement0) What do you think?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@IISResetMe

Mathias R. Jessen (IISResetMe) commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator Author

Ilya (@iSazonov) I agree that depth limitations are not at all correctly imposed by the serializer right now, but that's a broader issue that should be addressed in a separate issue/PR. This is simply an attempt to not produce invalid XML by fixing an adjecent bug.

For the broader issue of depth limitations we'll need to address the fact that depth is currently ignored by container types as well.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator

Mathias R. Jessen (@IISResetMe) In the case I'd keep the method signature. And could you please open new issue about the wrong depth ignoring?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@IISResetMe

Mathias R. Jessen (IISResetMe) commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator Author

I'll revert the last commit, but plan on tackle the depth issue as part of #7290

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@IISResetMe
Revert WriteMemberInfoCollection cleanup commit
febeca3
@IISResetMe

Mathias R. Jessen (IISResetMe) commented Dec 18, 2018

Copy link
Copy Markdown
Collaborator Author

Ilya (@iSazonov) Reverted depth-related refactorings

iSazonov reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iSazonov
Ilya (iSazonov) approved these changes Dec 19, 2018
View reviewed changes
@iSazonov Ilya (iSazonov) self-assigned this Dec 19, 2018
@iSazonov

Ilya (iSazonov) commented Dec 19, 2018

Copy link
Copy Markdown
Collaborator

Mathias R. Jessen (@IISResetMe) Please add a empty commit with [Feature] in title before we merge..

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@IISResetMe
[Feature]Trigger tests
3ad3ef6
@iRon7

iRon7 commented Dec 20, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Please be aware that this issue shouldn't only concern the technical fix of #7290 but also the presumed compatibility with prior versions. Meaning: a lot of scripts in the field might break due to this change!
Refer to the discussion in case #8393 from Michael Klement (@mklement0) and StackOverflow Question "Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2" concerning the perception of the default -Depth (of 2) of the similar cmdlet: ConvertTo-Json.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Dec 20, 2018

Copy link
Copy Markdown
Collaborator

iRon7 In the PR we fix a bug in ConvertTo-Xml cmdlet but you reference a ConvertTo-Json - could you clarify that you mean?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7

iRon7 commented Dec 20, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

I am sorry, I guess my comment wasn't that clear. What I mean is, that appearently every user of the ConvertTo-Json cmdlets appears to trapped by the pitfall that the -Depthis limited by default to 2. The issues with ConvertTo-Xml are less than ConvertTo-Json which could be explained by usage but also by the concerned bug. Meaning that if the default depth now suddenly changes to 2, users might experience the same type of issues as with ConvertTo-Json. Besides, scripts that used to work might now suddenly break and have truncated property paths...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Dec 20, 2018

Copy link
Copy Markdown
Collaborator

iRon7 The PR doesn't change defaults. It only fix a bug. Defaults we'll change after PowerShell Committee review #7290.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@IISResetMe

Mathias R. Jessen (IISResetMe) commented Dec 20, 2018

Copy link
Copy Markdown
Collaborator Author

iRon7 one approach could be:

  • Fix depth enforcement
  • Set default depth = 5

Doesnt have to be 2 just because that's the case for the json cmdlet

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7

iRon7 commented Dec 20, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Mathias R. Jessen (@IISResetMe). Yes, changing the default depth might work but could cause a performance impact for existing scripts were the default depth (of 2) did work.
E.g.:

Get-Service | ConvertTo-Xml

takes ~700ms, where:

Get-Service | ConvertTo-Xml -Depth 5

takes 6.5 minutes...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@PaulHigin

Paul Higinbotham (PaulHigin) commented Dec 20, 2018

Copy link
Copy Markdown
Contributor

Mathias R. Jessen (@IISResetMe)
I still feel the unused method parameters should be removed. Yes, they should eventually be implemented, but since they are not now, they should be removed. It is Ok to add a TODO comment and create an Issue to add the functionality later.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Dec 21, 2018

Copy link
Copy Markdown
Collaborator

Paul Higinbotham (@PaulHigin) A code you reference is not new code so the change is not related the PR. I believe we get better code with fixing #7290. Mathias R. Jessen (@IISResetMe) Are ready to fix #7290?

takes 6.5 minutes...

iRon7 Please open new performance issue so we can investigate this problem.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iRon7

iRon7 commented Dec 21, 2018

Copy link
Copy Markdown

Ilya (@iSazonov), I do not have a performance issue (...yet). Just some concerns that the fix (even it is technically correct) might break a few scripts in the field and if you try to prevent that with a higher default -Depth, you might introduce a few performance complains with existing scripts.
Anyway, it is totally up to you what to decide (any direction in this delimema has its pro's and cons).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Dec 21, 2018

Copy link
Copy Markdown
Collaborator

Steve Lee (@SteveL-MSFT) We need an additional review from MSFT experts and get a conclusion about iRon7's concerns - can we merge before PowerShell Committee review #7290?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Dec 21, 2018

Copy link
Copy Markdown
Member

I think the scope of these changes is ok to accept without waiting on conclusion of #7290

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 22, 2018
@iSazonov
Ilya (iSazonov) merged commit 0e971a4 into PowerShell:master Dec 22, 2018
@iRon7

iRon7 commented Jan 6, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

I just found a similar issue with a PSCustomObject and a recurring reference:

$Parent = [PSCustomObject]@{
	Name = "Parent"
}
$Child = [PSCustomObject]@{
	Name = "Child"
}
$Parent | Add-Member Child $Child
$Child | Add-Member Parent $Parent

But the error message is completely different from #7290

PS C:\> $Parent | ConvertTo-Xml

ConvertTo-Xml : Unexpected end of file has occurred. The following elements are not closed: Objects. Line 9, position 12.
At line:1 char:11

  • $Parent | ConvertTo-Xml
  •       ~~~~~~~~~~~~~
    
    • CategoryInfo : NotSpecified: (:) [ConvertTo-Xml], XmlException
    • FullyQualifiedErrorId : System.Xml.XmlException,Microsoft.PowerShell.Commands.ConvertToXmlCommand

I am not sure whether this is covered in the upcoming fix and tests
Anyways, let me know if you like me to open a new bug report for this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@iSazonov

Ilya (iSazonov) commented Jan 6, 2019

Copy link
Copy Markdown
Collaborator

iRon7 You can download a latest night build from main page of the repo and test.
For your example I get:

$Parent | ConvertTo-Xml

xml                            Objects
---                            -------
version="1.0" encoding="utf-8" Objects
iRon7 and IISResetMe reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@IISResetMe @iSazonov
Fix xml nesting bug in CustomSerializer.WriteMemberInfoCollection() (P…
778f60f
…owerShell#8476)

WriteMemberInfoCollection() calls WriteEndElement() at most once, even if WriteStartElement() has been called more than once. This commit moves the WriteEndElement() call up immediately after the elements text value has been written.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iSazonov Ilya (iSazonov) iSazonov approved these changes
@JamesWTruher James Truher (JamesWTruher) Awaiting requested review from JamesWTruher
+1 more reviewer
@PaulHigin Paul Higinbotham (PaulHigin) PaulHigin approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@iSazonov Ilya (iSazonov)

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

ConvertTo-Xml fails with nested custom objects: omits closing </Objects> tag

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@IISResetMe @iSazonov @iRon7 @PaulHigin @SteveL-MSFT
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.