#pragma warning disable 1998
            protected override async Task <IJsonReference> VisitJsonReferenceAsync(IJsonReference reference, string path, string typeNameHint)
#pragma warning restore 1998
            {
                if (reference.Reference != null)
                {
                    if (!_removeExternalReferences || reference.Reference.DocumentPath == null)
                    {
                        _schemaReferences[reference] = reference.Reference.ActualObject;
                    }
                    else
                    {
                        var externalReference     = reference.Reference;
                        var externalReferenceRoot = externalReference.FindParentDocument();
                        reference.ReferencePath = externalReference.DocumentPath + JsonPathUtilities.GetJsonPath(
                            externalReferenceRoot, externalReference, _contractResolver).TrimEnd('#');
                    }
                }
                else if (_removeExternalReferences && _rootObject != reference && reference.DocumentPath != null)
                {
                    throw new NotSupportedException("removeExternalReferences not supported");
                    //return new JsonSchema4 { ReferencePath = reference.DocumentPath };
                }

                return(reference);
            }
예제 #2
0
        private static void UpdateSchemaReferencePaths(object obj, HashSet <object> checkedObjects, Dictionary <JsonSchema4, JsonSchema4> schemaReferences)
        {
            if (obj == null || obj is string)
            {
                return;
            }

            var schema = obj as JsonSchema4;

            if (schema != null && schema.SchemaReference != null)
            {
                if (schema.SchemaReference.DocumentPath == null)
                {
                    schemaReferences[schema] = schema.SchemaReference.ActualSchema;
                }
                else
                {
                    // TODO: Improve performance here (like the rest)
                    var externalReference     = schema.SchemaReference;
                    var externalReferenceRoot = externalReference.FindRootParent();
                    schema.SchemaReferencePath = externalReference.DocumentPath + JsonPathUtilities.GetJsonPath(externalReferenceRoot, externalReference);
                }
            }

            if (obj is IDictionary)
            {
                foreach (var item in ((IDictionary)obj).Values.OfType <object>().ToList())
                {
                    UpdateSchemaReferencePaths(item, checkedObjects, schemaReferences);
                }
            }
            else if (obj is IEnumerable)
            {
                foreach (var item in ((IEnumerable)obj).OfType <object>().ToArray())
                {
                    UpdateSchemaReferencePaths(item, checkedObjects, schemaReferences);
                }
            }

            if (!(obj is JToken))
            {
                foreach (var member in ReflectionCache.GetPropertiesAndFields(obj.GetType()).Where(p =>
                                                                                                   p.CanRead && p.IsIndexer == false && p.MemberInfo is PropertyInfo &&
                                                                                                   p.CustomAttributes.JsonIgnoreAttribute == null))
                {
                    var value = member.GetValue(obj);
                    if (value != null)
                    {
                        if (!checkedObjects.Contains(value))
                        {
                            checkedObjects.Add(value);
                            UpdateSchemaReferencePaths(value, checkedObjects, schemaReferences);
                        }
                    }
                }
            }
        }
