Example #1
0
        /// <summary>Appends the schema to the root object.</summary>
        /// <param name="objectToAppend">The object to append.</param>
        /// <exception cref="InvalidOperationException">Could not find the JSON path of a child object.</exception>
        public void Append(JsonSchema4 objectToAppend)
        {
            var rootSchema = RootObject as JsonSchema4;

            if (rootSchema != null && objectToAppend != null)
            {
                var typeName = objectToAppend.GetTypeName(_typeNameGenerator, string.Empty);
                if (!string.IsNullOrEmpty(typeName) && !rootSchema.Definitions.ContainsKey(typeName))
                {
                    rootSchema.Definitions[typeName] = objectToAppend;
                }
                else
                {
                    rootSchema.Definitions["ref_" + Guid.NewGuid().ToString().Replace("-", "_")] = objectToAppend;
                }
            }
            else
            {
                throw new InvalidOperationException("Could not find the JSON path of a child object.");
            }
        }
        /// <summary>Appends the schema to the root object.</summary>
        /// <param name="schema">The schema to append.</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <exception cref="ArgumentNullException"><paramref name="schema"/> is <see langword="null"/></exception>
        public virtual void AppendSchema(JsonSchema4 schema, string typeNameHint)
        {
            if (schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }

            if (_rootSchema == null)
            {
                _rootSchema = schema;
            }
            else if (schema != _rootSchema)
            {
                var typeName = schema.GetTypeName(_settings.TypeNameGenerator, typeNameHint);
                if (!string.IsNullOrEmpty(typeName) && !_rootSchema.Definitions.ContainsKey(typeName))
                {
                    _rootSchema.Definitions[typeName] = schema;
                }
                else
                {
                    _rootSchema.Definitions["ref_" + Guid.NewGuid().ToString().Replace("-", "_")] = schema;
                }
            }
        }