<PackageReference Include="NJsonSchema" Version="9.10.72" />

JsonSchema4

A base class for describing a JSON schema.
using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NJsonSchema.Collections; using NJsonSchema.Generation; using NJsonSchema.Infrastructure; using NJsonSchema.References; 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 { [JsonConverter(typeof(ExtensionDataDeserializationConverter))] public class JsonSchema4 : JsonReferenceBase<JsonSchema4>, IDocumentPathProvider, IJsonReference, IJsonReferenceBase, IJsonExtensionObject { private const SchemaType SerializationSchemaType = SchemaType.JsonSchema; private static Lazy<PropertyRenameAndIgnoreSerializerContractResolver> ContractResolver = new Lazy<PropertyRenameAndIgnoreSerializerContractResolver>(() => CreateJsonSerializerContractResolver(SchemaType.JsonSchema)); 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 _dictionaryKey; private JsonSchema4 _item; private ICollection<JsonSchema4> _items; private bool _allowAdditionalItems = true; private JsonSchema4 _additionalItemsSchema; private bool _allowAdditionalProperties = true; private JsonSchema4 _additionalPropertiesSchema; [JsonIgnore] private JsonXmlObject _xmlObject; private Lazy<object> _typeRaw; public static string ToolchainVersion => IntrospectionExtensions.GetTypeInfo(typeof(JsonSchema4)).Assembly.GetName().Version + " (Newtonsoft.Json v" + IntrospectionExtensions.GetTypeInfo(typeof(JToken)).Assembly.GetName().Version + ")"; [JsonIgnore] public JsonSchema4 InheritedSchema { get { if (AllOf == null || AllOf.Count == 0 || HasReference) return null; if (AllOf.Count == 1) return AllOf.First().ActualSchema; if (AllOf.Any(delegate(JsonSchema4 s) { if (s.HasReference) return !s.ActualSchema.IsAnyType; return false; })) return AllOf.First(delegate(JsonSchema4 s) { if (s.HasReference) 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 OpenApiDiscriminator BaseDiscriminator { get { object openApiDiscriminator = DiscriminatorObject; if (openApiDiscriminator == null) { JsonSchema4 inheritedSchema = InheritedSchema; if (inheritedSchema == null) return null; openApiDiscriminator = inheritedSchema.ActualSchema.BaseDiscriminator; } return (OpenApiDiscriminator)openApiDiscriminator; } } [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 virtual string Description { get; set; } [JsonIgnore] public JsonObjectType Type { get; set; } [JsonIgnore] public JsonSchema4 ParentSchema { get { return Parent as JsonSchema4; } } [JsonIgnore] public virtual object Parent { 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; } [JsonIgnore] public decimal? ExclusiveMaximum { get; set; } [JsonIgnore] public bool IsExclusiveMaximum { get; set; } [JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public decimal? Minimum { get; set; } [JsonIgnore] public decimal? ExclusiveMinimum { get; set; } [JsonIgnore] 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; } [JsonProperty("x-deprecated", DefaultValueHandling = DefaultValueHandling.Ignore)] public bool IsDeprecated { get; set; } [JsonProperty("x-abstract", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public bool IsAbstract { get; set; } [JsonProperty("x-nullable", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public bool? IsNullableRaw { get; set; } [JsonProperty("x-example", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public object Example { get; set; } [JsonProperty("x-enumFlags", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public bool IsFlagEnumerable { 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; } [JsonProperty("x-dictionaryKey", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] public JsonSchema4 DictionaryKey { get { return _dictionaryKey; } set { _dictionaryKey = value; if (_dictionaryKey != null) _dictionaryKey.Parent = this; } } [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.Parent = 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.Parent = this; } } [JsonIgnore] public IDictionary<string, JsonSchema4> Definitions { get { return _definitions; } internal set { if (_definitions != value) { RegisterSchemaDictionary(_definitions, value); _definitions = value; } } } [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 IsArray { get { if (Type.HasFlag(JsonObjectType.Array)) { if (Items != null) return Items.Count == 0; return true; } return false; } } [JsonIgnore] public bool IsTuple { get { if (Type.HasFlag(JsonObjectType.Array)) { ICollection<JsonSchema4> items = Items; if (items == null) return false; return items.Any(); } return false; } } [JsonIgnore] public bool IsDictionary { get { if (Type.HasFlag(JsonObjectType.Object) && ActualProperties.Count == 0) { if (AdditionalPropertiesSchema == null) 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 && ActualProperties.Count == 0 && PatternProperties.Count == 0 && AllowAdditionalProperties && AdditionalPropertiesSchema == null && !MultipleOf.HasValue) return !IsEnumeration; return false; } } [JsonIgnore] public virtual JsonSchema4 ActualSchema { get { return GetActualSchema(new List<JsonSchema4>()); } } [JsonIgnore] public virtual JsonSchema4 ActualTypeSchema { get { return OneOf.FirstOrDefault((JsonSchema4 o) => !o.IsNullable(SchemaType.JsonSchema))?.ActualSchema ?? ActualSchema; } } [JsonIgnore] public bool HasReference { get { if (Reference == null) return HasAllOfSchemaReference; return true; } } [JsonIgnore] public bool HasAllOfSchemaReference { get { if (AllOf.Count == 1 && AllOf.Any((JsonSchema4 s) => s.HasReference) && Type == JsonObjectType.None && AnyOf.Count == 0 && OneOf.Count == 0 && Properties.Count == 0 && PatternProperties.Count == 0 && AllowAdditionalProperties && AdditionalPropertiesSchema == null && !MultipleOf.HasValue) return !IsEnumeration; return false; } } [JsonIgnore] [Obsolete("Use the Reference property instead.")] public JsonSchema4 SchemaReference { get { return Reference; } set { Reference = value; } } [JsonIgnore] [Obsolete("Use the HasReference property instead.")] public bool HasSchemaReference { get { return HasReference; } } [JsonIgnore] IJsonReference IJsonReference.ActualObject { get { return ActualSchema; } } [JsonIgnore] object IJsonReference.PossibleRoot { get { return Parent; } } [JsonIgnore] public override JsonSchema4 Reference { get { return base.Reference; } set { base.Reference = value; if (value != null) Type = JsonObjectType.None; } } [JsonExtensionData] public IDictionary<string, object> ExtensionData { get; set; } [JsonIgnore] public string Discriminator { get { return DiscriminatorObject?.PropertyName; } set { if (!string.IsNullOrEmpty(value)) DiscriminatorObject = new OpenApiDiscriminator { PropertyName = value }; else DiscriminatorObject = null; } } [JsonIgnore] public OpenApiDiscriminator DiscriminatorObject { get; set; } [JsonProperty("discriminator", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Order = -95)] internal object DiscriminatorRaw { get { if (JsonSchemaSerialization.CurrentSchemaType != SchemaType.Swagger2) return DiscriminatorObject; return Discriminator; } set { if (value is string) Discriminator = (string)value; else if (value != null) { DiscriminatorObject = ((JObject)value).ToObject<OpenApiDiscriminator>(); } } } [JsonIgnore] public Collection<string> EnumerationNames { get; set; } [JsonProperty("exclusiveMaximum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] internal object ExclusiveMaximumRaw { get { decimal? exclusiveMaximum = ExclusiveMaximum; if (!exclusiveMaximum.HasValue) { if (!IsExclusiveMaximum) return null; return true; } return exclusiveMaximum.GetValueOrDefault(); } set { if (value is bool) IsExclusiveMaximum = (bool)value; else if (value != null) { ExclusiveMaximum = Convert.ToDecimal(value); } } } [JsonProperty("exclusiveMinimum", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] internal object ExclusiveMinimumRaw { get { decimal? exclusiveMinimum = ExclusiveMinimum; if (!exclusiveMinimum.HasValue) { if (!IsExclusiveMinimum) return null; return true; } return exclusiveMinimum.GetValueOrDefault(); } set { if (value is bool) IsExclusiveMinimum = (bool)value; else if (value != null) { ExclusiveMinimum = Convert.ToDecimal(value); } } } [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>()); } } 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 FromSampleJson(string data) { return new SampleJsonSchemaGenerator().Generate(data); } public static async Task<JsonSchema4> FromFileAsync(string filePath) { Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory = JsonReferenceResolver.CreateJsonReferenceResolverFactory(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 = JsonReferenceResolver.CreateJsonReferenceResolverFactory(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 = JsonReferenceResolver.CreateJsonReferenceResolverFactory(new JsonSchemaGeneratorSettings()); return await FromJsonAsync(data, documentPath, referenceResolverFactory).ConfigureAwait(false); } public static async Task<JsonSchema4> FromJsonAsync(string data, string documentPath, Func<JsonSchema4, JsonReferenceResolver> referenceResolverFactory) { return await JsonSchemaSerialization.FromJsonAsync(data, SchemaType.JsonSchema, documentPath, referenceResolverFactory, ContractResolver.Value).ConfigureAwait(false); } 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; } public virtual bool IsNullable(SchemaType schemaType) { if (schemaType == SchemaType.OpenApi3 && IsNullableRaw.HasValue) return IsNullableRaw.Value; if (IsEnumeration && Enumeration.Contains(null)) return true; 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(schemaType)); return false; } public string ToJson() { return ToJson(Formatting.Indented); } public string ToJson(Formatting formatting) { string schemaVersion = SchemaVersion; SchemaVersion = "http://json-schema.org/draft-04/schema#"; string result = JsonSchemaSerialization.ToJson(this, SchemaType.JsonSchema, ContractResolver.Value, formatting); 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); } 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>(); } private JsonSchema4 GetActualSchema(IList<JsonSchema4> checkedSchemas) { if (checkedSchemas.Contains(this)) throw new InvalidOperationException("Cyclic references detected."); if (((IJsonReferenceBase)this).ReferencePath != null && Reference == null) throw new InvalidOperationException("The schema reference path '" + ((IJsonReferenceBase)this).ReferencePath + "' has not been resolved."); if (HasReference) { checkedSchemas.Add(this); if (HasAllOfSchemaReference) return AllOf.First().GetActualSchema(checkedSchemas); return Reference.GetActualSchema(checkedSchemas); } return this; } public static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSerializerContractResolver(SchemaType schemaType) { IgnoreEmptyCollectionsContractResolver ignoreEmptyCollectionsContractResolver = new IgnoreEmptyCollectionsContractResolver(); switch (schemaType) { case SchemaType.OpenApi3: ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonProperty), "x-readOnly", "readOnly"); ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonProperty), "x-writeOnly", "writeOnly"); ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonSchema4), "x-nullable", "nullable"); ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonSchema4), "x-example", "example"); break; case SchemaType.Swagger2: ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonProperty), "x-readOnly", "readOnly"); ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonSchema4), "x-example", "example"); break; default: ignoreEmptyCollectionsContractResolver.RenameProperty(typeof(JsonProperty), "x-readOnly", "readonly"); break; } return ignoreEmptyCollectionsContractResolver; } [OnDeserialized] internal void OnDeserialized(StreamingContext ctx) { Initialize(); } private void ResetTypeRaw() { _typeRaw = new Lazy<object>(delegate { JsonObjectType[] array = (from v in Enum.GetValues(Type.GetType()).OfType<JsonObjectType>() where Type.HasFlag(v) 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.Parent = this; } } else if (sender is ObservableCollection<JsonSchema4>) { foreach (JsonSchema4 item2 in (ObservableCollection<JsonSchema4>)sender) { item2.Parent = this; } } else if (sender is ObservableDictionary<string, JsonSchema4>) { foreach (JsonSchema4 value in ((ObservableDictionary<string, JsonSchema4>)sender).Values) { value.Parent = this; } } } } }