예제 #3
0
        /// <summary>Updates the <see cref="JsonSchema4.SchemaReferencePath" /> properties
        /// from the available <see cref="JsonSchema4.SchemaReference" /> properties.</summary>
        /// <param name="rootObject">The root object.</param>
        public static void UpdateSchemaReferencePaths(object rootObject)
        {
            var schemaReferences = new Dictionary <JsonSchema4, JsonSchema4>();

            UpdateSchemaReferencePaths(rootObject, new HashSet <object>(), schemaReferences);

            var searchedSchemas = schemaReferences.Select(p => p.Value).Distinct();
            var result          = JsonPathUtilities.GetJsonPaths(rootObject, searchedSchemas);

            foreach (var p in schemaReferences)
            {
                p.Key.SchemaReferencePath = result[p.Value];
            }
        }
        /// <summary>Updates the <see cref="IJsonReferenceBase.Reference" /> properties
        /// from the available <see cref="IJsonReferenceBase.Reference" /> properties.</summary>
        /// <param name="rootObject">The root object.</param>
        /// <param name="removeExternalReferences">Specifies whether to remove external references (otherwise they are inlined).</param>
        /// <param name="contractResolver">The contract resolver.</param>
        public static void UpdateSchemaReferencePaths(object rootObject, bool removeExternalReferences, IContractResolver contractResolver)
        {
            var schemaReferences = new Dictionary <IJsonReference, IJsonReference>();

            var updater = new JsonReferencePathUpdater(rootObject, schemaReferences, removeExternalReferences, contractResolver);

            updater.Visit(rootObject);

            var searchedSchemas = schemaReferences.Select(p => p.Value).Distinct();
            var result          = JsonPathUtilities.GetJsonPaths(rootObject, searchedSchemas, contractResolver);

            foreach (var p in schemaReferences)
            {
                p.Key.ReferencePath = result[p.Value];
            }
        }
        private static void UpdateSchemaReferencePaths(object root, object obj, HashSet <object> checkedObjects, ISchemaDefinitionAppender schemaDefinitionAppender)
        {
            if (obj == null || obj is string)
            {
                return;
            }

            var schema = obj as JsonSchema4;

            if (schema != null && schema.SchemaReference != null)
            {
                schema.SchemaReferencePath = JsonPathUtilities.GetJsonPath(root, schema.SchemaReference.ActualSchema, schemaDefinitionAppender);
            }

            if (obj is IDictionary)
            {
                foreach (var item in ((IDictionary)obj).Values.OfType <object>().ToList())
                {
                    UpdateSchemaReferencePaths(root, item, checkedObjects, schemaDefinitionAppender);
                }
            }
            else if (obj is IEnumerable)
            {
                foreach (var item in ((IEnumerable)obj).OfType <object>().ToArray())
                {
                    UpdateSchemaReferencePaths(root, item, checkedObjects, schemaDefinitionAppender);
                }
            }

            if (!(obj is JToken))
            {
                foreach (var property in ReflectionCache.GetProperties(obj.GetType()).Where(p =>
                                                                                            p.CanRead && p.IsIndexer == false && p.MemberInfo is PropertyInfo &&
                                                                                            p.CustomAttributes.JsonIgnoreAttribute == null))
                {
                    var value = property.GetValue(obj);
                    if (value != null)
                    {
                        if (!checkedObjects.Contains(value))
                        {
                            checkedObjects.Add(value);
                            UpdateSchemaReferencePaths(root, value, checkedObjects, schemaDefinitionAppender);
                        }
                    }
                }
            }
        }
        private static void UpdateSchemaReferencePaths(object root, object obj, List <object> checkedObjects)
        {
            if (obj == null || obj is string)
            {
                return;
            }

            var schema = obj as JsonSchema4;

            if (schema != null && schema.SchemaReference != null)
            {
                schema.SchemaReferencePath = JsonPathUtilities.GetJsonPath(root, schema.SchemaReference);
            }

            if (obj is IDictionary)
            {
                foreach (var item in ((IDictionary)obj).Values)
                {
                    UpdateSchemaReferencePaths(root, item, checkedObjects);
                }
            }
            else if (obj is IEnumerable)
            {
                foreach (var item in (IEnumerable)obj)
                {
                    UpdateSchemaReferencePaths(root, item, checkedObjects);
                }
            }
            else
            {
                foreach (var property in obj.GetType().GetRuntimeProperties().Where(p => p.CanRead && p.GetCustomAttribute <JsonIgnoreAttribute>() == null))
                {
                    var value = property.GetValue(obj);
                    if (value != null)
                    {
                        if (!checkedObjects.Contains(value))
                        {
                            checkedObjects.Add(value);

                            UpdateSchemaReferencePaths(root, value, checkedObjects);
                        }
                    }
                }
            }
        }
예제 #7
0
        private static void UpdateSchemaReferences(object root, object obj, HashSet <object> checkedObjects)
        {
            if (obj == null || obj is string)
            {
                return;
            }

            var schema = obj as JsonSchema4;

            if (schema != null && schema.SchemaReferencePath != null)
            {
                schema.SchemaReference = JsonPathUtilities.GetObjectFromJsonPath(root, schema.SchemaReferencePath);
            }

            if (obj is IDictionary)
            {
                foreach (var item in ((IDictionary)obj).Values)
                {
                    UpdateSchemaReferences(root, item, checkedObjects);
                }
            }
            else if (obj is IEnumerable)
            {
                foreach (var item in (IEnumerable)obj)
                {
                    UpdateSchemaReferences(root, item, checkedObjects);
                }
            }
            else
            {
                foreach (var property in ReflectionCache.GetProperties(obj.GetType()).Where(p => p.PropertyInfo.CanRead && p.CustomAttributes.JsonIgnoreAttribute == null))
                {
                    var value = property.PropertyInfo.GetValue(obj);
                    if (value != null)
                    {
                        if (!checkedObjects.Contains(value))
                        {
                            checkedObjects.Add(value);
                            UpdateSchemaReferences(root, value, checkedObjects);
                        }
                    }
                }
            }
        }
        private void LoadProperty <TSchemaType>(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver)
            where TSchemaType : JsonSchema4, new()
        {
            var propertyType            = property.PropertyType;
            var propertyTypeDescription = JsonObjectTypeDescription.FromType(propertyType);

            var attributes = property.GetCustomAttributes().ToArray();

            var jsonProperty = Generate <JsonProperty>(propertyType, schemaResolver);
            var propertyName = JsonPathUtilities.GetPropertyName(property);

            parentSchema.Properties.Add(propertyName, jsonProperty);

            var requiredAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RequiredAttribute");

            if (propertyTypeDescription.IsAlwaysRequired || requiredAttribute != null)
            {
                parentSchema.RequiredProperties.Add(property.Name);
            }

            jsonProperty.Description = GetDescription(property, attributes);

            dynamic regexAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RegularExpressionAttribute");

            if (regexAttribute != null)
            {
                jsonProperty.Pattern = regexAttribute.Pattern;
            }

            dynamic rangeAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RangeAttribute");

            if (rangeAttribute != null)
            {
                if (rangeAttribute.Minimum != null)
                {
                    jsonProperty.Minimum = rangeAttribute.Minimum;
                }
                if (rangeAttribute.Maximum != null)
                {
                    jsonProperty.Maximum = rangeAttribute.Maximum;
                }
            }
        }
        /// <summary>Updates the <see cref="IJsonReferenceBase.Reference" /> properties
        /// from the available <see cref="IJsonReferenceBase.Reference" /> properties.</summary>
        /// <param name="rootObject">The root object.</param>
        /// <param name="removeExternalReferences">Specifies whether to remove external references (otherwise they are inlined).</param>
        /// <param name="contractResolver">The contract resolver.</param>
        public static void UpdateSchemaReferencePaths(object rootObject, bool removeExternalReferences, IContractResolver contractResolver)
        {
            var schemaReferences = new Dictionary <IJsonReference, IJsonReference>();

            var updater = new JsonReferencePathUpdater(rootObject, schemaReferences, removeExternalReferences, contractResolver);

            updater.Visit(rootObject);

            var    searchedSchemas = schemaReferences.Select(p => p.Value).Distinct();
            var    result          = JsonPathUtilities.GetJsonPaths(rootObject, searchedSchemas, contractResolver);
            string referencePath   = null;

            foreach (var p in schemaReferences)
            {
                // iiQ Custom
                result.TryGetValue(p.Value, out referencePath);
                if (!string.IsNullOrEmpty(referencePath))
                {
                    p.Key.ReferencePath = referencePath;
                }

                // p.Key.ReferencePath = result[p.Value];
            }
        }
예제 #10
0
        private void LoadProperty <TSchemaType>(PropertyInfo property, TSchemaType parentSchema, ISchemaResolver schemaResolver)
            where TSchemaType : JsonSchema4, new()
        {
            var propertyType            = property.PropertyType;
            var propertyTypeDescription = JsonObjectTypeDescription.FromType(propertyType);

            var attributes = property.GetCustomAttributes().ToArray();

            if (attributes.All(a => !(a is JsonIgnoreAttribute)))
            {
                var jsonProperty = Generate <JsonProperty>(propertyType, property.GetCustomAttributes(), schemaResolver);
                var propertyName = JsonPathUtilities.GetPropertyName(property);
                parentSchema.Properties.Add(propertyName, jsonProperty);

                var jsonPropertyAttribute  = property.GetCustomAttribute <JsonPropertyAttribute>();
                var isJsonPropertyRequired = jsonPropertyAttribute != null && jsonPropertyAttribute.Required == Required.Always;

                var requiredAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RequiredAttribute");
                if (propertyTypeDescription.IsAlwaysRequired || requiredAttribute != null || isJsonPropertyRequired)
                {
                    parentSchema.RequiredProperties.Add(propertyName);
                }

                dynamic displayAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.DisplayAttribute");
                if (displayAttribute != null && displayAttribute.Name != null)
                {
                    jsonProperty.Title = displayAttribute.Name;
                }

                jsonProperty.Description = GetDescription(property, attributes);

                dynamic regexAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RegularExpressionAttribute");
                if (regexAttribute != null)
                {
                    jsonProperty.Pattern = regexAttribute.Pattern;
                }

                if (propertyTypeDescription.Type == JsonObjectType.Number || propertyTypeDescription.Type == JsonObjectType.Integer)
                {
                    dynamic rangeAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.RangeAttribute");
                    if (rangeAttribute != null)
                    {
                        if (rangeAttribute.Minimum != null)
                        {
                            jsonProperty.Minimum = rangeAttribute.Minimum;
                        }
                        if (rangeAttribute.Maximum != null)
                        {
                            jsonProperty.Maximum = rangeAttribute.Maximum;
                        }
                    }
                }

                dynamic minLengthAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.MinLengthAttribute");
                if (minLengthAttribute != null && minLengthAttribute.Length != null)
                {
                    if (propertyTypeDescription.Type == JsonObjectType.String)
                    {
                        jsonProperty.MinLength = minLengthAttribute.Length;
                    }
                    else if (propertyTypeDescription.Type == JsonObjectType.Array)
                    {
                        jsonProperty.MinItems = minLengthAttribute.Length;
                    }
                }

                dynamic maxLengthAttribute = TryGetAttribute(attributes, "System.ComponentModel.DataAnnotations.MaxLengthAttribute");
                if (maxLengthAttribute != null && maxLengthAttribute.Length != null)
                {
                    if (propertyTypeDescription.Type == JsonObjectType.String)
                    {
                        jsonProperty.MaxLength = maxLengthAttribute.Length;
                    }
                    else if (propertyTypeDescription.Type == JsonObjectType.Array)
                    {
                        jsonProperty.MaxItems = maxLengthAttribute.Length;
                    }
                }
            }
        }