Skip to content

Empty key name converting to PSObject - #2820

Closed
Kitt Holland (kittholland) wants to merge 2 commits into
PowerShell:masterfrom
kittholland:EmptyKeyname
Closed

Kitt Holland (kittholland) wants to merge 2 commits into
PowerShell:masterfrom
kittholland:EmptyKeyname

Conversation

@kittholland

@kittholland Kitt Holland (kittholland) commented Dec 1, 2016 •

Copy link
Copy Markdown
Contributor

In issue #1755 we determined that you could not create a PSObject with a
key name of an empty string. This is supported in JSON and in
Hashtables.

Simple change was to lower evaluations of name field from
'IsNullOrEmpty' to '== null'.

Simple test to confirm is:
[pscustomobject]@{'' = 'Test'}

Simple JSON to test which results in ths same error:
'{"": "Test"}' | ConvertFrom-Json

After these changes, both of these cases work and I am able to
succesfully Convert project.lock.json from JSON and back to JSON with no
data loss.

There are many references to the areas of code I have updated and I do not believe I can foresee all consequences however I have ran the full test suite locally and have not found any indication these changes are harmful, while I have confirmed that it addresses the issue we are seeing.

I would also at least like to submit the above two cases as tests for this issue, but I am uncertain where those tests should go.

In issue PowerShell#1755 we determined that you could not create a PSObject with a
key name of an empty string. This is supported in JSON and in
Hashtables.

Simple change was to lower evaluations of name field from
'IsNullOrEmpty' to '== null'.

Simple test to confirm is:
[pscustomobject]@{'' = 'Test'}

Simple JSON to test which results in ths same error:
{
"": "Test"
}

After these changes, both of these cases work and I am able to
succesfully Convert project.lock.json from JSON and back to JSON with no
data loss.
This no longer checks for empty names.
@msftclas

Hi Kitt Holland (@kittholland), I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution!
You've already signed the contribution license agreement. Thanks!

The agreement was validated by Microsoft and real humans are currently evaluating your PR.

TTYL, MSBOT;

@lzybkr

Copy link
Copy Markdown
Contributor

I wonder if it makes more sense to "reserve" a property name for the JSON cmdlets to use instead of allowing the empty string as a property name.

@lzybkr Jason Shirk (lzybkr) self-assigned this Dec 1, 2016
@kittholland

Copy link
Copy Markdown
Contributor Author

I'm not sure how people feel about the valid hashtable @{'' = 'Test'} failing to convert to psobject as well. Not sure if that is a bug or intended. If we add a reserved property for JSON would we want to handle the hashtable conversion case as well?

@lzybkr Jason Shirk (lzybkr) added the Review - Committee The PR/Issue needs a review from the PowerShell Committee label Dec 1, 2016
@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee discussed this and agree we should support this for JSON (and investigate if YAML will be an issue when we support YAML), but not something we should support in the language for objects as there are situations where we would error today that would start working due to unintended circumstances.

Recommendation is see how JSON.Net is handling this case and use as a precedent

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Dec 8, 2016
@SteveL-MSFT

Copy link
Copy Markdown
Member

I experimented with Newtonsoft.Json and they don't have this problem as deserializing to a specific type would require that object to have a property with empty name or they deserialize to a Newtonsoft.Json.Linq.JProperty. It seems that for PowerShell to support this and not introduce the concept of an empty property name to the language, we may need to introduce a json type.

@kittholland

Copy link
Copy Markdown
Contributor Author

Can I close this PR? I don't think we will be implementing a solution with this type of approach.

@lzybkr

Copy link
Copy Markdown
Contributor

Because of the potential language issues raised, e.g. [pscustomobject]@{ "$Foo" = 42 }, I agree we should not take this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Committee-Reviewed PS-Committee has reviewed this and made a decision Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants