/// <summary>
        /// Determines whether this instance can generate a <see cref="JSchema"/> for the specified object type.
        /// </summary>
        /// <param name="context">The <see cref="Type"/> and associated information.</param>
        /// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns>
        public override bool CanGenerateSchema(JSchemaTypeGenerationContext context)
        {
            bool isNullable = ReflectionUtils.IsNullableType(context.ObjectType);
            Type t          = isNullable ? Nullable.GetUnderlyingType(context.ObjectType) : context.ObjectType;

            return(t.IsEnum());
        }
示例#2
0
        /// <summary>
        /// Gets a <see cref="JSchema"/> for a <see cref="Type"/>.
        /// </summary>
        /// <param name="context">The <see cref="Type"/> and associated information used to generate a <see cref="JSchema"/>.</param>
        /// <returns>The generated <see cref="JSchema"/>.</returns>
        public override JSchema GetSchema(JSchemaTypeGenerationContext context)
        {
            bool isNullable = ReflectionUtils.IsNullableType(context.ObjectType);
            Type t = isNullable ? Nullable.GetUnderlyingType(context.ObjectType) : context.ObjectType;

            if (!t.IsEnum())
            {
                return null;
            }

            JSchema schema = new JSchema
            {
                Type = JSchemaType.String
            };

            if (isNullable && context.Required != Required.Always)
            {
                schema.Type |= JSchemaType.Null;
            }

            string[] names = Enum.GetNames(t);

            foreach (string name in names)
            {
                string finalName = EnumUtils.ToEnumName(t, name, CamelCaseText);

                schema.Enum.Add(JValue.CreateString(finalName));
            }

            return schema;
        }
