Repository navigation
Closed
Labels
Milestone
Description
In PowerShell repo we get an exception for null parameter:
PS > $null | ConvertTo-Json -Compress
ConvertTo-Json: Value cannot be null. (Parameter 'type')
PS > Get-Error
Exception :
Type : System.ArgumentNullException
Message : Value cannot be null. (Parameter 'type')
ParamName : type
TargetSite :
Name : VerifyValueAndType
DeclaringType : System.Text.Json.JsonSerializer
MemberType : Method
Module : System.Text.Json.dll
StackTrace :
at System.Text.Json.JsonSerializer.VerifyValueAndType(Object value, Type type)
at System.Text.Json.JsonSerializer.Serialize(Object value, Type inputType, JsonSerializerOptions options)
at Microsoft.PowerShell.Commands.JsonObject.ConvertToJson2(Object objectToProcess, ConvertToJsonContext& context) in
C:\Users\1\Documents\GitHub\iSazonov\PowerShell\src\Microsoft.PowerShell.Commands.Utility\commands\utility\WebCmdlet\J
sonObject.cs:line 538
at Microsoft.PowerShell.Commands.ConvertToJsonCommand2.EndProcessing() in C:\Users\1\Documents\GitHub\iSazonov\Power
Shell\src\Microsoft.PowerShell.Commands.Utility\commands\utility\WebCmdlet\ConvertToJsonCommand.cs:line 252
at System.Management.Automation.Cmdlet.DoEndProcessing() in C:\Users\1\Documents\GitHub\iSazonov\PowerShell\src\Syst
em.Management.Automation\engine\cmdlet.cs:line 187
at System.Management.Automation.CommandProcessorBase.Complete() in C:\Users\1\Documents\GitHub\iSazonov\PowerShell\s
rc\System.Management.Automation\engine\CommandProcessorBase.cs:line 590
Source : System.Text.Json
HResult : -2147467261
CategoryInfo : NotSpecified: (:) [ConvertTo-Json], ArgumentNullException
FullyQualifiedErrorId : System.ArgumentNullException,Microsoft.PowerShell.Commands.ConvertToJsonCommand2
InvocationInfo :
MyCommand : ConvertTo-Json
ScriptLineNumber : 1
OffsetInLine : 9
HistoryId : 1
Line : $null | ConvertTo-Json -Compress
PositionMessage : At line:1 char:9
+ $null | ConvertTo-Json -Compress
+ ~~~~~~~~~~~~~~~~~~~~~~~~
InvocationName : ConvertTo-Json
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
I tried to directly reference System.Text.Json 5.0.0-preview.2.20160.6 but get the same exception.
I wonder to see JsonSerializer.VerifyValueAndType() because it was removed it in #2259
Now we moved to 5.0 Preview3 with the same error.
Activity
layomia commentedon Apr 29, 2020
@iSazonov it is indeed odd that you are seeing
VerifyValueAndTypein the stacktrace using a preview 3 build. Can you share a simple repro of the issue? Is any caller of the serializer passing innullas an argument for theinputTypeto any of theSerializemethod overloads?iSazonov commentedon Apr 29, 2020
Today we moved to 5.0 Preview4 in PowerShell repo and I will re-check.
Yes, we are trying to serialize null. It worked before but not in 5.0.
iSazonov commentedon Apr 29, 2020
@layomia After we update to latest .Net 5.0 preview I don't see
VerifyValueAndTypebut exception present.Exception : Type : System.ArgumentNullException Message : Value cannot be null. (Parameter 'inputType') ParamName : inputType TargetSite : Name : Serialize DeclaringType : System.Text.Json.JsonSerializer MemberType : Method Module : System.Text.Json.dll StackTrace : at System.Text.Json.JsonSerializer.Serialize(Object value, Type inputType, JsonSerializerOptions options)PowerShell 6.0 and 7.0 use NewtonSoft Json.Net and follow works:
But after we move to .Net 5.0 Json API we get the exception.
It is single regression we see currently (.Net team great work!).
JsonSerializer.Serialize()processes null-s well in general but does not allow null-s in its signature (it explicitly check and throw on inputType == null).Expectation is that follow works:
layomia commentedon Apr 29, 2020
Glad to hear! Thanks.
So the expectation is that when the instance to serialize is
null, passingnullas the input type doesn't cause theArgumentNullExceptionto be thrown.Why pass a
nullinputTypein the first place? Which scenarios require this?iSazonov commentedon Apr 29, 2020
Yes.
I look only from PowerShell side and I do not know if there are problems for other scenarios/applications.
I could do a check for null before calling this method, but my fear is that there may also be such limitations inside the method.
PowerShell scenario
$variable | ConvertTo-Json -Compressthat is "get an input object and convert to JSON" - so we should get the target type at runtime. This is implemented as:Again, my request comes from only the scenario and I could create a workaround (use
typeof(string)if null or address the scenario before the call as mentioned above) if MSFT team confirm that the API design is ideal.layomia commentedon Apr 30, 2020
Yes, we consider passing a
nullinput type invalid, and the API docs call this out.Doing a null check here is probably the best option, as the serializer will simply write
"null"if the root object isnullanyway. Looks like you're using a string-returning overload, so you could just write the null by hand.I'll go ahead and close this issue, since it is not actionable as is. Please feel free to re-open if you still face issues.
iSazonov commentedon Apr 30, 2020
@layomia Thanks! I will implement the way. I hope it will be in next PowerShell Preview and we will get more feedback.
danmoseley commentedon Nov 5, 2020
@layomia since ASP.NET had to react to that, do you think it is worth a breaking change note? so it appears in https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.1-5.0#core-net-libraries
layomia commentedon Nov 5, 2020
@danmosemsft - yes, it is a breaking change and has been shown to be significant. The change was made in #528. Will file a breaking change note.
danmoseley commentedon Nov 5, 2020
thanks!
layomia commentedon Nov 9, 2020
Update: a breaking change doc was already filed for this: dotnet/docs#20823, https://docs.microsoft.com/en-us/dotnet/core/compatibility/3.1-5.0#jsonserializerserialize-throws-argumentnullexception-when-type-parameter-is-null.