JsonSchema4
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NJsonSchema.Collections;
using NJsonSchema.Generation;
using NJsonSchema.Infrastructure;
using NJsonSchema.Validation;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace NJsonSchema
{
public class JsonSchema4 : IDocumentPathProvider
{
private IDictionary<string, JsonProperty> _properties;
private IDictionary<string, JsonSchema4> _patternProperties;
private IDictionary<string, JsonSchema4> _definitions;
private ICollection<JsonSchema4> _allOf;
private ICollection<JsonSchema4> _anyOf;
private ICollection<JsonSchema4> _oneOf;
private JsonSchema4 _not;
private JsonSchema4 _item;
private ICollection<JsonSchema4> _items;
private bool _allowAdditionalItems = true;
private JsonSchema4 _additionalItemsSchema;
private bool _allowAdditionalProperties = true;
private JsonSchema4 _additionalPropertiesSchema;
private JsonSchema4 _schemaReference;
[JsonIgnore]
private JsonXmlObject _xmlObject;
private Lazy<object> _typeRaw;
public static string ToolchainVersion => IntrospectionExtensions.GetTypeInfo(typeof(JsonSchema4)).Assembly.GetName().Version.ToString();
[JsonIgnore]
public JsonSchema4 InheritedSchema {
get {
if (AllOf == null || AllOf.Count == 0 || HasSchemaReference)
return null;
if (AllOf.Count == 1)
return AllOf.First().ActualSchema;
if (AllOf.Any(delegate(JsonSchema4 s) {
if (s.HasSchemaReference)
return !s.ActualSchema.IsAnyType;
return false;
}))
return AllOf.First(delegate(JsonSchema4 s) {
if (s.HasSchemaReference)
return !s.ActualSchema.IsAnyType;
return false;
}).ActualSchema;
if (AllOf.Any(delegate(JsonSchema4 s) {
if (s.Type.HasFlag(JsonObjectType.Object))
return !s.ActualSchema.IsAnyType;
return false;
}))
return AllOf.First(delegate(JsonSchema4 s) {
if (s.Type.HasFlag(JsonObjectType.Object))
return !s.ActualSchema.IsAnyType;
return false;
}).ActualSchema;
return AllOf.First((JsonSchema4 s) => !s.ActualSchema.IsAnyType)?.ActualSchema;
}
}
[JsonIgnore]
public IReadOnlyCollection<JsonSchema4> AllInheritedSchemas {
get {
List<JsonSchema4> list = (InheritedSchema != null) ? new List<JsonSchema4> {
InheritedSchema
} : new List<JsonSchema4>();
return list.Concat(list.SelectMany((JsonSchema4 s) => s.AllInheritedSchemas)).ToList();
}
}
[JsonIgnore]
public string BaseDiscriminator {
get {
if (!string.IsNullOrEmpty(Discriminator))
return Discriminator;
string text = InheritedSchema?.ActualSchema.BaseDiscriminator;
if (!string.IsNullOrEmpty(text))
return text;
return null;
}
}
[JsonIgnore]
public IReadOnlyDictionary<string, JsonProperty> ActualProperties {
get {
List<KeyValuePair<string, JsonProperty>> source = Properties.Union((from s in AllOf
where s.ActualSchema != InheritedSchema
select s).SelectMany((JsonSchema4 s) => s.ActualSchema.ActualProperties)).ToList();
List<IGrouping<string, KeyValuePair<string, JsonProperty>>> source2 = (from p in source
group p by p.Key into g
where g.Count() > 1
select g).ToList();
if (source2.Any())
throw new InvalidOperationException("The properties " + string.Join(", ", (from g in source2
select g.Key) + " are defined multiple times."));
return new ReadOnlyDictionary<string, JsonProperty>(source.ToDictionary((KeyValuePair<string, JsonProperty> p) => p.Key, (KeyValuePair<string, JsonProperty> p) => p.Value));
}
}
[JsonProperty("$schema", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -99)]
public string SchemaVersion { get; set; }
[JsonProperty("id", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -98)]
public string Id { get; set; }
[JsonProperty("title", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -97)]
public string Title { get; set; }
[JsonProperty("description", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Description { get; set; }
[JsonIgnore]
public JsonObjectType Type { get; set; }
[JsonIgnore]
public string DocumentPath { get; set; }
[JsonProperty("schemaReferencePath", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal string SchemaReferencePath { get; set; }
[JsonIgnore]
public JsonSchema4 SchemaReference {
get {
return _schemaReference;
}
set {
if (_schemaReference != value) {
_schemaReference = value;
SchemaReferencePath = null;
if (value != null)
Type = JsonObjectType.None;
}
}
}
[JsonIgnore]
public virtual JsonSchema4 ActualSchema {
get {
return GetActualSchema(new List<JsonSchema4>());
}
}
[JsonIgnore]
public virtual JsonSchema4 ParentSchema { get; set; }
[JsonProperty("format", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Format { get; set; }
[JsonProperty("default", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public object Default { get; set; }
[JsonProperty("multipleOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public decimal? MultipleOf { get; set; }
[JsonProperty("maximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public decimal? Maximum { get; set; }
[JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool IsExclusiveMaximum { get; set; }
[JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public decimal? Minimum { get; set; }
[JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool IsExclusiveMinimum { get; set; }
[JsonProperty("maxLength", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int? MaxLength { get; set; }
[JsonProperty("minLength", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int? MinLength { get; set; }
[JsonProperty("pattern", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public string Pattern { get; set; }
[JsonProperty("maxItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MaxItems { get; set; }
[JsonProperty("minItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MinItems { get; set; }
[JsonProperty("uniqueItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public bool UniqueItems { get; set; }
[JsonProperty("maxProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MaxProperties { get; set; }
[JsonProperty("minProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public int MinProperties { get; set; }
[JsonIgnore]
public ICollection<object> Enumeration { get; set; }
[JsonIgnore]
public bool IsEnumeration {
get {
return Enumeration.Count > 0;
}
}
[JsonIgnore]
public ICollection<string> RequiredProperties { get; set; }
[JsonIgnore]
public IDictionary<string, JsonProperty> Properties {
get {
return _properties;
}
internal set {
if (_properties != value) {
RegisterProperties(_properties, value);
_properties = value;
}
}
}
[JsonProperty("xml", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonXmlObject Xml {
get {
return _xmlObject;
}
set {
_xmlObject = value;
if (_xmlObject != null)
_xmlObject.ParentSchema = this;
}
}
[JsonIgnore]
public IDictionary<string, JsonSchema4> PatternProperties {
get {
return _patternProperties;
}
internal set {
if (_patternProperties != value) {
RegisterSchemaDictionary(_patternProperties, value);
_patternProperties = value;
}
}
}
[JsonIgnore]
public JsonSchema4 Item {
get {
return _item;
}
set {
if (_item != value) {
_item = value;
if (_item != null) {
_item.ParentSchema = this;
Items.Clear();
}
}
}
}
[JsonIgnore]
public ICollection<JsonSchema4> Items {
get {
return _items;
}
internal set {
if (_items != value) {
RegisterSchemaCollection(_items, value);
_items = value;
if (_items != null)
Item = null;
}
}
}
[JsonProperty("not", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public JsonSchema4 Not {
get {
return _not;
}
set {
_not = value;
if (_not != null)
_not.ParentSchema = this;
}
}
[JsonIgnore]
public IDictionary<string, JsonSchema4> Definitions {
get {
return _definitions;
}
internal set {
if (_definitions != value) {
RegisterSchemaDictionary(_definitions, value);
_definitions = value;
}
}
}
[JsonProperty("resourceDefinitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> ResourceDefinitions { get; set; }
[JsonProperty("constraints", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> ConstraintsDefinitions { get; set; }
[JsonIgnore]
public ICollection<JsonSchema4> AllOf {
get {
return _allOf;
}
internal set {
if (_allOf != value) {
RegisterSchemaCollection(_allOf, value);
_allOf = value;
}
}
}
[JsonIgnore]
public ICollection<JsonSchema4> AnyOf {
get {
return _anyOf;
}
internal set {
if (_anyOf != value) {
RegisterSchemaCollection(_anyOf, value);
_anyOf = value;
}
}
}
[JsonIgnore]
public ICollection<JsonSchema4> OneOf {
get {
return _oneOf;
}
internal set {
if (_oneOf != value) {
RegisterSchemaCollection(_oneOf, value);
_oneOf = value;
}
}
}
[JsonIgnore]
public bool AllowAdditionalItems {
get {
return _allowAdditionalItems;
}
set {
if (_allowAdditionalItems != value) {
_allowAdditionalItems = value;
if (!_allowAdditionalItems)
AdditionalItemsSchema = null;
}
}
}
[JsonIgnore]
public JsonSchema4 AdditionalItemsSchema {
get {
return _additionalItemsSchema;
}
set {
if (_additionalItemsSchema != value) {
_additionalItemsSchema = value;
if (_additionalItemsSchema != null)
AllowAdditionalItems = true;
}
}
}
[JsonIgnore]
public bool AllowAdditionalProperties {
get {
return _allowAdditionalProperties;
}
set {
if (_allowAdditionalProperties != value) {
_allowAdditionalProperties = value;
if (!_allowAdditionalProperties)
AdditionalPropertiesSchema = null;
}
}
}
[JsonIgnore]
public JsonSchema4 AdditionalPropertiesSchema {
get {
return _additionalPropertiesSchema;
}
set {
if (_additionalPropertiesSchema != value) {
_additionalPropertiesSchema = value;
if (_additionalPropertiesSchema != null)
AllowAdditionalProperties = true;
}
}
}
[JsonIgnore]
public bool IsDictionary {
get {
if (Type.HasFlag(JsonObjectType.Object) && Properties.Count == 0) {
if (!AllowAdditionalProperties)
return PatternProperties.Any();
return true;
}
return false;
}
}
[JsonIgnore]
public bool IsAnyType {
get {
if ((Type.HasFlag(JsonObjectType.Object) || Type == JsonObjectType.None) && AllOf.Count == 0 && AnyOf.Count == 0 && OneOf.Count == 0 && Properties.Count == 0 && PatternProperties.Count == 0 && AllowAdditionalProperties && AdditionalPropertiesSchema == null && !MultipleOf.HasValue)
return !IsEnumeration;
return false;
}
}
[JsonIgnore]
public bool HasSchemaReference {
get {
if (SchemaReference == null)
return HasAllOfSchemaReference;
return true;
}
}
[JsonIgnore]
public bool HasAllOfSchemaReference {
get {
if (Type == JsonObjectType.None && AllOf.Count == 1 && AnyOf.Count == 0 && OneOf.Count == 0 && Properties.Count == 0 && PatternProperties.Count == 0 && AllowAdditionalProperties && AdditionalPropertiesSchema == null && !MultipleOf.HasValue)
return !IsEnumeration;
return false;
}
}
[JsonProperty("discriminator", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -95)]
public string Discriminator { get; set; }
[JsonIgnore]
public Collection<string> EnumerationNames { get; set; }
[JsonProperty("additionalItems", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object AdditionalItemsRaw {
get {
if (AdditionalItemsSchema != null)
return AdditionalItemsSchema;
if (!AllowAdditionalItems)
return false;
return null;
}
set {
if (value is bool)
AllowAdditionalItems = (bool)value;
else if (value != null) {
AdditionalItemsSchema = FromJsonWithoutReferenceHandling(value.ToString());
}
}
}
[JsonProperty("additionalProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object AdditionalPropertiesRaw {
get {
if (AdditionalPropertiesSchema != null)
return AdditionalPropertiesSchema;
if (!AllowAdditionalProperties)
return false;
return null;
}
set {
if (value is bool)
AllowAdditionalProperties = (bool)value;
else if (value != null) {
AdditionalPropertiesSchema = FromJsonWithoutReferenceHandling(value.ToString());
}
}
}
[JsonProperty("items", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal object ItemsRaw {
get {
if (Item != null)
return Item;
if (Items.Count > 0)
return Items;
return null;
}
set {
if (value is JArray)
Items = new ObservableCollection<JsonSchema4>(from t in (JArray)value
select FromJsonWithoutReferenceHandling(t.ToString()));
else if (value != null) {
Item = FromJsonWithoutReferenceHandling(value.ToString());
}
}
}
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -97)]
internal object TypeRaw {
get {
if (_typeRaw == null)
ResetTypeRaw();
return _typeRaw.Value;
}
set {
if (value is JArray)
Type = ((JArray)value).Aggregate(JsonObjectType.None, (JsonObjectType type, JToken token) => type | ConvertStringToJsonObjectType(token.ToString()));
else
Type = ConvertStringToJsonObjectType(value as string);
ResetTypeRaw();
}
}
[JsonProperty("required", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<string> RequiredPropertiesRaw {
get {
if (RequiredProperties == null || RequiredProperties.Count <= 0)
return null;
return RequiredProperties;
}
set {
RequiredProperties = value;
}
}
[JsonProperty("properties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> PropertiesRaw {
get {
if (Properties == null || Properties.Count <= 0)
return null;
return ((IEnumerable<KeyValuePair<string, JsonProperty>>)Properties).ToDictionary((Func<KeyValuePair<string, JsonProperty>, string>)((KeyValuePair<string, JsonProperty> p) => p.Key), (Func<KeyValuePair<string, JsonProperty>, JsonSchema4>)((KeyValuePair<string, JsonProperty> p) => p.Value));
}
set {
Properties = ((value != null) ? new ObservableDictionary<string, JsonProperty>(value.ToDictionary((KeyValuePair<string, JsonSchema4> p) => p.Key, (KeyValuePair<string, JsonSchema4> p) => JsonProperty.FromJsonSchema(p.Key, p.Value))) : new ObservableDictionary<string, JsonProperty>());
}
}
[JsonProperty("patternProperties", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> PatternPropertiesRaw {
get {
if (PatternProperties == null || PatternProperties.Count <= 0)
return null;
return PatternProperties.ToDictionary((KeyValuePair<string, JsonSchema4> p) => p.Key, (KeyValuePair<string, JsonSchema4> p) => p.Value);
}
set {
PatternProperties = ((value != null) ? new ObservableDictionary<string, JsonSchema4>(value.ToDictionary((KeyValuePair<string, JsonSchema4> p) => p.Key, (KeyValuePair<string, JsonSchema4> p) => p.Value)) : new ObservableDictionary<string, JsonSchema4>());
}
}
[JsonProperty("definitions", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal IDictionary<string, JsonSchema4> DefinitionsRaw {
get {
if (Definitions == null || Definitions.Count <= 0)
return null;
return Definitions;
}
set {
Definitions = ((value != null) ? new ObservableDictionary<string, JsonSchema4>(value) : new ObservableDictionary<string, JsonSchema4>());
}
}
[JsonProperty("x-enumNames", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
public Collection<string> EnumerationNamesRaw {
get {
if (EnumerationNames == null || EnumerationNames.Count <= 0)
return null;
return EnumerationNames;
}
set {
EnumerationNames = ((value != null) ? new ObservableCollection<string>(value) : new ObservableCollection<string>());
}
}
[JsonProperty("enum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<object> EnumerationRaw {
get {
if (Enumeration == null || Enumeration.Count <= 0)
return null;
return Enumeration;
}
set {
Enumeration = ((value != null) ? new ObservableCollection<object>(value) : new ObservableCollection<object>());
}
}
[JsonProperty("allOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> AllOfRaw {
get {
if (AllOf == null || AllOf.Count <= 0)
return null;
return AllOf;
}
set {
AllOf = ((value != null) ? new ObservableCollection<JsonSchema4>(value) : new ObservableCollection<JsonSchema4>());
}
}
[JsonProperty("anyOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> AnyOfRaw {
get {
if (AnyOf == null || AnyOf.Count <= 0)
return null;
return AnyOf;
}
set {
AnyOf = ((value != null) ? new ObservableCollection<JsonSchema4>(value) : new ObservableCollection<JsonSchema4>());
}
}
[JsonProperty("oneOf", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)]
internal ICollection<JsonSchema4> OneOfRaw {
get {
if (OneOf == null || OneOf.Count <= 0)
return null;
return OneOf;
}
set {
OneOf = ((value != null) ? new ObservableCollection<JsonSchema4>(value) : new ObservableCollection<JsonSchema4>());
}
}
[JsonExtensionData]
public IDictionary<string, object> ExtensionData { get; set; }
public JsonSchema4()
{
Initialize();
}
public static JsonSchema4 CreateAnySchema()
{
return new JsonSchema4();
}
public static TSchemaType CreateAnySchema<TSchemaType>() where TSchemaType : JsonSchema4, new
{
return new TSchemaType();
}
public static async Task<JsonSchema4> FromTypeAsync<TType>()
{
return await JsonSchema4.FromTypeAsync<TType>(new JsonSchemaGeneratorSettings()).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromTypeAsync(Type type)
{
return await FromTypeAsync(type, new JsonSchemaGeneratorSettings()).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromTypeAsync<TType>(JsonSchemaGeneratorSettings settings)
{
return await new JsonSchemaGenerator(settings).GenerateAsync(typeof(TType)).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromTypeAsync(Type type, JsonSchemaGeneratorSettings settings)
{
return await new JsonSchemaGenerator(settings).GenerateAsync(type).ConfigureAwait(false);
}
public static JsonSchema4 FromData(string data)
{
return new DataToJsonSchemaGenerator().Generate(data);
}
public static async Task<JsonSchema4> FromFileAsync(string filePath)
{
Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory = (JsonSchema4 schema) => new JsonReferenceResolver(new JsonSchemaResolver(schema, new JsonSchemaGeneratorSettings()));
return await FromFileAsync(filePath, referenceResolverFactory).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromFileAsync(string filePath, Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory)
{
return await FromJsonAsync(await DynamicApis.FileReadAllTextAsync(filePath), filePath, referenceResolverFactory).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromUrlAsync(string url)
{
Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory = (JsonSchema4 schema) => new JsonReferenceResolver(new JsonSchemaResolver(schema, new JsonSchemaGeneratorSettings()));
return await FromUrlAsync(url, referenceResolverFactory).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromUrlAsync(string url, Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory)
{
return await FromJsonAsync(await DynamicApis.HttpGetAsync(url), url, referenceResolverFactory).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromJsonAsync(string data)
{
return await FromJsonAsync(data, null).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromJsonAsync(string data, string documentPath)
{
Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory = (JsonSchema4 schema) => new JsonReferenceResolver(new JsonSchemaResolver(schema, new JsonSchemaGeneratorSettings()));
return await FromJsonAsync(data, documentPath, referenceResolverFactory).ConfigureAwait(false);
}
public static async Task<JsonSchema4> FromJsonAsync(string data, string documentPath, Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory)
{
data = JsonSchemaReferenceUtilities.ConvertJsonReferences(data);
JsonSchema4 schema = JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings {
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
schema.DocumentPath = documentPath;
JsonReferenceResolver jsonReferenceResolver = referenceResolverFactory(schema);
if (!string.IsNullOrEmpty(documentPath))
jsonReferenceResolver.AddDocumentReference(documentPath, schema);
await JsonSchemaReferenceUtilities.UpdateSchemaReferencesAsync(schema, jsonReferenceResolver).ConfigureAwait(false);
return schema;
}
internal static JsonSchema4 FromJsonWithoutReferenceHandling(string data)
{
return JsonConvert.DeserializeObject<JsonSchema4>(data, new JsonSerializerSettings {
ConstructorHandling = ConstructorHandling.Default,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});
}
public bool Inherits(JsonSchema4 schema)
{
schema = schema.ActualSchema;
if (InheritedSchema?.ActualSchema != schema)
return InheritedSchema?.Inherits(schema) ?? false;
return true;
}
private JsonSchema4 GetActualSchema(IList<JsonSchema4> checkedSchemas)
{
if (checkedSchemas.Contains(this))
throw new InvalidOperationException("Cyclic references detected.");
if (SchemaReferencePath != null && SchemaReference == null)
throw new InvalidOperationException("The schema reference path '" + SchemaReferencePath + "' has not been resolved.");
if (HasSchemaReference) {
checkedSchemas.Add(this);
if (HasAllOfSchemaReference)
return AllOf.First().GetActualSchema(checkedSchemas);
return SchemaReference.GetActualSchema(checkedSchemas);
}
return this;
}
public virtual bool IsNullable(NullHandling nullHandling)
{
if (Type.HasFlag(JsonObjectType.Null) && OneOf.Count == 0)
return true;
if (Type == JsonObjectType.None || Type.HasFlag(JsonObjectType.Null))
return OneOf.Any((JsonSchema4 o) => o.IsNullable(nullHandling));
return false;
}
public string ToJson()
{
JsonSchemaGeneratorSettings settings = new JsonSchemaGeneratorSettings();
return ToJson(settings);
}
public string ToJson(JsonSchemaGeneratorSettings settings)
{
string schemaVersion = SchemaVersion;
SchemaVersion = "http://json-schema.org/draft-04/schema#";
JsonSchemaReferenceUtilities.UpdateSchemaReferencePaths(this);
string result = JsonSchemaReferenceUtilities.ConvertPropertyReferences(JsonConvert.SerializeObject(this, Formatting.Indented));
SchemaVersion = schemaVersion;
return result;
}
public bool InheritsSchema(JsonSchema4 parentSchema)
{
if (parentSchema != null)
return ActualSchema.AllInheritedSchemas.Concat(new List<JsonSchema4> {
this
}).Any((JsonSchema4 s) => s.ActualSchema == parentSchema.ActualSchema);
return false;
}
public ICollection<ValidationError> Validate(string jsonData)
{
return new JsonSchemaValidator().Validate(jsonData, ActualSchema);
}
public ICollection<ValidationError> Validate(JToken token)
{
return new JsonSchemaValidator().Validate(token, ActualSchema);
}
public JsonSchema4 FindRootParent()
{
JsonSchema4 parentSchema = ParentSchema;
if (parentSchema == null)
return this;
while (parentSchema.ParentSchema != null) {
parentSchema = parentSchema.ParentSchema;
}
return parentSchema;
}
private static JsonObjectType ConvertStringToJsonObjectType(string value)
{
switch (value) {
case "array":
return JsonObjectType.Array;
case "boolean":
return JsonObjectType.Boolean;
case "integer":
return JsonObjectType.Integer;
case "number":
return JsonObjectType.Number;
case "null":
return JsonObjectType.Null;
case "object":
return JsonObjectType.Object;
case "string":
return JsonObjectType.String;
case "file":
return JsonObjectType.File;
default:
return JsonObjectType.None;
}
}
private void Initialize()
{
if (Items == null)
Items = new ObservableCollection<JsonSchema4>();
if (Properties == null)
Properties = new ObservableDictionary<string, JsonProperty>();
if (PatternProperties == null)
PatternProperties = new ObservableDictionary<string, JsonSchema4>();
if (Definitions == null)
Definitions = new ObservableDictionary<string, JsonSchema4>();
if (RequiredProperties == null)
RequiredProperties = new ObservableCollection<string>();
if (AllOf == null)
AllOf = new ObservableCollection<JsonSchema4>();
if (AnyOf == null)
AnyOf = new ObservableCollection<JsonSchema4>();
if (OneOf == null)
OneOf = new ObservableCollection<JsonSchema4>();
if (Enumeration == null)
Enumeration = new Collection<object>();
if (EnumerationNames == null)
EnumerationNames = new Collection<string>();
}
[OnDeserialized]
internal void OnDeserialized(StreamingContext ctx)
{
Initialize();
}
private void ResetTypeRaw()
{
_typeRaw = new Lazy<object>(delegate {
JsonObjectType[] array = (from v in (from Enum v in Enum.GetValues(Type.GetType())
where Type.HasFlag(v)
select v).OfType<JsonObjectType>()
where v != JsonObjectType.None
select v).ToArray();
if (array.Length > 1)
return new JArray(from f in array
select new JValue(f.ToString().ToLowerInvariant()));
if (array.Length == 1)
return new JValue(array[0].ToString().ToLowerInvariant());
return null;
});
}
private void RegisterProperties(IDictionary<string, JsonProperty> oldCollection, IDictionary<string, JsonProperty> newCollection)
{
if (oldCollection != null)
((ObservableDictionary<string, JsonProperty>)oldCollection).CollectionChanged -= InitializeSchemaCollection;
if (newCollection != null) {
((ObservableDictionary<string, JsonProperty>)newCollection).CollectionChanged += InitializeSchemaCollection;
InitializeSchemaCollection(newCollection, null);
}
}
private void RegisterSchemaDictionary(IDictionary<string, JsonSchema4> oldCollection, IDictionary<string, JsonSchema4> newCollection)
{
if (oldCollection != null)
((ObservableDictionary<string, JsonSchema4>)oldCollection).CollectionChanged -= InitializeSchemaCollection;
if (newCollection != null) {
((ObservableDictionary<string, JsonSchema4>)newCollection).CollectionChanged += InitializeSchemaCollection;
InitializeSchemaCollection(newCollection, null);
}
}
private void RegisterSchemaCollection(ICollection<JsonSchema4> oldCollection, ICollection<JsonSchema4> newCollection)
{
if (oldCollection != null)
((ObservableCollection<JsonSchema4>)oldCollection).CollectionChanged -= InitializeSchemaCollection;
if (newCollection != null) {
((ObservableCollection<JsonSchema4>)newCollection).CollectionChanged += InitializeSchemaCollection;
InitializeSchemaCollection(newCollection, null);
}
}
private void InitializeSchemaCollection(object sender, NotifyCollectionChangedEventArgs e)
{
if (sender is ObservableDictionary<string, JsonProperty>) {
foreach (KeyValuePair<string, JsonProperty> item in (ObservableDictionary<string, JsonProperty>)sender) {
item.Value.Name = item.Key;
item.Value.ParentSchema = this;
}
} else if (sender is ObservableCollection<JsonSchema4>) {
foreach (JsonSchema4 item2 in (ObservableCollection<JsonSchema4>)sender) {
item2.ParentSchema = this;
}
} else if (sender is ObservableDictionary<string, JsonSchema4>) {
foreach (JsonSchema4 value in ((ObservableDictionary<string, JsonSchema4>)sender).Values) {
value.ParentSchema = this;
}
}
}
}
}