示例#3
0
        /// <summary>
        /// Gets a <see cref="JSchema"/> for a <see cref="Type"/>.
        /// </summary>
        /// <param name="context">The <see cref="Type"/> and associated information used to generate a <see cref="JSchema"/>.</param>
        /// <returns>The generated <see cref="JSchema"/>.</returns>
        public override JSchema GetSchema(JSchemaTypeGenerationContext context)
        {
            bool isNullable = ReflectionUtils.IsNullableType(context.ObjectType);
            Type t          = isNullable ? Nullable.GetUnderlyingType(context.ObjectType) : context.ObjectType;

            JSchema schema = new JSchema
            {
                Title       = context.SchemaTitle,
                Description = context.SchemaDescription,
                Type        = JSchemaType.String
            };

            if (isNullable && context.Required != Required.Always && context.Required != Required.DisallowNull)
            {
                schema.Type |= JSchemaType.Null;
                schema.Enum.Add(JValue.CreateNull());
            }

            string[] names = Enum.GetNames(t);

            foreach (string name in names)
            {
                string finalName = EnumUtils.ToEnumName(t, name, CamelCaseText);

                schema.Enum.Add(JValue.CreateString(finalName));
            }

            return(schema);
        }
            public override JSchema GetSchema(JSchemaTypeGenerationContext context)
            {
                // customize the generated schema for these types to have a format
                if (context.ObjectType == typeof(int))
                {
                    return CreateSchemaWithFormat(context.ObjectType, context.Required, "int32");
                }
                if (context.ObjectType == typeof(long))
                {
                    return CreateSchemaWithFormat(context.ObjectType, context.Required, "int64");
                }
                if (context.ObjectType == typeof(float))
                {
                    return CreateSchemaWithFormat(context.ObjectType, context.Required, "float");
                }
                if (context.ObjectType == typeof(double))
                {
                    return CreateSchemaWithFormat(context.ObjectType, context.Required, "double");
                }
                if (context.ObjectType == typeof(byte))
                {
                    return CreateSchemaWithFormat(context.ObjectType, context.Required, "byte");
                }
                if (context.ObjectType == typeof(DateTime) || context.ObjectType == typeof(DateTimeOffset))
                {
                    return CreateSchemaWithFormat(context.ObjectType, context.Required, "date-time");
                }

                // use default schema generation for all other types
                return null;
            }
        /// <summary>
        /// Gets a <see cref="JSchema"/> for a <see cref="Type"/>.
        /// </summary>
        /// <param name="context">The <see cref="Type"/> and associated information used to generate a <see cref="JSchema"/>.</param>
        /// <returns>The generated <see cref="JSchema"/>.</returns>
        public override JSchema GetSchema(JSchemaTypeGenerationContext context)
        {
            bool isNullable = ReflectionUtils.IsNullableType(context.ObjectType);
            Type t          = isNullable ? Nullable.GetUnderlyingType(context.ObjectType) : context.ObjectType;

            JSchema schema = new JSchema
            {
                Title       = context.SchemaTitle,
                Description = context.SchemaDescription,
                Type        = JSchemaType.String
            };

            if (isNullable && context.Required != Required.Always && context.Required != Required.DisallowNull)
            {
                schema.Type |= JSchemaType.Null;
                schema.Enum.Add(JValue.CreateNull());
            }

            object defaultValue = context.MemberProperty?.DefaultValue;

            if (defaultValue != null)
            {
                EnumUtils.TryToString(t, defaultValue, GetResolveNamingStrategy(), out string finalName);

                schema.Default = JToken.FromObject(finalName ?? defaultValue.ToString());
            }

            EnumInfo enumValues = EnumUtils.GetEnumValuesAndNames(t, GetResolveNamingStrategy());

            for (int i = 0; i < enumValues.Values.Length; i++)
            {
                string finalName = enumValues.ResolvedNames[i];

                schema.Enum.Add(JValue.CreateString(finalName));
            }

            return(schema);
        }
        private JSchema GenerateInternal(Type type, Required valueRequired, JsonProperty memberProperty, JsonContainerContract container)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            Type nonNullableType = ReflectionUtils.IsNullableType(type) ? Nullable.GetUnderlyingType(type) : type;

            Uri explicitId = GetTypeId(nonNullableType, true);

            JsonContract contract = ContractResolver.ResolveContract(type);

            var key = CreateKey(valueRequired, memberProperty, contract);

            switch (contract.ContractType)
            {
                case JsonContractType.Object:
                case JsonContractType.Array:
                case JsonContractType.Dictionary:
                    TypeSchema typeSchema = _typeSchemas.SingleOrDefault(s => s.Key.Equals(key));
                    if (typeSchema != null)
                        return typeSchema.Schema;
                    break;
            }

            JSchema schema = null;

            JSchemaGenerationProvider provider = ResolveTypeProvider(nonNullableType, memberProperty);
            if (provider != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, valueRequired, memberProperty, container, this);

                schema = provider.GetSchema(context);

                if (schema == null)
                    throw new JSchemaException("Could not get schema for type '{0}' from provider '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.FullName, provider.GetType().FullName));
            }
            
            if (_generationProviders != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, valueRequired, memberProperty, container, this);

                foreach (JSchemaGenerationProvider generationProvider in _generationProviders)
                {
                    schema = generationProvider.GetSchema(context);
                }
            }

            if (schema != null)
            {
                _typeSchemas.Add(new TypeSchema(key, schema));
                return schema;
            }

            schema = new JSchema();
            if (explicitId != null)
                schema.Id = explicitId;

            switch (contract.ContractType)
            {
                case JsonContractType.Object:
                case JsonContractType.Array:
                case JsonContractType.Dictionary:
                    _typeSchemas.Add(new TypeSchema(key, schema));
                    break;
            }

            return PopulateSchema(schema, contract, memberProperty, valueRequired);
        }
        private JSchema GenerateInternal(Type type, Required?valueRequired, JsonProperty memberProperty, JsonContainerContract container, JSchemaGenerationProvider currentGenerationProvider)
        {
            ValidationUtils.ArgumentNotNull(type, nameof(type));

            Type nonNullableType = ReflectionUtils.IsNullableType(type) ? Nullable.GetUnderlyingType(type) : type;

            Uri explicitId = GetTypeId(nonNullableType, true);

            JsonContract contract = _generator.ContractResolver.ResolveContract(type);

            Required resolvedRequired = valueRequired ?? _generator.DefaultRequired;

            TypeSchemaKey key = CreateKey(resolvedRequired, memberProperty, contract);

            if (ShouldReferenceType(contract))
            {
                TypeSchema typeSchema = GetCachedSchema(key);
                if (typeSchema != null)
                {
                    return(typeSchema.Schema);
                }
            }

            JSchema schema = null;

            JSchemaGenerationProvider provider = ResolveTypeProvider(nonNullableType, memberProperty);

            if (provider != null && provider != currentGenerationProvider)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, resolvedRequired, memberProperty, container, this);
                context.GenerationProvider = provider;

                schema = provider.GetSchema(context);

                if (schema == null)
                {
                    throw new JSchemaException("Could not get schema for type '{0}' from provider '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.FullName, provider.GetType().FullName));
                }
            }

            if (_generator._generationProviders != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, resolvedRequired, memberProperty, container, this);

                foreach (JSchemaGenerationProvider generationProvider in _generator._generationProviders)
                {
                    if (generationProvider != currentGenerationProvider && generationProvider.CanGenerateSchema(context))
                    {
                        context.GenerationProvider = generationProvider;

                        schema = generationProvider.GetSchema(context);
                        break;
                    }
                }
            }

            if (schema != null)
            {
                // check to see whether the generation provide had already generated the type recursively
                // and reuse that cached schema rather than duplicate
                TypeSchema typeSchema = GetCachedSchema(key);

                if (typeSchema != null)
                {
                    schema = typeSchema.Schema;
                }
                else
                {
                    if (ShouldReferenceType(contract))
                    {
                        _typeSchemas.Add(new TypeSchema(key, schema));
                    }
                }
            }
            else
            {
                schema = new JSchema();
                if (explicitId != null)
                {
                    schema.Id = explicitId;
                }

                if (ShouldReferenceType(contract))
                {
                    _typeSchemas.Add(new TypeSchema(key, schema));
                }

                PopulateSchema(schema, contract, memberProperty, resolvedRequired);
            }

            return(schema);
        }
示例#8
0
 /// <summary>
 /// Determines whether this instance can generate a <see cref="JSchema"/> for the specified object type.
 /// </summary>
 /// <param name="context">The <see cref="Type"/> and associated information.</param>
 /// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.</returns>
 public virtual bool CanGenerateSchema(JSchemaTypeGenerationContext context)
 {
     return(true);
 }
示例#9
0
 /// <summary>
 /// Gets a <see cref="JSchema"/> for a <see cref="Type"/>.
 /// </summary>
 /// <param name="context">The <see cref="Type"/> and associated information used to generate a <see cref="JSchema"/>.</param>
 /// <returns>The generated <see cref="JSchema"/> or <c>null</c> if type should not have a customized schema.</returns>
 public abstract JSchema GetSchema(JSchemaTypeGenerationContext context);
        private JSchema GenerateInternal(Type type, Required?valueRequired, JsonProperty memberProperty, JsonContainerContract container)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            Type nonNullableType = ReflectionUtils.IsNullableType(type) ? Nullable.GetUnderlyingType(type) : type;

            Uri explicitId = GetTypeId(nonNullableType, true);

            JsonContract contract = _generator.ContractResolver.ResolveContract(type);

            Required resolvedRequired = valueRequired ?? _generator.DefaultRequired;

            TypeSchemaKey key = CreateKey(resolvedRequired, memberProperty, contract);

            if (ShouldReferenceType(contract))
            {
                TypeSchema typeSchema = _typeSchemas.SingleOrDefault(s => s.Key.Equals(key));
                if (typeSchema != null)
                {
                    return(typeSchema.Schema);
                }
            }

            JSchema schema = null;

            JSchemaGenerationProvider provider = ResolveTypeProvider(nonNullableType, memberProperty);

            if (provider != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, resolvedRequired, memberProperty, container, _generator);

                schema = provider.GetSchema(context);

                if (schema == null)
                {
                    throw new JSchemaException("Could not get schema for type '{0}' from provider '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.FullName, provider.GetType().FullName));
                }
            }

            if (_generator._generationProviders != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, resolvedRequired, memberProperty, container, _generator);

                foreach (JSchemaGenerationProvider generationProvider in _generator._generationProviders)
                {
                    schema = generationProvider.GetSchema(context);
                }
            }

            if (schema != null)
            {
                if (ShouldReferenceType(contract))
                {
                    _typeSchemas.Add(new TypeSchema(key, schema));
                }
            }
            else
            {
                schema = new JSchema();
                if (explicitId != null)
                {
                    schema.Id = explicitId;
                }

                if (ShouldReferenceType(contract))
                {
                    _typeSchemas.Add(new TypeSchema(key, schema));
                }

                PopulateSchema(schema, contract, memberProperty, resolvedRequired);
            }

            return(schema);
        }
        private JSchema GenerateInternal(Type type, Required? valueRequired, JsonProperty memberProperty, JsonContainerContract container)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            Type nonNullableType = ReflectionUtils.IsNullableType(type) ? Nullable.GetUnderlyingType(type) : type;

            Uri explicitId = GetTypeId(nonNullableType, true);

            JsonContract contract = _generator.ContractResolver.ResolveContract(type);

            Required resolvedRequired = valueRequired ?? _generator.DefaultRequired;

            TypeSchemaKey key = CreateKey(resolvedRequired, memberProperty, contract);

            if (ShouldReferenceType(contract))
            {
                TypeSchema typeSchema = _typeSchemas.SingleOrDefault(s => s.Key.Equals(key));
                if (typeSchema != null)
                {
                    return typeSchema.Schema;
                }
            }

            JSchema schema = null;

            JSchemaGenerationProvider provider = ResolveTypeProvider(nonNullableType, memberProperty);
            if (provider != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, resolvedRequired, memberProperty, container, _generator);

                schema = provider.GetSchema(context);

                if (schema == null)
                {
                    throw new JSchemaException("Could not get schema for type '{0}' from provider '{1}'.".FormatWith(CultureInfo.InvariantCulture, type.FullName, provider.GetType().FullName));
                }
            }

            if (_generator._generationProviders != null)
            {
                JSchemaTypeGenerationContext context = new JSchemaTypeGenerationContext(type, resolvedRequired, memberProperty, container, _generator);

                foreach (JSchemaGenerationProvider generationProvider in _generator._generationProviders)
                {
                    schema = generationProvider.GetSchema(context);
                }
            }

            if (schema != null)
            {
                if (ShouldReferenceType(contract))
                {
                    _typeSchemas.Add(new TypeSchema(key, schema));
                }
            }
            else
            {
                schema = new JSchema();
                if (explicitId != null)
                {
                    schema.Id = explicitId;
                }

                if (ShouldReferenceType(contract))
                {
                    _typeSchemas.Add(new TypeSchema(key, schema));
                }

                PopulateSchema(schema, contract, memberProperty, resolvedRequired);
            }

            return schema;
        }
 /// <summary>
 /// Gets a <see cref="JSchema"/> for a <see cref="Type"/>.
 /// </summary>
 /// <param name="context">The <see cref="Type"/> and associated information used to generate a <see cref="JSchema"/>.</param>
 /// <returns>The generated <see cref="JSchema"/> or <c>null</c> if type should not have a customized schema.</returns>
 public abstract JSchema GetSchema(JSchemaTypeGenerationContext context);