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
[JsonInheritanceConverter(typeof(A), "$type")]
[JsonInheritanceAttribute("B", typeof(B))]
public class A
{
public int? X { get; set; }
}
public class B : A
{
public int? Y { get; set; }
}
A a = new A { X = 1 };
string s = JsonSerializer.Serialize(a); //error.
Code is generated via NSwagStudio, simplified DEMO.
the suggestion in NET 7 , use [JsonDerivedType]
The text was updated successfully, but these errors were encountered:
The stack overflow happens when JsonInheritanceConverter.Read is trying to determine the subtype to deserialize. From your example, this happens when you look at type A which has a JsonInheritanceConverter attribute.
JsonInheritanceConverter.Read calls JsonInheritanceConverter.GetDiscriminatorType to get the subtype to use for deserialization. If the discriminator is not present in the serialized JSON stream, JsonInheritanceConverter.GetDiscriminatorType will return the base type (A) again.
Deserializing A will result in JsonInheritanceConverter.Read being called, and we have the endless loop causing the stack overflow.
It seems when a result type has subtypes with discriminator values, the discriminator must always be present, otherwise this endless loop will occur.
In my case, the return value is empty, but since the type is not nullable, it becomes an instance of A, serialized into JSON as {} in my case, with no discriminator, and so I see the same issue that you are seeing.
CODE:
Code is generated via NSwagStudio, simplified DEMO.
the suggestion in NET 7 , use
[JsonDerivedType]
The text was updated successfully, but these errors were encountered: