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

Suppress warnings about [Obsolete] member usage in JSON src-gen'd code #63496

Merged
merged 1 commit into from
Jan 7, 2022
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 @@ -146,7 +146,10 @@ private void AddSource(string fileName, string source, bool isRootContextDef = f
bool isInGlobalNamespace = @namespace == JsonConstants.GlobalNamespaceValue;

StringBuilder sb = new(@"// <auto-generated/>
#nullable enable");
#nullable enable

// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618");

if (!isInGlobalNamespace)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,5 +522,36 @@ public class MyType
Assert.Equal(1, types.Count);
Assert.Equal("HelloWorld.MyType", types.Keys.First());
}

[Fact]
public static void NoWarningsDueToObsoleteMembers()
{
string source = @"using System;
using System.Text.Json.Serialization;

namespace Test
{
[JsonSerializable(typeof(ClassWithObsolete))]
public partial class JsonContext : JsonSerializerContext { }

public class ClassWithObsolete
{
[Obsolete(""This is a test"")]
public bool Test { get; set; }
}
}
";

Compilation compilation = CompilationHelper.CreateCompilation(source);
JsonSourceGenerator generator = new JsonSourceGenerator();

Compilation newCompilation = CompilationHelper.RunGenerators(compilation, out _, generator);
ImmutableArray<Diagnostic> generatorDiags = newCompilation.GetDiagnostics();

// No diagnostics expected.
CompilationHelper.CheckDiagnosticMessages(DiagnosticSeverity.Info, generatorDiags, Array.Empty<(Location, string)>());
CompilationHelper.CheckDiagnosticMessages(DiagnosticSeverity.Warning, generatorDiags, Array.Empty<(Location, string)>());
CompilationHelper.CheckDiagnosticMessages(DiagnosticSeverity.Error, generatorDiags, Array.Empty<(Location, string)>());
}
}
}