OpenApiDiscriminator
Describes a schema discriminator.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema.References;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace NJsonSchema
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
public class OpenApiDiscriminator
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
private sealed class DiscriminatorMappingConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
[return: System.Runtime.CompilerServices.Nullable(2)]
public override object ReadJson(JsonReader reader, Type objectType, [System.Runtime.CompilerServices.Nullable(2)] object existingValue, JsonSerializer serializer)
{
Dictionary<string, string> dictionary = serializer.Deserialize<Dictionary<string, string>>(reader);
if (dictionary != null && existingValue != null) {
IDictionary<string, JsonSchema> dictionary2 = (IDictionary<string, JsonSchema>)existingValue;
dictionary2.Clear();
{
foreach (KeyValuePair<string, string> item in dictionary) {
JsonSchema jsonSchema = new JsonSchema();
((IJsonReferenceBase)jsonSchema).ReferencePath = item.Value;
dictionary2[item.Key] = jsonSchema;
}
return existingValue;
}
}
return existingValue;
}
public override void WriteJson(JsonWriter writer, [System.Runtime.CompilerServices.Nullable(2)] object value, JsonSerializer serializer)
{
IDictionary<string, JsonSchema> dictionary = value as IDictionary<string, JsonSchema>;
if (dictionary != null) {
Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
foreach (KeyValuePair<string, JsonSchema> item in dictionary) {
dictionary2[item.Key] = ((IJsonReferenceBase)item.Value).ReferencePath;
}
JObject jObject = JObject.FromObject(dictionary2, serializer);
writer.WriteToken(jObject.CreateReader());
} else
writer.WriteValue((string)null);
}
}
[JsonProperty("propertyName", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string PropertyName { get; set; }
[System.Runtime.CompilerServices.Nullable(1)]
[JsonProperty("mapping", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
[JsonConverter(typeof(DiscriminatorMappingConverter))]
[field: System.Runtime.CompilerServices.Nullable(1)]
public IDictionary<string, JsonSchema> Mapping {
[System.Runtime.CompilerServices.NullableContext(1)]
get;
} = new Dictionary<string, JsonSchema>();
[JsonIgnore]
public object JsonInheritanceConverter { get; set; }
[System.Runtime.CompilerServices.NullableContext(1)]
public void AddMapping(Type type, JsonSchema schema)
{
object jsonInheritanceConverter = JsonInheritanceConverter;
MethodInfo methodInfo = (jsonInheritanceConverter != null) ? jsonInheritanceConverter.GetType().GetRuntimeMethod("GetDiscriminatorValue", new Type[1] {
typeof(Type)
}) : null;
if (methodInfo != (MethodInfo)null) {
MethodInfo methodInfo2 = methodInfo;
object jsonInheritanceConverter2 = JsonInheritanceConverter;
object[] parameters = new Type[1] {
type
};
string key = (string)methodInfo2.Invoke(jsonInheritanceConverter2, parameters);
Mapping[key] = new JsonSchema {
Reference = schema.ActualSchema
};
} else
Mapping[type.Name] = new JsonSchema {
Reference = schema.ActualSchema
};
}
}
}