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

CS8631 in System.Text.Json source generated code #78903

Closed
sungam3r opened this issue Nov 28, 2022 · 2 comments
Closed

CS8631 in System.Text.Json source generated code #78903

sungam3r opened this issue Nov 28, 2022 · 2 comments

Comments

@sungam3r
Copy link
Contributor

Description

Simple program produces warning/error (depends on TreatWarningsAsErrors switch) when using System.Text.Json source generator

Reproduction Steps

Repro

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

  <PropertyGroup>
    <LangVersion>latest</LangVersion>
    <OutputType>Library</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>

</Project>
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;

namespace A;

[JsonSerializable(typeof(Class1))]
internal partial class MyJsonContext : JsonSerializerContext
{
}

public class Class1
{
    public Class2? Property { get; set; }
}

public class Class2 : ReadOnlyDictionary<string, object?>
{

    public Class2(IDictionary<string, object?> dictionary)
        : base(dictionary)
    {
    }
}

Expected behavior

No warning/error.

Actual behavior

Error CS8631 The type 'A.Class2' cannot be used as type parameter 'TCollection' in the generic type or method 'JsonMetadataServices.CreateIDictionaryInfo<TCollection, TKey, TValue>(JsonSerializerOptions, JsonCollectionInfoValues)'. Nullability of type argument 'A.Class2' doesn't match constraint type 'System.Collections.Generic.IDictionary<string, object>'.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --info
ПАКЕТ SDK ДЛЯ .NET:
 Version:   7.0.100
 Commit:    e12b7af219

Среда выполнения:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100\

Host:
  Version:      7.0.0
  Architecture: x64
  Commit:       d099f075e4

.NET SDKs installed:
  5.0.403 [C:\Program Files\dotnet\sdk]
  7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 28, 2022
@ghost
Copy link

ghost commented Nov 28, 2022

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

Issue Details

Description

Simple program produces warning/error (depends on TreatWarningsAsErrors switch) when using System.Text.Json source generator

Reproduction Steps

Repro

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

  <PropertyGroup>
    <LangVersion>latest</LangVersion>
    <OutputType>Library</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors>
  </PropertyGroup>

</Project>
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;

namespace A;

[JsonSerializable(typeof(Class1))]
internal partial class MyJsonContext : JsonSerializerContext
{
}

public class Class1
{
    public Class2? Property { get; set; }
}

public class Class2 : ReadOnlyDictionary<string, object?>
{

    public Class2(IDictionary<string, object?> dictionary)
        : base(dictionary)
    {
    }
}

Expected behavior

No warning/error.

Actual behavior

Error CS8631 The type 'A.Class2' cannot be used as type parameter 'TCollection' in the generic type or method 'JsonMetadataServices.CreateIDictionaryInfo<TCollection, TKey, TValue>(JsonSerializerOptions, JsonCollectionInfoValues)'. Nullability of type argument 'A.Class2' doesn't match constraint type 'System.Collections.Generic.IDictionary<string, object>'.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --info
ПАКЕТ SDK ДЛЯ .NET:
 Version:   7.0.100
 Commit:    e12b7af219

Среда выполнения:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100\

Host:
  Version:      7.0.0
  Architecture: x64
  Commit:       d099f075e4

.NET SDKs installed:
  5.0.403 [C:\Program Files\dotnet\sdk]
  7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

Author: sungam3r
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Dec 2, 2022

Duplicate of #61734. Incidentally we've disabled nullability warnings in .NET 7 so upgrading to the latest STJ version should make the issue go away.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Dec 2, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Jan 1, 2023
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

2 participants