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

Argument tests for CreateJsonTypeInfo and CreateJsonPropertyInfo #71324

Merged
merged 2 commits into from
Jun 27, 2022

Conversation

krwq
Copy link
Member

@krwq krwq commented Jun 27, 2022

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

@ghost
Copy link

ghost commented Jun 27, 2022

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

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

Author: krwq
Assignees: eiriktsarpalis
Labels:

area-System.Text.Json

Milestone: -

@krwq krwq force-pushed the contract-customization-arg-checks branch from d87996f to 39b0578 Compare June 27, 2022 10:41
Comment on lines +435 to +443
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

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.

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.

Other than one minor nit, lgtm.

if (IsInvalidForSerialization(propertyType))
{
ThrowHelper.ThrowArgumentException_CannotSerializeInvalidType(nameof(propertyType), propertyType, Type, name);
}
Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member

@eiriktsarpalis eiriktsarpalis Jun 27, 2022

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()));
Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

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.

@krwq krwq merged commit 5be7092 into dotnet:main Jun 27, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jul 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CreateJsonPropertyInfo NullReferenceException
4 participants