Skip to content

System.Text.Json source generator doesn't handle ReadOnlySpan<T> properties correctly #98590

@jp2masa

Description

@jp2masa

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

    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 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:

CS0306 The type 'ReadOnlySpan' may not be used as a type argument

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 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

Metadata

Metadata

Labels

area-System.Text.Jsonbugin-prThere is an active PR which will close this issue when it is mergedsource-generatorIndicates an issue with a source generator feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions