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

Fix simple scenarios for combining contexts #6

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,10 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe

sb.Append($@"

private static {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerContextTypeRef} context)
private {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerContextTypeRef}? context)
{{
{contextTypeRef} {JsonContextVarName} = ({contextTypeRef})context;
{JsonSerializerOptionsTypeRef} options = context.Options;
{contextTypeRef} {JsonContextVarName} = ({contextTypeRef}?)context ?? this;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not necessarily sure I understand the ramifications of this change. Who calls this method and under what circumstances can the context argument be null?

Copy link
Owner Author

@krwq krwq Jun 7, 2022

Choose a reason for hiding this comment

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

it will be null if options.TypeInfoResolver is not directly set to JsonSerializerContext (i.e. through combine). It's called when we add properties to JsonTypeInfo (it's happening with delay because otherwise it would cause SO)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Could the change be made simpler by pushing the null check to the callsite?
E.g.

FooPropInit(options.SerializerContext ?? this);

Copy link
Collaborator

Choose a reason for hiding this comment

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

How does this change impact existing scenaria, where the SerializerContext is simply unset and not tied a different IJsonTypeInfoResolver?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Could the change be made simpler by pushing the null check to the callsite?

Unfortunatelly no - we don't have access to context from where this is called (it's called inside of SourceGenJsonTypeInfo).

How does this change impact existing scenaria, where the SerializerContext is simply unset and not tied a different IJsonTypeInfoResolver?

This is only used for source gen and for those scenarios context is currently always set - the new scenarios where its unset only appeared after combining context was made possible, without combining this would never be called.

Copy link
Collaborator

@eiriktsarpalis eiriktsarpalis Jun 7, 2022

Choose a reason for hiding this comment

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

What about the removed if (context == null ... checks in SourceGenJsonTypeInfo? How were they being exercised before and did we have test coverage for them?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think it was just preventive check (probably triggered by nullability warning). It should not be possible for that ever to be null before combining was possible

{JsonSerializerOptionsTypeRef} options = {JsonContextVarName}.Options;

{JsonPropertyInfoTypeRef}[] {PropVarName} = {propertyArrayInstantiationValue};
");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ protected JsonSerializerContext(JsonSerializerOptions? options, bool bindOptions
{
options.TypeInfoResolver = this;
}
else
{
_options = options;
}

krwq marked this conversation as resolved.
Show resolved Hide resolved
_options = options;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ internal override JsonParameterInfoValues[] GetParameterInfoValues()
{
JsonSerializerContext? context = Options.SerializerContext;
JsonParameterInfoValues[] array;
if (context == null || CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null)
if (CtorParamInitFunc == null || (array = CtorParamInitFunc()) == null)
{
ThrowHelper.ThrowInvalidOperationException_NoMetadataForTypeCtorParams(context, Type);
return null!;
Expand All @@ -120,7 +120,7 @@ internal void AddPropertiesUsingSourceGenInfo()

JsonSerializerContext? context = Options.SerializerContext;
JsonPropertyInfo[] array;
if (context == null || PropInitFunc == null || (array = PropInitFunc(context)) == null)
if (PropInitFunc == null || (array = PropInitFunc(context!)) == null)
{
if (typeof(T) == typeof(object))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void CombiningContexts_Serialization<T>(T value, string expectedJs

public static IEnumerable<object[]> GetCombiningContextsData()
{
yield return WrapArgs(new JsonMessage { Message = "Hi" }, """{ "Message" : { "Hi" } }""");
yield return WrapArgs(new JsonMessage { Message = "Hi" }, """{ "Message" : "Hi", "Length" : 2 }""");
yield return WrapArgs(new Person("John", "Doe"), """{ "FirstName" : "John", "LastName" : "Doe" }""");
static object[] WrapArgs<T>(T value, string expectedJson) => new object[] { value, expectedJson };
}
Expand Down