Repository navigation
Invoke-RestMethod cmdlet error if the response is neither JSON nor XML #2862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dd26d01
c91f083
5ae33c9
ad9d14b
a250f8e
52ad2d7
ea22114
524d2af
62670ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,20 +44,32 @@ public static object ConvertFromJson(string input, out ErrorRecord error) | |
| } | ||
| error = null; | ||
| #if CORECLR | ||
| object obj = JsonConvert.DeserializeObject(input, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }); | ||
|
|
||
| // JObject is a IDictionary | ||
| if (obj is JObject) | ||
| object obj = null; | ||
| try | ||
| { | ||
| obj = JsonConvert.DeserializeObject(input, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.None, MaxDepth = 1024 }); | ||
|
|
||
| // JObject is a IDictionary | ||
| var dictionary = obj as JObject; | ||
| obj = PopulateFromJDictionary(dictionary, out error); | ||
| if (dictionary != null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hope Declaration Expressions can be brought back in C# 7. It will make code like this much more concise. |
||
| { | ||
| obj = PopulateFromJDictionary(dictionary, out error); | ||
| } | ||
| else | ||
| { | ||
| // JArray is a collection | ||
| var list = obj as JArray; | ||
| if (list != null) | ||
| { | ||
| obj = PopulateFromJArray(list, out error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // JArray is a collection | ||
| else if (obj is JArray) | ||
| catch (JsonException je) | ||
| { | ||
| var list = obj as JArray; | ||
| obj = PopulateFromJArray(list, out error); | ||
| var msg = string.Format(CultureInfo.CurrentCulture, WebCmdletStrings.JsonDeserializationFailed, je.Message); | ||
| // the same as JavaScriptSerializer does | ||
| throw new ArgumentException(msg, je); | ||
| } | ||
| #else | ||
| //In ConvertTo-Json, to serialize an object with a given depth, we set the RecursionLimit to depth + 2, see JavaScriptSerializer constructor in ConvertToJsonCommand.cs. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,4 +207,7 @@ | |
| <data name="ReachedMaximumDepthAllowed" xml:space="preserve"> | ||
| <value>The maximum depth allowed for serialization is {0}.</value> | ||
| </data> | ||
| <data name="JsonDeserializationFailed" xml:space="preserve"> | ||
| <value>Conversion from JSON failed with error: {0}</value> | ||
| </data> | ||
| </root> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add newline.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this file is autogenerated |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| Describe 'Unit tests for JsonObject' -tags "CI" { | ||
|
|
||
| function ShouldThrow | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the template with FullyQualifiedErrorId to check for proper errors And see my comment about
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This man tells that I've got to verify FullyQualifiedErrorId, going to fix this, thx. However, it does not explicitly state that all the expected exception checks should Here developers simply forgot to throw before catch: Here instead of err checking we will get a nullref: Perhaps this is the reason why in places like here or here were desided to implement the custom assertions. Of course, all the mistakes I encountered are related to lack of calibration, but I merely revealed you a couple of drawbacks. All in all, you can still insist on it, persuade me by proposing other arguments and other ideas to refactor it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the comment about testcase is valid, thx
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's totally fine to have custom assertions in your test. I think Ilya (@iSazonov) meant that the For those tests you found out, they were written before we came up with the guidelines, but we do want future test to follow the guidelines as much as possible 😄 |
||
| { | ||
| param ( | ||
| [Parameter(ValueFromPipeline = $true)] | ||
| $InputObject, | ||
| [Parameter(Position = 0)] | ||
| $ExpectedException | ||
| ) | ||
|
|
||
| try | ||
| { | ||
| & $InputObject | ||
| throw "Should throw exception" | ||
| } | ||
| catch | ||
| { | ||
| $_.FullyQualifiedErrorId | should be $ExpectedException | ||
| } | ||
| } | ||
|
|
||
| $validStrings = @( | ||
| @{ name = "empty"; str = "" } | ||
| @{ name = "spaces"; str = " " } | ||
| @{ name = "object"; str = "{a:1}" } | ||
| ) | ||
|
|
||
| It 'no error for valid string - <name>' -TestCase $validStrings { | ||
| param ($str) | ||
| $errRecord = $null | ||
| [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, [ref]$errRecord) | ||
| $errRecord | Should BeNullOrEmpty | ||
| } | ||
|
|
||
| $invalidStrings = @( | ||
| @{ name = "plain text"; str = "plaintext" } | ||
| @{ name = "part"; str = '{"a" :' } | ||
| ) | ||
|
|
||
| It 'throw ArgumentException for invalid string - <name>' -TestCase $invalidStrings { | ||
| param ($str) | ||
| $errRecord = $null | ||
| { [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($str, [ref]$errRecord) } | ShouldThrow 'ArgumentException' | ||
| } | ||
| } | ||
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.
Why we don't want to mask this exception?
Uh oh!
There was an error while loading. Please reload this page.
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.
Since it is already handled here. I decided to fix the behavior of JsonObject making them invariant of a platform rather than handling serialization exceptions for every platform. You can also take a look at the C# unit tests I mentioned above.
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.
I like this change, so when using
JsonObject.ConvertFromJsonin C#, you don't need to catch different exceptions between FullCLR and CoreCLR.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.
Thanks. Clear.