-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Using StringValues
with JsonSerializerContext
produces warnings with 7.0.0-preview*
#74652
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescription
Reproduction Steps<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<OutputType>exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Primitives" Version="7.0.0-preview.7.22375.6" />
<PackageReference Include="System.Text.Json" Version="7.0.0-preview.7.22375.6" />
</ItemGroup>
</Project> using System;
using System.Text.Json.Serialization;
using Microsoft.Extensions.Primitives;
Console.WriteLine("Hello, world!");
[JsonSerializable(typeof(SomeClass))]
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default)]
public sealed partial class SomeJsonSerializerContext : JsonSerializerContext
{
}
public sealed class SomeClass
{
public StringValues SomeStringValues { get; set; }
} $ dotnet build
MSBuild version 17.4.0-preview-22368-02+c8492483a for .NET
Determining projects to restore...
All projects are up-to-date for restore.
C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(219,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [C:\Users\adren\rider\system-text-json-source-generator-fails-on-stringvalues\system-text-json-source-generator-fails-on-stringvalues.csproj]
C:\Users\adren\rider\system-text-json-source-generator-fails-on-stringvalues\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SomeJsonSerializerContext.StringValues.g.cs(31,32): warning CS8631: The type 'Microsoft.Extensions.Primitives.StringValues' cannot be used as type parameter 'TCollection' in the generic type or method 'JsonMetadataServices.CreateIListInfo<TCollection, TElement>(JsonSerializerOptions, JsonCollectionInfoValues<TCollection>)'. Nullability of type argument 'Microsoft.Extensions.Primitives.StringValues' doesn't match constraint type 'System.Collections.Generic.IList<string>'. [C:\Users\adren\rider\system-text-json-source-generator-fails-on-stringvalues\system-text-json-source-generator-fails-on-stringvalues.csproj]
system-text-json-source-generator-fails-on-stringvalues -> C:\Users\adren\rider\system-text-json-source-generator-fails-on-stringvalues\bin\Debug\net7.0\system-text-json-source-generator-fails-on-stringvalues.dll
Build succeeded.
C:\Users\adren\rider\system-text-json-source-generator-fails-on-stringvalues\System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\SomeJsonSerializerContext.StringValues.g.cs(31,32): warning CS8631: The type 'Microsoft.Extensions.Primitives.StringValues' cannot be used as type parameter 'TCollection' in the generic type or method 'JsonMetadataServices.CreateIListInfo<TCollection, TElement>(JsonSerializerOptions, JsonCollectionInfoValues<TCollection>)'. Nullability of type argument 'Microsoft.Extensions.Primitives.StringValues' doesn't match constraint type 'System.Collections.Generic.IList<string>'. [C:\Users\adren\rider\system-text-json-source-generator-fails-on-stringvalues\system-text-json-source-generator-fails-on-stringvalues.csproj]
1 Warning(s)
0 Error(s)
Time Elapsed 00:00:01.41 Expected behaviorUsing Actual behaviorUsing
Regression?This works when using version Known Workarounds
Configuration$ dotnet --info
.NET SDK:
Version: 7.0.100-preview.7.22377.5
Commit: ba310d9309
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22000
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.100-preview.7.22377.5\
Host:
Version: 7.0.0-preview.7.22375.6
Architecture: x64
Commit: eecb028078 Other informationNot sure where the fix belongs, but here's where the warning bubbles from: Lines 195 to 198 in 536f34d
|
Related to #61734. I don't believe this is a regression in .NET 7, the warnings have surfaced because of updates to the nullability annotation of |
@eiriktsarpalis I agree this isn't an STJ regression in .NET 7 (traces back to #55566 which shipped with .NET 6), but I was surprised to run into it using a combination of Given RC1 is right around the corner, I'm assuming this won't meet the bar for shipping with Investigation stuffCollecting snippets here to see if this is something I can contribute to, or if it's out of my depth. Here's the generated source causing the issue from // <auto-generated/>
#nullable enable
// Suppress warnings about [Obsolete] member usage in generated code.
#pragma warning disable CS0618
public sealed partial class SomeJsonSerializerContext
{
private global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Microsoft.Extensions.Primitives.StringValues>? _StringValues;
public global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Microsoft.Extensions.Primitives.StringValues> StringValues
{
get => _StringValues ??= Create_StringValues(Options);
}
private static global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Microsoft.Extensions.Primitives.StringValues> Create_StringValues(global::System.Text.Json.JsonSerializerOptions options)
{
global::System.Text.Json.Serialization.Metadata.JsonTypeInfo<global::Microsoft.Extensions.Primitives.StringValues>? jsonTypeInfo = null;
global::System.Text.Json.Serialization.JsonConverter? customConverter;
if (options.Converters.Count > 0 && (customConverter = GetRuntimeProvidedCustomConverter(options, typeof(global::Microsoft.Extensions.Primitives.StringValues))) != null)
{
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateValueInfo<global::Microsoft.Extensions.Primitives.StringValues>(options, customConverter);
}
else
{
global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues<global::Microsoft.Extensions.Primitives.StringValues> info = new global::System.Text.Json.Serialization.Metadata.JsonCollectionInfoValues<global::Microsoft.Extensions.Primitives.StringValues>()
{
ObjectCreator = () => new global::Microsoft.Extensions.Primitives.StringValues(),
NumberHandling = default,
SerializeHandler = StringValuesSerializeHandler
};
jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateIListInfo<global::Microsoft.Extensions.Primitives.StringValues, global::System.String>(options, info);
}
return jsonTypeInfo;
}
private static void StringValuesSerializeHandler(global::System.Text.Json.Utf8JsonWriter writer, global::Microsoft.Extensions.Primitives.StringValues value)
{
writer.WriteStartArray();
for (int i = 0; i < value.Count; i++)
{
writer.WriteStringValue(value[i]);
}
writer.WriteEndArray();
}
} where the offending line is: jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateIListInfo<global::Microsoft.Extensions.Primitives.StringValues, global::System.String>(options, info); and the literal patch would be: - jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateIListInfo<global::Microsoft.Extensions.Primitives.StringValues, global::System.String>(options, info);
+ jsonTypeInfo = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateIListInfo<global::Microsoft.Extensions.Primitives.StringValues, global::System.String?>(options, info); Back tracking through the generator source code:
So it seems like the patch would probably involve making Not sure how much work that would involve, but going to keep poking at this until/unless someone with more experience wants to jump in. |
Given though technically it isn't directly our regression it is a regression from the overall product perspective. IMO we should at least bar check this and I agree the appropriate fix for 7.0 (if any) seems like disabling warnings for the generated code since we don't know all the edge cases |
Let's try to mitigate the problem (and the related issue in #61734) by disabling nullability warnings in .NET 7. |
See #74801 (comment). We should keep this one open. |
Description
JsonSerializerContext
produces warnings when models containStringValues
due to nullable annotation mismatch.Reproduction Steps
Expected behavior
Using
StringValues
in conjunction withJsonSerializerContext
does not produce warnings.Actual behavior
Using
StringValues
in conjunction withJsonSerializerContext
produces warnings.Regression?
This works when using version
6.0.0
ofMicrosoft.Extensions.Primitives
because the interfaces forStringValues
were updated with nullable annotations for7.0.0-preview*
.Known Workarounds
CS8631
6.0.0
ofMicrosoft.Extensions.Primitives
Configuration
Other information
Not sure where the fix belongs, but here's where the warning bubbles from:
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonMetadataServices.Collections.cs
Lines 195 to 198 in 536f34d
The text was updated successfully, but these errors were encountered: