-
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
Can't deserialize object despite custom JsonSerializerContext in Blazor WASM #81709
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsIs there an existing issue for this?
Describe the bugDeserializing an object with a [JsonConstructor] that has parameters in Blazor WASM throws an exception with One of the types in question, although only the first few lines are the important ones:
Adding a The exception I paste below is for Expected BehaviorThe whole object should be deserialized, the generated deserializer using the marked Steps To ReproduceCreate two classes in a project shared by ASP.NET Core backend & Blazor WASM, Exceptions (if any)
.NET Version7.0.102 Anything else?dotnet --info Runtime Environment: Host: .NET SDKs installed: .NET runtimes installed: Other architectures found: Environment variables: global.json file:
|
I'm not yet sure if this is related to something we do in JSON or not - it might be by design. @eerhardt do you know perhaps? |
Would be weird to have to give the constructor a dynamic dependency on itself by design. Or is there another way of doing that? Or does the custom JsonSerializerContext need more information to properly generate serialization logic? |
The issue here is that the trimmer is removing the parameter names for methods it doesn't think are reflected upon. It does this for all assemblies marked "IsTrimmable=true", which The reason the
A big issue here is that you seem to be trying to use the source generator, but it is still using the Reflection based serializer (i.e. JsonSerializer is using Refleciton to walk over the constructors of the Type). You will get a more consistent behavior if you ensure the source generated code is used when you serialize the object between the server and client. (See the FYI - @agocke @vitek-karas @sbomer |
It seems I did something wrong with deserialization then - when using that StjDeserializer with the custom serializer context (
I tried this so often, I even added a parameter to the call (https://blazor-webauthn.onrender.com/?customSerializer=true) so I could test it in a production-like environment. When I "Go to implementation" on the serializer context, it does find a Do I need to add something else to my serializer to make it use the custom context? |
Any chance you could be generating var options = new JsonSerializerOptions();
_ = new FidoModelSerializerContext(options); // use a context that is specifically bound to the current instance or just use the v7 sdk when generating the context. |
The lib is build & published using v6, yes. Not entirely sure what the sample code does - why put the options object into another options object and put that other object into the first object's TypeInfoResolver? I'm assuming there's supposed to be the custom serializer context there for now. I passed a
Then I created options and passed a n
The other way around doesn't work, I can't add an options instance to a context after construction. So after seeing it in the exception message, I tried |
(Edited the title to better describe the content for the next person having the problem I had) |
My apologies, my initial sample had an error. I've updated it for clarity.
Yes, you're absolutely right this should work best. I would strongly encourage you to regenerate the |
Since this is a library in the .NET Foundation, possibly (hopefully) used by many and big applications, would it make sense to stay with the LTS (.NET 6) and only update when a new LTS arrives (.NET 8)? |
Is there an existing issue for this?
Describe the bug
Deserializing an object with a [JsonConstructor] that has parameters in Blazor WASM throws an exception with
ConstructorContainsNullParameterNames
when published. It works in debug mode, hinting at that information being trimmed away somewhere, even though the containing type is included in a customJsonSerializerContext
and therefore serialization-logic is being generated.One of the types in question, although only the first few lines are the important ones:
Adding a
[DynamicDependency]
on itself to the constructor fixes this: passwordless-lib/fido2-net-lib@1fbfb25The exception I paste below is for
PublicKeyCredentialRpEntity
, but since that is a bigger class I pasted this smaller one. Same problem though, as you can see in the linked commit.Expected Behavior
The whole object should be deserialized, the generated deserializer using the marked
[JsonConstructor]
.Steps To Reproduce
Create two classes in a project shared by ASP.NET Core backend & Blazor WASM,
A
having a property ofB
(although this probably also happens whenB
is sent directly)Give
B
a ctor with parameters but no default ctor.Add custom
JsonSerializerContext
with [JsonSerializable] generating a serializer for the type you send.Publish the project (letting the linker do some trimming).
Send object from backend to frontend, try to deserialize in Blazor WASM.
Exceptions (if any)
.NET Version
7.0.102
Anything else?
dotnet --info
.NET SDK:
Version: 7.0.102
Commit: 4bbdd14480
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.102\
Host:
Version: 7.0.2
Architecture: x64
Commit: d037e07
.NET SDKs installed:
3.1.426 [C:\Program Files\dotnet\sdk]
6.0.308 [C:\Program Files\dotnet\sdk]
7.0.102 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.19 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
The text was updated successfully, but these errors were encountered: