Repository navigation
Closed
Labels
Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Cmdletsgeneral cmdlet issues
Description
Steps to reproduce
You will need to generate an API token and grant the notifications privilege.
PS /Users/greg> $apitoken = "yourapitoken"
PS /Users/greg> $headers = @{ Authorization = "token $apitoken" }
PS /Users/greg> $resp = Invoke-RestMethod -Uri https://api.github.com/notifications -Headers $headers
PS /Users/greg> $resp -eq $null
PS /Users/greg>What is returned is an Object.
PS /Users/greg> $resp -is [System.Object]
True
PS /Users/greg> Should be no different than this.
PS /Users/greg> $obj = New-Object System.Object
PS /Users/greg> $obj -eq $null
False
PS /Users/greg> Expected behavior
PS /Users/greg> $resp -eq $null
True
PS /Users/greg>Actual behavior
PS /Users/greg> $resp -eq $null
PS /Users/greg>Environment data
Name Value
---- -----
PSVersion 6.0.0-beta.9
PSEdition Core
GitCommitId v6.0.0-beta.9
OS Darwin 17.2.0 Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/R...
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0This occurs for Windows PowerShell versions too.
Metadata
Metadata
Assignees
Labels
Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Cmdletsgeneral cmdlet issues
Activity
SteveL-MSFT commentedon Nov 3, 2017
The repro that you provided doesn't actually work as I get an error
Requires authentication. Is what you're expecting is that if the response body is empty, then the output should be $null?thezim commentedon Nov 3, 2017
Steve Lee (@SteveL-MSFT) repro updated with the missing piece.
markekraus commentedon Nov 3, 2017
Greg Zimmerman (@thezim) do you have what the raw response is for that? Is it
Noneor{}or a 0-byte body?markekraus commentedon Nov 3, 2017
actually, I was able to do this after clearing all the notifications. it is returning
[]. for me, this is returning aSystem.Object[]. which happens to have no elements. I think that is a fair representation of an empty JSON array.is not the same as
thezim commentedon Nov 3, 2017
Mark Kraus (@markekraus) I agree that it is a fair representation but it is not very intuitive. Most cmdlets follow the null model. Should Get-ChildItem return
System.Object[]? The underlying data type (JSON) shouldn't matter since it actually returns aObject.markekraus commentedon Nov 3, 2017
The underlying JSON type does matter, actually. If they API says it returns an array that is empty when there are no items, then an empty array is expected. If we return
$nullinstead of an empty array, we break parity with the API. If the API wanted to return$nullit would return the JSON equivalentnull. This is true of top level and nested items too. the following are equivalent :{ "NestedArray": [] }just as these two are equivalent:
{ "NestedArray": null }Even in PowerShell, empty arrays are not null:
result:
False.There is a difference between object serialization and cmdlets which return collections of items. If you used
-OutVariableinstead of an assignment, you would get an emptyArrayListwhich isn't null either:Result:
In that instance the API is promising to return an
ArrayListand does, even though it is empty and it is not evaluated as$null.markekraus commentedon Nov 3, 2017
Though, I do see that
Invoke-RestMethodis not treatingnullby itself as$nullResult:
FalseThat should arguably be true. because this does work:
Result:
Truethezim commentedon Nov 3, 2017
Given deserialization logic
irm $urlshould not be null. If you make it that way you are breaking the API as well.markekraus commentedon Nov 3, 2017
Greg Zimmerman (@thezim)
nullin JSON is serialized as$nullin PowerShell. if the API returns nothing butnullwith theapplication/jsoncontent-type, then irm should return$null, IMO. This works fine whennullis nested,Result:
but instead of returning
$nullwhen the response is justnull, it is returning the stringnull. A string withnullin PowerShell is"null"in JSON, notnull.SteveL-MSFT commentedon Nov 3, 2017
My opinion is that an empty array should not be null for the reasons Mark Kraus (@markekraus) provides. As for a single
nullin the JSON, it seems reasonable for that to be $null and if the string "null" is currently being returned, that looks like a bug.markekraus commentedon Nov 3, 2017
Steve Lee (@SteveL-MSFT) it looks like irm handles all the other JSON literals properly
So it's just
nullthat's not right. Fixing that will be a breaking change, though. I imagine if people were were using an endpoint that returned single values that includednullwill have logic in place to deal with the broken behavior.SteveL-MSFT commentedon Nov 3, 2017
Mark Kraus (@markekraus) is this only the case where there's a single
null? if a json object has mutple properties and one or more are null, we deserialize those to$null, correct?markekraus commentedon Nov 4, 2017
Steve Lee (@SteveL-MSFT) it is the only case I have found. If it is in an array
[null, null, null]it works:result:
nullbecomes the string"null"when it is a key in a key/value pair (which we already know null and empty string keys in JSON are problematic), but when its a value in a key/value pair it works fine.So the only place it appears to be broken is single value
null.SteveL-MSFT commentedon Nov 4, 2017
If we want to take a breaking change, now is the time to do it. I wouldn't do it for 6.1.0.
markekraus commentedon Nov 4, 2017
#5338 for fixing the
nullserialization.SteveL-MSFT commentedon Nov 8, 2017
@PowerShell/powershell-committee reviewed this and agrees that the single
nullstring should be$nullvalue