Skip to content
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

Added guard to throw InvalidOperrationException. #95723

Closed
wants to merge 1 commit into from

Conversation

rhirano0715
Copy link
Contributor

Added guard to throw InvalidOperrationException if the argument typeToConvert and Converter are incompatible. The exception message is "Type 'NameOfType' is not compatible with converter 'ConverterName'." and "Type 'NameOfType' is not compatible with converter 'ConverterName'.

NOTES:
The test threw a NotSupportedException on the following line and could not reproduce this issue, so the JsonSerializerOptions instantiation was changed. OptionsTests.ConverterRead_VerifyInvalidTypeToConvertFails() lineno 1432 KeyValuePair<int, int> kvp = converter.Read(ref reader, typeToConvert, options);

Fix #36605

Added guard to throw InvalidOperrationException if the argument typeToConvert and Converter are incompatible.
The exception message is "Type 'NameOfType' is not compatible with converter 'ConverterName'." and "Type 'NameOfType' is not compatible with converter 'ConverterName'.

NOTES:
The test threw a NotSupportedException on the following line and could not reproduce this issue, so the JsonSerializerOptions instantiation was changed.
OptionsTests.ConverterRead_VerifyInvalidTypeToConvertFails() lineno 1432
KeyValuePair<int, int> kvp = converter.Read(ref reader, typeToConvert, options);

Fix dotnet#36605
@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Dec 7, 2023
@ghost
Copy link

ghost commented Dec 7, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Added guard to throw InvalidOperrationException if the argument typeToConvert and Converter are incompatible. The exception message is "Type 'NameOfType' is not compatible with converter 'ConverterName'." and "Type 'NameOfType' is not compatible with converter 'ConverterName'.

NOTES:
The test threw a NotSupportedException on the following line and could not reproduce this issue, so the JsonSerializerOptions instantiation was changed. OptionsTests.ConverterRead_VerifyInvalidTypeToConvertFails() lineno 1432 KeyValuePair<int, int> kvp = converter.Read(ref reader, typeToConvert, options);

Fix #36605

Author: rhirano0715
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@@ -151,6 +151,11 @@ internal virtual bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, J

internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, scoped ref ReadStack state, out T? value, out bool isPopulatedValue)
{
if (typeToConvert != typeof(T))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the newly exposed CanConvert(Type typeToConvert) method be used here?
Does TryWrite method need similar guard?

@@ -151,6 +151,11 @@ internal virtual bool OnTryRead(ref Utf8JsonReader reader, Type typeToConvert, J

internal bool TryRead(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options, scoped ref ReadStack state, out T? value, out bool isPopulatedValue)
{
if (typeToConvert != typeof(T))
{
ThrowHelper.ThrowInvalidOperationException($"Type '{typeToConvert}' is not compatible with converter '{GetType()}'.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception message needs to be localizable. Besides afaic you should add a throw method in the ThrowHelper class like others.

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were designing STJ from scratch today, we would most likely be leaving out the typeToConvert parameter: it's mostly redundant since all converters must be generic and is only really useful in very limited (and somewhat unsound) polymorphic serialization applications.

Most converter implementations flat out ignore the typeToConvert parameter, so I honestly don't see a lot of benefit in adding this check in what is the serializer's hot path method. As to what is causing the NRE's described in #36605 these are better captured in #50205 and adding this check wouldn't suffice.

Thank you for taking the time to provide this PR, but unfortunately this isn't a change we would be considering.

@rhirano0715 rhirano0715 deleted the issue36605 branch December 11, 2023 01:56
@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Guard against invalid TypeToConvert args to read methods of object and collection converter
4 participants