-
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
System.Text.Json source generator doesn't handle ReadOnlySpan<T>
properties correctly
#98590
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionWhen using the System.Text.Json source generator with Reproduction Steps class Test(string value)
{
public string Value =>
value;
public ReadOnlySpan<char> AsSpan =>
value;
}
[JsonSourceGenerationOptions(IgnoreReadOnlyProperties = true)]
[JsonSerializable(typeof(Test))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
} Expected behaviorNo compilation error. Also, if Actual behaviorMultiple compilation errors on generated code, because Example:
Relevant part of the generated code private static global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[] TestPropInit(global::System.Text.Json.JsonSerializerOptions options)
{
var properties = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfo[2];
var info0 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<string>
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::TestApp.Test),
Converter = null,
Getter = static obj => ((global::TestApp.Test)obj).Value,
Setter = null,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = null,
PropertyName = "Value",
JsonPropertyName = null
};
properties[0] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<string>(options, info0);
var info1 = new global::System.Text.Json.Serialization.Metadata.JsonPropertyInfoValues<global::System.ReadOnlySpan<char>>
{
IsProperty = true,
IsPublic = true,
IsVirtual = false,
DeclaringType = typeof(global::TestApp.Test),
Converter = null,
Getter = static obj => ((global::TestApp.Test)obj).AsSpan,
Setter = null,
IgnoreCondition = null,
HasJsonInclude = false,
IsExtensionData = false,
NumberHandling = null,
PropertyName = "AsSpan",
JsonPropertyName = null
};
properties[1] = global::System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreatePropertyInfo<global::System.ReadOnlySpan<char>>(options, info1);
return properties;
} Regression?No response Known WorkaroundsNo response Configuration.NET 8.0.200 Also tried using the latest preview version of Other informationNo response
|
Not sure if this statement is meant to apply specifically to Span/ReadOnlySpan-typed properties, or if this statement was meant to apply to getter-only properties of any type. Commenting out the |
In the meantime as a workaround you could either mark the property internal or convert it to a method. |
Unfortunately, it's not my code, but it's open source, so I can fork it. Would skipping all properties whose type The only alternative I can think of would be implementing an API for conversion or explicit casting of types like: [JsonSourceGenerationConvert(typeof(ReadOnlySpan<char>), typeof(string))] In this case, the property type would be replaced with I was also a bit disappointed to find that I can't declare a member with the same name as the generated one to replace it, as the generator doesn't care if it's creating a duplicate declaration. Is this by design, or would it be acceptable to skip already declared members? This would make it much easier to workaround bugs or customize generated code. If it helps, I can work on a PR to fix this. |
More likely, it would involve updating the SG so that all ref-like property types get skipped. |
Description
When using the System.Text.Json source generator with
ReadOnlySpan<T>
properties, the generated code is invalid. Also,IgnoreReadOnlyProperties
doesn't skip getter-only properties.Reproduction Steps
Expected behavior
No compilation error. Also, if
IgnoreReadOnlyProperties
is supposed to ignore getter-only properties, both properties above should be skipped.Actual behavior
Multiple compilation errors on generated code, because
ReadOnlySpan<char>
was used as a generic argument.Example:
Relevant part of the generated code
Regression?
No response
Known Workarounds
No response
Configuration
.NET 8.0.200
Also tried using the latest preview version of
System.Text.Json
from NuGet.Other information
No response
The text was updated successfully, but these errors were encountered: