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

Make source generated JsonTypeInfo properties nullable-oblivious #95341

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.Json.Serialization;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using SourceGenerators;

Expand Down Expand Up @@ -1009,7 +1007,9 @@ private static void GenerateTypeInfoFactoryHeader(SourceWriter writer, TypeGener
/// <summary>
/// Defines the source generated JSON serialization contract metadata for a given type.
/// </summary>
#nullable disable annotations // Marking the property type as nullable-oblivious.
public {{typeInfoFQN}} {{typeInfoPropertyName}}
#nullable enable annotations
eiriktsarpalis marked this conversation as resolved.
Show resolved Hide resolved
{
get => _{{typeInfoPropertyName}} ??= ({{typeInfoFQN}}){{OptionsInstanceVariableName}}.GetTypeInfo(typeof({{typeFQN}}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,5 +737,36 @@ public class MyClass
Compilation compilation = CompilationHelper.CreateCompilation(source, parseOptions: CompilationHelper.CreateParseOptions(languageVersion));
CompilationHelper.RunJsonSourceGenerator(compilation);
}

[Fact]
public void DoesNotWarnOnNullabilityMismatch()
{
string source = $$"""
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
#nullable enable

namespace HelloWorld
{
public static class MyClass
{
public static string Test()
{
Dictionary<int, string?> values = new();
return JsonSerializer.Serialize(values, JsonContext.Default.DictionaryInt32String);
}
}

[JsonSerializable(typeof(Dictionary<int, string>))]
internal partial class JsonContext : JsonSerializerContext
{
}
}
""";

Compilation compilation = CompilationHelper.CreateCompilation(source);
CompilationHelper.RunJsonSourceGenerator(compilation);
}
}
}
Loading