Skip to content

Commit

Permalink
Align JsonInheritanceConverterAttribute in Liquid template, RicoSuter…
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Nov 19, 2024
1 parent 9c627bf commit 22d33c9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,24 @@ public class JsonInheritanceConverter<TBase> : System.Text.Json.Serialization.Js

public override void Write(System.Text.Json.Utf8JsonWriter writer, TBase value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteString(_discriminatorName, GetDiscriminatorValue(value.GetType()));
if (value != null)
{
writer.WriteStartObject();
writer.WriteString(_discriminatorName, GetDiscriminatorValue(value.GetType()));

var bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes((object)value, options);
var document = System.Text.Json.JsonDocument.Parse(bytes);
foreach (var property in document.RootElement.EnumerateObject())
{
property.WriteTo(writer);
}

var bytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes((object)value, options);
var document = System.Text.Json.JsonDocument.Parse(bytes);
foreach (var property in document.RootElement.EnumerateObject())
writer.WriteEndObject();
}
else
{
property.WriteTo(writer);
writer.WriteNullValue();
}

writer.WriteEndObject();
}

public string GetDiscriminatorValue(System.Type type)
Expand All @@ -71,22 +78,25 @@ public class JsonInheritanceConverter<TBase> : System.Text.Json.Serialization.Js

protected System.Type GetDiscriminatorType(System.Text.Json.JsonElement jObject, System.Type objectType, string discriminatorValue)
{
var jsonInheritanceAttributeSubtype = GetObjectSubtype(objectType, discriminatorValue);
if (jsonInheritanceAttributeSubtype != null)
if (discriminatorValue != null)
{
return jsonInheritanceAttributeSubtype;
}
var jsonInheritanceAttributeSubtype = GetObjectSubtype(objectType, discriminatorValue);
if (jsonInheritanceAttributeSubtype != null)
{
return jsonInheritanceAttributeSubtype;
}

if (objectType.Name == discriminatorValue)
{
return objectType;
}
if (objectType.Name == discriminatorValue)
{
return objectType;
}

var typeName = objectType.Namespace + "." + discriminatorValue;
var subtype = System.Reflection.IntrospectionExtensions.GetTypeInfo(objectType).Assembly.GetType(typeName);
if (subtype != null)
{
return subtype;
var typeName = objectType.Namespace + "." + discriminatorValue;
var subtype = System.Reflection.IntrospectionExtensions.GetTypeInfo(objectType).Assembly.GetType(typeName);
if (subtype != null)
{
return subtype;
}
}

throw new System.InvalidOperationException("Could not find subtype of '" + objectType.Name + "' with discriminator '" + discriminatorValue + "'.");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NJsonSchema.Converters;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Xunit;
Expand Down Expand Up @@ -63,6 +65,16 @@ public async Task When_using_JsonInheritanceAttribute_and_SystemTextJson_then_sc
""".ReplaceLineEndings(), data);
}

[Fact]
public async Task When_discriminator_is_wrong_then_no_stackoverflow()
{
//// Act & Assert
Assert.Throws<InvalidOperationException>(() => // Throws "Could not find subtype of..."
{
JsonSerializer.Deserialize<Fruit>("{\"k\": \"invalid\"}");
});
}

#if !NETFRAMEWORK

public class Apple2 : Fruit2
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/Converters/JsonInheritanceConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public JsonInheritanceConverter(string discriminatorName)
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, TBase? value, JsonSerializerOptions options)
{
if (value is not null)
if (value != null)
{
writer.WriteStartObject();
writer.WriteString(_discriminatorName, GetDiscriminatorValue(value.GetType()));
Expand Down

0 comments on commit 22d33c9

Please sign in to comment.