Skip to content

[Text.Json] Error: NoMetadataForTypeProperties, SourceGenerationContext #113946

Open
@elringus

Description

@elringus

Description

Setting GenerationMode = JsonSourceGenerationMode.Serialization results in error: NoMetadataForTypeProperties, SourceGenerationContext exception in browser-wasm taget.

at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1[[Data, DotNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnTryRead(Utf8JsonReader& , Type , JsonSerializerOptions , ReadStack& , Data& )\n' +
    '   at System.Text.Json.Serialization.JsonConverter`1[[Data, DotNet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader& , Data& , JsonSerializerOptions , ReadStack& )\n' +
    '   at System.Text.Json.JsonSerializer.ReadFromSpan[Data](ReadOnlySpan`1 , JsonTypeInfo`1 )\n' +
    '   at Program.__Wrapper_Mutate_1171346831(JSMarshalerArgument* __arguments_buffer)\n' +
    'Error: NoMetadataForTypeProperties, SourceGenerationContext, Data\n' +

Reproduction Steps

.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net9.0</TargetFramework>
        <Configuration>Release</Configuration>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
        <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
        <WasmEnableLegacyJsInterop>false</WasmEnableLegacyJsInterop>
        <Nullable>enable</Nullable>
        <RunAOTCompilation>true</RunAOTCompilation>
        <OptimizationPreference>Speed</OptimizationPreference>

        <PublishTrimmed>true</PublishTrimmed>
        <TrimMode>full</TrimMode>
        <InvariantTimezone>true</InvariantTimezone>
        <InvariantGlobalization>true</InvariantGlobalization>
        <PredefinedCulturesOnly>true</PredefinedCulturesOnly>
    </PropertyGroup>

    <ItemGroup>
        <AssemblyAttribute Include="System.Runtime.Versioning.SupportedOSPlatform">
            <_Parameter1>browser</_Parameter1>
        </AssemblyAttribute>
    </ItemGroup>
</Project>

Program.cs

using System.Runtime.InteropServices.JavaScript;
using System.Text.Json;
using System.Text.Json.Serialization;

public struct Data
{
    public string Info;
    public bool Ok;
    public int Revision;
    public string[] Messages;
}

[JsonSerializable(typeof(Data))]
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
internal partial class SourceGenerationContext : JsonSerializerContext;

public static partial class Program
{
    public static void Main () { }

    [JSExport]
    public static string Mutate (string json)
    {
        var data = JsonSerializer.Deserialize<Data>(json,
            SourceGenerationContext.Default.Data);

        data.Info = "000";
        data.Ok = !data.Ok;
        data.Revision += 1;

        return JsonSerializer.Serialize(data,
            SourceGenerationContext.Default.Data);
    }
}

main.mjs

import { dotnet } from "./bin/Release/net9.0/browser-wasm/AppBundle/_framework/dotnet.js";

const { setModuleImports, getAssemblyExports, getConfig } = await dotnet
  .withDiagnosticTracing(false)
  .create();
const exports = await getAssemblyExports(getConfig().mainAssemblyName);

let data = {
    Info: "info",
    Ok: true,
    Revision: 1,
    Messages: ["1", "2", "3", "4"]
};

const startTime = performance.now();

for (let i = 0; i < 100000; i++)
    data = JSON.parse(exports.Program.Mutate(JSON.stringify(data)));

const endTime = performance.now();
const totalTime = endTime - startTime;

console.log(`Result: ${totalTime.toFixed(2)} ms`);

run

dotnet publish
node main.mjs

Expected behavior

No errors.

Actual behavior

Error is thrown.

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
 Version:           9.0.104
 Commit:            2750432faa
 Workload version:  9.0.100-manifests.163bd6d0
 MSBuild version:   17.12.27+e0b90a9a8

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64

.NET workloads installed:
 [wasm-tools]
   Installation Source: SDK 9.0.100
   Manifest Version:    9.0.3/9.0.100

Host:
  Version:      9.0.3
  Architecture: x64
  Commit:       831d23e561

Node v22.12.0

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions