-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Argument tests for CreateJsonTypeInfo and CreateJsonPropertyInfo #71324
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsFixes: #71296 I went through all APIs and seems only CreateJsonTypeInfo and CreatePropertyInfo were missing checks. I also added some test gaps for null modifier but that was checking args already
|
d87996f
to
39b0578
Compare
if (type == null) | ||
{ | ||
throw new ArgumentNullException(nameof(type)); | ||
} | ||
|
||
if (options == null) | ||
{ | ||
throw new ArgumentNullException(nameof(options)); | ||
} |
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.
We should be using ArgumentNullException.ThrowIfnull
for cases like that, see #68178.
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.
Actually we're also compiling against netfx and that's not available in there
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.
Yeah, I just realized. We have an equivalent method in ThrowHelper
so you can just use that.
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.
Other than one minor nit, lgtm.
if (IsInvalidForSerialization(propertyType)) | ||
{ | ||
ThrowHelper.ThrowArgumentException_CannotSerializeInvalidType(nameof(propertyType), propertyType, Type, name); | ||
} |
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.
Does the method check if TypeInfoKind == JsonTypeInfoKind.Object
. We migth want to throw InvalidOperationException
if we call the create method for an invalid declaring type.
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 thought we discussed this and decided not to throw. We won't let you add such property but no problem with creating
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.
Fair enough -- probably not too important. But we won't be able to change this in the future because it would create a likely breaking change.
[InlineData(typeof(RefStruct))] | ||
public static void CreateJsonTypeInfoWithInappropriateTypeThrows(Type type) | ||
{ | ||
Assert.Throws<ArgumentException>(() => JsonTypeInfo.CreateJsonTypeInfo(type, new JsonSerializerOptions())); |
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.
@fanyang-mono One of the types being passed in (probably void) results in a runtime crash. Would you be able to take a look and see why?
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've filed #71339 as I'm most likely going to try to workaround the issue in this PR
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 missed the message yesterday. Will take a look at the issue when I get a chance.
Fixes: #71296
I went through all APIs and seems only CreateJsonTypeInfo and CreatePropertyInfo were missing checks. I also added some test gaps for null modifier but that was checking args already