-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Missing Enum constraint on JsonNumberEnumConverter<TEnum> in reference assembly #99878
Comments
Strange, why APICompact didn't catch this. Looks like bug or missing usecase to check in it. cc @ViktorHofer |
If I understand correctly, ApiCompat would have to compare ITypeParameterSymbol.ConstraintTypes. However, |
@KalleOlaviNiemitalo what branch is this? I see the constraint is present in both
and
I also can't reproduce the crash you're seeing: |
@eiriktsarpalis, you're looking at JsonStringEnumConverter<TEnum>, but this issue is for JsonNumberEnumConverter<TEnum>. |
D'oh! Didn't even notice it was for a different type. Thanks. |
FYI @carlossanlop @ViktorHofer @ericstj this will require a .NET 8 backport that updates the reference source. |
Does this truly deserve a backport? It seems unlikely that the bug would be hurting any production applications. I originally noticed the missing constraint in the documentation and was going to file an issue there, but filed this one here instead. |
Not sure we need the backport here. Please file an issue in dotnet/sdk that APICompat didn't catch this. |
@ShreyasJejurkar, do you intend to file the APICompat issue? I'm not really familiar with the tool. |
@eiriktsarpalis, should this issue be moved to the 9.0.0 milestone because it won't be serviced in 8.0.x? |
Description
In the source code, System.Text.Json.Serialization.JsonNumberEnumConverter<TEnum> has a type constraint
where TEnum : struct, Enum
. In the reference assembly however, the constraint is justwhere TEnum : struct
and omitsEnum
. Thus, ifTEnum
is a non-enum value type, the compiler doesn't notice any problem but it fails at run time.runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonNumberEnumConverter.cs
Lines 17 to 18 in 3eb129f
runtime/src/libraries/System.Text.Json/ref/System.Text.Json.cs
Line 981 in 3eb129f
Reproduction Steps
NonEnum.csproj:
Program.cs:
dotnet run
Expected behavior
The compiler should report an error:
Actual behavior
The build succeeds without any warnings, but the program fails at run time:
Regression?
Not a regression. Both the
src
andref
changes came from #88984, so they have never been consistent in any release.Known Workarounds
No response
Configuration
.NET SDK 8.0.202
.NET 8.0.3
Windows 10 22H2 x64
Other information
This bug might be considered part of #26187.
The System.Enum type constraint is missing from the documentation page https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonnumberenumconverter-1?view=net-8.0 as well.
The System.Text.Json 8.0.3 NuGet package does not carry any reference assemblies. Thus, if you reference that and target .NET Framework, then you get the expected error CS0315 at compile time.
The text was updated successfully, but these errors were encountered: