Skip to content

Make resolver GetTypeInfo methods return new instances on every call #76683

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
56 changes: 18 additions & 38 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ private sealed partial class Emitter
private const string DefaultOptionsStaticVarName = "s_defaultOptions";
private const string DefaultContextBackingStaticVarName = "s_defaultContext";
internal const string GetConverterFromFactoryMethodName = "GetConverterFromFactory";
private const string GetTypeInfoCoreMethodName = "GetTypeInfoCore";
private const string GetTypeInfoMethodName = "GetTypeInfo";
private const string InfoVarName = "info";
private const string PropertyInfoVarName = "propertyInfo";
internal const string JsonContextVarName = "jsonContext";
Expand All @@ -37,6 +39,7 @@ private sealed partial class Emitter
private const string JsonTypeInfoReturnValueLocalVariableName = "jsonTypeInfo";
private const string PropInitMethodNameSuffix = "PropInit";
private const string RuntimeCustomConverterFetchingMethodName = "GetRuntimeProvidedCustomConverter";
private const string TypeLocalVariableName = "type";
private const string SerializeHandlerPropName = "SerializeHandler";
private const string ValueVarName = "value";
private const string WriterVarName = "writer";
Expand Down Expand Up @@ -1227,9 +1230,9 @@ private static string GetFetchLogicForGetCustomConverter_TypesWithFactories()
{
return @$"

private static {JsonConverterTypeRef} {GetConverterFromFactoryMethodName}({JsonSerializerOptionsTypeRef} {OptionsLocalVariableName}, {TypeTypeRef} type, {JsonConverterFactoryTypeRef} factory)
private static {JsonConverterTypeRef} {GetConverterFromFactoryMethodName}({JsonSerializerOptionsTypeRef} {OptionsLocalVariableName}, {TypeTypeRef} {TypeLocalVariableName}, {JsonConverterFactoryTypeRef} factory)
{{
{JsonConverterTypeRef}? converter = factory.CreateConverter(type, {OptionsLocalVariableName});
{JsonConverterTypeRef}? converter = factory.CreateConverter({TypeLocalVariableName}, {OptionsLocalVariableName});
if (converter == null || converter is {JsonConverterFactoryTypeRef})
{{
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{ExceptionMessages.InvalidJsonConverterFactoryOutput}"", factory.GetType()));
Expand All @@ -1245,56 +1248,33 @@ private string GetGetTypeInfoImplementation()

sb.Append(
@$"/// <inheritdoc/>
public override {JsonTypeInfoTypeRef} GetTypeInfo({TypeTypeRef} type)
{{");

HashSet<TypeGenerationSpec> types = new(_currentContext.TypesWithMetadataGenerated);
public override {JsonTypeInfoTypeRef} {GetTypeInfoMethodName}({TypeTypeRef} {TypeLocalVariableName})
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsInstanceVariableName});
Copy link
Member

Choose a reason for hiding this comment

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

Nit:

Suggested change
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsInstanceVariableName});
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsPropertyName});


// TODO (https://github.com/dotnet/runtime/issues/52218): Make this Dictionary-lookup-based if root-serializable type count > 64.
foreach (TypeGenerationSpec metadata in types)
{
if (metadata.ClassType != ClassType.TypeUnsupportedBySourceGen)
{
sb.Append($@"
if (type == typeof({metadata.TypeRef}))
{{
return this.{metadata.TypeInfoPropertyName};
}}
{JsonTypeInfoTypeRef}? {JsonTypeInfoResolverTypeRef}.{GetTypeInfoMethodName}({TypeTypeRef} {TypeLocalVariableName}, {JsonSerializerOptionsTypeRef} {OptionsLocalVariableName})
=> {GetTypeInfoCoreMethodName}({TypeLocalVariableName}, {OptionsLocalVariableName});
");
}
}

sb.AppendLine(@"
return null!;
}");

// Explicit IJsonTypeInfoResolver implementation
sb.AppendLine();
sb.Append(@$"{JsonTypeInfoTypeRef}? {JsonTypeInfoResolverTypeRef}.GetTypeInfo({TypeTypeRef} type, {JsonSerializerOptionsTypeRef} {OptionsLocalVariableName})
{{
if ({OptionsInstanceVariableName} == {OptionsLocalVariableName})
{{
return this.GetTypeInfo(type);
}}
else
{{");
sb.Append(@$"private {JsonTypeInfoTypeRef}? {GetTypeInfoCoreMethodName}({TypeTypeRef} {TypeLocalVariableName}, {JsonSerializerOptionsTypeRef} {OptionsLocalVariableName})
{{");

// TODO (https://github.com/dotnet/runtime/issues/52218): Make this Dictionary-lookup-based if root-serializable type count > 64.
foreach (TypeGenerationSpec metadata in types)
foreach (TypeGenerationSpec metadata in _currentContext.TypesWithMetadataGenerated)
{
if (metadata.ClassType != ClassType.TypeUnsupportedBySourceGen)
{
sb.Append($@"
if (type == typeof({metadata.TypeRef}))
{{
return {metadata.CreateTypeInfoMethodName}({OptionsLocalVariableName});
}}
if ({TypeLocalVariableName} == typeof({metadata.TypeRef}))
{{
return {metadata.CreateTypeInfoMethodName}({OptionsLocalVariableName});
}}
");
}
}

sb.Append($@"
return null;
}}
return null;
}}
");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,5 +463,49 @@ public TestResolver(Func<Type, JsonSerializerOptions, JsonTypeInfo?> getTypeInfo

public JsonTypeInfo? GetTypeInfo(Type type, JsonSerializerOptions options) => _getTypeInfo(type, options);
}

[Fact]
public static void OptionsGetTypeInfoDoesNotReturnContextInstance()
{
PersonJsonContext context = PersonJsonContext.Default;

// Context GetTypeInfo(Type) method returns new instance on each call
Assert.NotSame(context.GetTypeInfo(typeof(Person)), context.GetTypeInfo(typeof(Person)));

JsonTypeInfo context_StaticProp_TypeInfo = context.Person;
JsonTypeInfo context_Fetched_TypeInfo = context.GetTypeInfo(typeof(Person));

// Context fetched instance is different from context static instance
Assert.NotSame(context_StaticProp_TypeInfo, context_Fetched_TypeInfo);
context_Fetched_TypeInfo.Properties.Clear(); // Context fetched instance is mutable
Assert.Equal(2, context_StaticProp_TypeInfo.Properties.Count);
Assert.Equal(0, context_Fetched_TypeInfo.Properties.Count);

// Context static instance is not mutable
Assert.Throws<InvalidOperationException>(() => context_StaticProp_TypeInfo.CreateObject = null);
Assert.Throws<InvalidOperationException>(() => context_StaticProp_TypeInfo.Properties.Clear());

// Context-owned options is read-only so fetched instance is cached
JsonTypeInfo options_Cached_TypeInfo = context.Options.GetTypeInfo(typeof(Person));
Assert.Same(options_Cached_TypeInfo, context.Options.GetTypeInfo(typeof(Person)));

// Context-owned type info is read-only
Assert.Throws<InvalidOperationException>(() => options_Cached_TypeInfo.Properties.Clear());

// Options fetched instance is different from context instances
Assert.NotSame(context_StaticProp_TypeInfo, options_Cached_TypeInfo);
Assert.NotSame(context_Fetched_TypeInfo, options_Cached_TypeInfo);

// Context GetTypeInfo(Type, Options) method returns new instance on each call
IJsonTypeInfoResolver resolver = context;
JsonSerializerOptions options = new() { TypeInfoResolver = context };

Assert.NotSame(
resolver.GetTypeInfo(typeof(Person), options),
resolver.GetTypeInfo(typeof(Person), options));

// Options are still mutable, so fetched type-info's are not cached.
Assert.NotSame(options.GetTypeInfo(typeof(Person)), options.GetTypeInfo(typeof(Person)));
}
}
}