[release/3.0] Honor IsReadOnly when deserializing collections#40438
Conversation
…#40366) * Don't add to collection when IsReadOnly is true * Throw NotSupportedException instead of JsonException * Add test for readonly collections * Revert back to InvalidCastException
|
Once merged, we need to submit a breaking-change issue to document the change in exception type from preview7 to now. |
|
Looks like there's a failing test on this branch that wasn't present on |
|
Yep, I believe master is failing too.
Yes. I'd recommend submitting the PR in master and add the commit to this PR after that passes CI. |
Submitted #40439. |
ericstj
left a comment
There was a problem hiding this comment.
I'm ok with this change. If we believe the NotSupportedException is correct, it is better to make that change now than have it be breaking in vNext. I agree that this is common enough for folks to hit that we need a better exception.
|
tactics approved - preview9 |
Ports #40366 to 3.0.
Closes https://github.com/dotnet/corefx/issues/40237
// @layomia @steveharter @ahsonkhan
Description
Previously, the System.Text.Json serializer would try to call
Addon any collection, even if it has anIsReadOnlyproperty that returnstrue. This would lead to aNotSupportedException. It also used to throw aJsonExceptionif a collection was unsupported.After this PR, the serializer will no longer attempt to call
Addif the collection hasIsReadOnly == trueand will always throw aNotSupportedExceptionwith a descriptive message for unsupported collections.Customer Impact
Customers will now have to catch/handle/deal with
NotSupportedExceptionin addition toJsonExceptionwhen deserializing. Read-only collections will have a better exception message telling the customer that the collection is unsupported.Regression?
No.
Risk
Low. Most collections that returns
trueforIsReadOnlywill throwNotSupportedExceptionwhen trying to callAddanyway. This will just give them a better exception message.