/// <summary>
        /// Generates a schema from a CLR type.
        /// </summary>
        /// <param name="builder">The schema builder.</param>
        /// <param name="type">The type to generate.</param>
        /// <returns>The schema builder (for fluent syntax support).</returns>
        public static JsonSchemaBuilder FromType(this JsonSchemaBuilder builder, Type type)
        {
            var context = new SchemaGeneratorContext(type, new List <Attribute>());

            context.GenerateIntents();

            context.Optimize();

            context.Apply(builder);

            return(builder);
        }
Exemple #2
0
        /// <summary>
        /// Gets or creates a <see cref="SchemaGeneratorContext"/> based on the given
        /// type and attribute set.
        /// </summary>
        /// <param name="type">The type to generate.</param>
        /// <param name="attributes">The attribute set on the property.</param>
        /// <returns>
        /// A generation context, from the cache if one exists with the specified
        /// type and attribute set; otherwise a new one.  New contexts are automatically
        /// cached.
        /// </returns>
        /// <remarks>
        /// Use this in your generator if it needs to create keywords with subschemas.
        /// </remarks>
        public static SchemaGeneratorContext Get(Type type, List <Attribute> attributes)
        {
            var hash = attributes?.GetAttributeSetHashCode() ?? 0;
            var key  = new Key(type, hash);

            if (!Cache.TryGetValue(key, out var context))
            {
                context     = new SchemaGeneratorContext(type, attributes);
                _cache[key] = context;
                context.GenerateIntents();
            }

            return(context);
        }