Skip to content

[net9.0-preview3]: NativeAOT System.Text.Json throws "System.NotSupportedException: 'System.Text.Json.Serialization.JsonDerivedTypeAttribute[]' is missing native code or metadata." #101205

Closed as not planned
@lambdageek

Description

@lambdageek

When compiling an app that uses System.Text.Json using NativeAOT, using .NET 9 Preview 3, the app throws an exception:

Unhandled exception. System.NotSupportedException: 'System.Text.Json.Serialization.JsonDerivedTypeAttribute[]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.General.TypeUnifier.WithVerifiedTypeHandle(RuntimeArrayTypeInfo, RuntimeTypeInfo) + 0x78
   at System.Array.InternalCreate(RuntimeType, Int32, Int32*, Int32*) + 0xdc
   at System.Array.CreateInstance(Type, Int32) + 0x4c
   at System.Attribute.Instantiate(IEnumerable`1, Type) + 0xd8
   at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](MemberInfo, Boolean) + 0x48
   at System.Text.Json.Serialization.Metadata.JsonPolymorphismOptions.CreateFromAttributeDeclarations(Type) + 0x9c
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo.PopulatePolymorphismMetadata() + 0x18
   at System.Text.Json.Serialization.Metadata.JsonMetadataServices.CreateCore[T](JsonSerializerOptions, JsonObjectInfoValues`1) + 0xdc
   at ContractDescriptorParser.ContractDescriptorContext.Create_ContractDescriptor(JsonSerializerOptions) + 0xec
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type) + 0x4c
   at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type, JsonSerializerOptions.CachingContext) + 0x20
--- End of stack trace from previous location ---
   at System.Text.Json.JsonSerializerOptions.CachingContext.CacheEntry.GetResult() + 0x24
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Nullable`1, Boolean, Boolean) + 0x54
   at System.Text.Json.JsonSerializerOptions.GetTypeInfo(Type) + 0x4c
   at ContractDescriptorParser.ContractDescriptorContext.get_ContractDescriptor() + 0x30
   at ContractDescriptorParser.ParseCompact(ReadOnlySpan`1) + 0x3c
   at Program.<Main>$(String[] args) + 0x104
zsh: abort      ./bin/Release/net9.0/osx-arm64/native/roundtrip-json ./foo.jsonc

This is a regression from .NET 8.

Repro: https://github.com/lambdageek/roundtrip-json

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions