You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering an issue with System.Text.Json polymorphic serialization in combination with NSwag code generation for an Angular frontend.
C# Class Hierarchy:
[JsonPolymorphic]
[JsonDerivedType(typeof(B), nameof(B))]
[JsonDerivedType(typeof(C), nameof(C))]
public abstract class A { }
[JsonPolymorphic]
[JsonDerivedType(typeof(C), nameof(C))]
public abstract class B : A { }
public class C : B { }
Angular Code Generated by NSwag:
export abstract class A {
_discriminator!: string;
}
export abstract class B extends A {
_discriminator!: string; // Duplicate
}
export class C extends B { }
The duplicate _discriminator property in B causes issues. When I manually remove the duplicate from B, everything works as expected.
However, if I remove the [JsonPolymorphic] and [JsonDerivedType] annotations from class B to avoid the duplication, I get the following runtime error when posting to a controller expecting B:
System.NotSupportedException: Deserialization of interface or abstract types is not supported.
This exception does not occur when the _discriminator is manually removed and the annotations are kept.
Setup:
Using System.Text.Json
NSwag via builder.Services.AddOpenApiDocument();
Controllers accept A, B, or C as parameters
What I’ve Tried:
-Removing annotations from B: causes deserialization error
-Keeping annotations: causes duplicate _discriminator in generated TypeScript
-Manually removing _discriminator from B: works, but not ideal
Questions:
Is this duplication of _discriminator expected behavior?
Is there a way to configure NSwag or System.Text.Json to avoid this duplication?
Is there a recommended workaround that doesn’t involve switching to Newtonsoft.Json?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm encountering an issue with System.Text.Json polymorphic serialization in combination with NSwag code generation for an Angular frontend.
C# Class Hierarchy:
Angular Code Generated by NSwag:
The duplicate _discriminator property in B causes issues. When I manually remove the duplicate from B, everything works as expected.
However, if I remove the [JsonPolymorphic] and [JsonDerivedType] annotations from class B to avoid the duplication, I get the following runtime error when posting to a controller expecting B:
This exception does not occur when the _discriminator is manually removed and the annotations are kept.
Setup:
What I’ve Tried:
-Removing annotations from B: causes deserialization error
-Keeping annotations: causes duplicate _discriminator in generated TypeScript
-Manually removing _discriminator from B: works, but not ideal
Questions:
Beta Was this translation helpful? Give feedback.
All reactions