Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions JsonSubTypes/JsonSubtypesConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal class JsonSubtypesConverter : JsonSubtypes

[ThreadStatic] private static bool _isInsideWrite;
[ThreadStatic] private static bool _allowNextWrite;
private bool _addDiscriminatorFirst;
private readonly bool _addDiscriminatorFirst;

internal JsonSubtypesConverter(Type baseType, string discriminatorProperty,
Dictionary<object, Type> subTypeMapping, bool serializeDiscriminatorProperty, bool addDiscriminatorFirst) : base(discriminatorProperty)
Expand All @@ -47,7 +47,19 @@ internal JsonSubtypesConverter(Type baseType, string discriminatorProperty,
_addDiscriminatorFirst = addDiscriminatorFirst;
foreach (var type in _subTypeMapping)
{
_supportedTypes.Add(type.Value, type.Key);
if (_supportedTypes.ContainsKey(type.Value))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of check is better suited in JsonSubtypesConverterBuilder (this class is internal)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConverterBuilder contains inverted dictionary, but we can add check for it anyway

{
if (_serializeDiscriminatorProperty)
{
throw new InvalidOperationException(
"Multiple discriminators on single type are not supported " +
"when discriminator serialization is enabled");
}
}
else
{
_supportedTypes.Add(type.Value, type.Key);
}
}
}

Expand Down Expand Up @@ -109,4 +121,4 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
jsonObj.WriteTo(writer);
}
}
}
}