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

System.Text.Json Source Generator with TreatWarningsAsErrors Fails #64499

Closed
romfir opened this issue Jan 29, 2022 · 3 comments
Closed

System.Text.Json Source Generator with TreatWarningsAsErrors Fails #64499

romfir opened this issue Jan 29, 2022 · 3 comments

Comments

@romfir
Copy link

romfir commented Jan 29, 2022

Description

When project is set to treat warnings as errors and XML documentation generation is enabled the build using System.Text.Json source generator fails.

Reproduction Steps

csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateDocumentationFile>True</GenerateDocumentationFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

</Project>

and when I add a public class derived from JsonSerializerContext the build fails

using System.Text.Json;
using System.Text.Json.Serialization;
namespace ConsoleApp7;

/// <summary>
/// </summary>
public class Program
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments.
        Console.WriteLine(args.Length);

        Person? person = new() { FirstName = "Jane", LastName = "Doe" };
        byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(person, MyJsonContext2.Default.Person);
        person = JsonSerializer.Deserialize(utf8Json, MyJsonContext2.Default.Person);
    }
}

/// <summary>
/// </summary>
public class Person
{
    /// <summary>
    /// </summary>
    public string? FirstName { get; set; }
    /// <summary>
    /// </summary>
    public string? LastName { get; set; }
}

/// <summary>
/// 
/// </summary>
[JsonSerializable(typeof(Person))]
public partial class MyJsonContext2 : JsonSerializerContext
{
}

image

Expected behavior

The build should pass, one idea is to add #pragma warning disable 1591 and possibly // <auto-generated /> to generated files

Actual behavior

The build failes

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jan 29, 2022
@ghost
Copy link

ghost commented Jan 29, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

When project is set to treat warnings as errors and XML documentation generation is enabled the build using System.Text.Json source generator fails.

Reproduction Steps

csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateDocumentationFile>True</GenerateDocumentationFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <TreatWarningsAsErrors>True</TreatWarningsAsErrors>
  </PropertyGroup>

</Project>

and when I add a public class derived from JsonSerializerContext the build fails

using System.Text.Json;
using System.Text.Json.Serialization;
namespace ConsoleApp7;

/// <summary>
/// </summary>
public class Program
{
    static void Main(string[] args)
    {
        // Display the number of command line arguments.
        Console.WriteLine(args.Length);

        Person? person = new() { FirstName = "Jane", LastName = "Doe" };
        byte[] utf8Json = JsonSerializer.SerializeToUtf8Bytes(person, MyJsonContext2.Default.Person);
        person = JsonSerializer.Deserialize(utf8Json, MyJsonContext2.Default.Person);
    }
}

/// <summary>
/// </summary>
public class Person
{
    /// <summary>
    /// </summary>
    public string? FirstName { get; set; }
    /// <summary>
    /// </summary>
    public string? LastName { get; set; }
}

/// <summary>
/// 
/// </summary>
[JsonSerializable(typeof(Person))]
public partial class MyJsonContext2 : JsonSerializerContext
{
}

image

Expected behavior

The build should pass, one idea is to add #pragma warning disable 1591 and possibly // <auto-generated /> to generated files

Actual behavior

The build failes

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: romfir
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Related to #61734 and #62076.

@layomia
Copy link
Contributor

layomia commented Feb 3, 2022

Dupe of #61379.

@layomia layomia closed this as completed Feb 3, 2022
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Feb 3, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants