Esempio n. 1
0
 void IAttributeHandler.AddConstraints(SchemaGeneratorContext context)
 {
     if (context.Attributes.Any(x => x.GetType() == typeof(AttributeWithIndirectHandler)))
     {
         context.Intents.Add(new Intents.MaxLengthIntent(AttributeWithIndirectHandler.MaxLength));
     }
 }
        /// <summary>
        /// Replaces one context with another.
        /// </summary>
        /// <param name="hashCode">The hashcode of the context to replace.</param>
        /// <param name="newContext">The new context.</param>
        public void Replace(int hashCode, SchemaGeneratorContext newContext)
        {
            var hc = Context.GetHashCode();

            if (hc == hashCode)
            {
                Context = newContext;
            }
        }
        public void AddConstraints(SchemaGeneratorContext context)
        {
            context.Intents.Add(new TypeIntent(SchemaValueType.Object));

            var valueType    = context.Type.GenericTypeArguments[1];
            var valueContext = SchemaGenerationContextCache.Get(valueType, context.Attributes);

            context.Intents.Add(new AdditionalPropertiesIntent(valueContext));
        }
Esempio n. 4
0
        public void AddConstraints(SchemaGeneratorContext context)
        {
            context.Intents.Add(new TypeIntent(SchemaValueType.Object));

            var props                = new Dictionary <string, SchemaGeneratorContext>();
            var required             = new List <string>();
            var propertiesToGenerate = context.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                       .Where(p => p.CanRead && p.CanWrite);
            var fieldsToGenerate           = context.Type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            var hiddenPropertiesToGenerate = context.Type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
                                             .Where(p => p.GetCustomAttribute <JsonIncludeAttribute>() != null);
            var hiddenFieldsToGenerate = context.Type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                                         .Where(p => p.GetCustomAttribute <JsonIncludeAttribute>() != null);
            var membersToGenerate = propertiesToGenerate.Cast <MemberInfo>()
                                    .Concat(fieldsToGenerate)
                                    .Concat(hiddenPropertiesToGenerate)
                                    .Concat(hiddenFieldsToGenerate)
                                    .OrderBy(m => m.Name);

            foreach (var member in membersToGenerate)
            {
                var memberAttributes = member.GetCustomAttributes().ToList();
                var ignoreAttribute  = memberAttributes.OfType <JsonIgnoreAttribute>().FirstOrDefault();
                if (ignoreAttribute != null)
                {
                    continue;
                }

                var memberContext = SchemaGenerationContextCache.Get(member.GetMemberType(), memberAttributes, context.Configuration);

                var name          = member.Name;
                var nameAttribute = memberAttributes.OfType <JsonPropertyNameAttribute>().FirstOrDefault();
                if (nameAttribute != null)
                {
                    name = nameAttribute.Name;
                }

                if (memberAttributes.OfType <ObsoleteAttribute>().Any())
                {
                    memberContext.Intents.Add(new DeprecatedIntent(true));
                }

                props.Add(name, memberContext);

                if (memberAttributes.OfType <RequiredAttribute>().Any())
                {
                    required.Add(name);
                }
            }

            context.Intents.Add(new PropertiesIntent(props));
            if (required.Any())
            {
                context.Intents.Add(new RequiredIntent(required));
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Replaces one context with another.
 /// </summary>
 /// <param name="hashCode">The hashcode of the context to replace.</param>
 /// <param name="newContext">The new context.</param>
 public void Replace(int hashCode, SchemaGeneratorContext newContext)
 {
     foreach (var property in Properties.ToList())
     {
         var hc = property.Value.GetHashCode();
         if (hc == hashCode)
         {
             Properties[property.Key] = newContext;
         }
     }
 }
        public void AddConstraints(SchemaGeneratorContext context)
        {
            var underlyingType = Nullable.GetUnderlyingType(context.Type);

            if (underlyingType == null)
            {
                return;
            }
            var underlyingContext = SchemaGenerationContextCache.Get(underlyingType, context.Attributes, context.Configuration);

            context.Intents.AddRange(underlyingContext.Intents);
        }
        public void AddConstraints(SchemaGeneratorContext context)
        {
            context.Intents.Add(new TypeIntent(SchemaValueType.Object));

            var props                = new Dictionary <string, SchemaGeneratorContext>();
            var required             = new List <string>();
            var propertiesToGenerate = context.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                       .Where(p => p.CanRead && p.CanWrite);

            foreach (var property in propertiesToGenerate)
            {
                var propAttributes  = property.GetCustomAttributes().ToList();
                var ignoreAttribute = propAttributes.OfType <JsonIgnoreAttribute>().FirstOrDefault();
                if (ignoreAttribute != null)
                {
                    continue;
                }

                var propContext = SchemaGenerationContextCache.Get(property.PropertyType, propAttributes);

                var name          = property.Name;
                var nameAttribute = propAttributes.OfType <JsonPropertyNameAttribute>().FirstOrDefault();
                if (nameAttribute != null)
                {
                    name = nameAttribute.Name;
                }

                if (propAttributes.OfType <ObsoleteAttribute>().Any())
                {
                    propContext.Intents.Add(new DeprecatedIntent(true));
                }

                props.Add(name, propContext);

                if (propAttributes.OfType <RequiredAttribute>().Any())
                {
                    required.Add(property.Name);
                }
            }

            context.Intents.Add(new PropertiesIntent(props));
            if (required.Any())
            {
                context.Intents.Add(new RequiredIntent(required));
            }
        }
Esempio n. 8
0
        public void AddConstraints(SchemaGeneratorContext context)
        {
            context.Intents.Add(new TypeIntent(SchemaValueType.Array));

            Type itemType = null;

            if (context.Type.IsGenericType)
            {
                itemType = context.Type.GetGenericArguments().First();
            }
            else if (context.Type.IsArray)
            {
                itemType = context.Type.GetElementType();
            }

            if (itemType == null)
            {
                return;
            }
            var itemContext = SchemaGenerationContextCache.Get(itemType, context.Attributes);

            context.Intents.Add(new ItemsIntent(itemContext));
        }
Esempio n. 9
0
        public void Run(SchemaGeneratorContext context)
        {
            var typeIntent = context.Intents.OfType <TypeIntent>().FirstOrDefault();

            if (typeIntent == null)
            {
                return;                                 // shouldn't happen because of ShouldRun(), but including just in case.
            }
            var nullableAttribute   = context.Attributes.OfType <NullableAttribute>().FirstOrDefault();
            var nullabilityOverride = nullableAttribute?.IsNullable;

            if (nullabilityOverride.HasValue)
            {
                if (nullabilityOverride.Value)
                {
                    typeIntent.Type |= SchemaValueType.Null;
                }
                else
                {
                    typeIntent.Type &= ~SchemaValueType.Null;
                }
                return;
            }

            if (context.Configuration.Nullability.HasFlag(Nullability.AllowForNullableValueTypes) &&
                context.Type.IsGenericType && context.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                typeIntent.Type |= SchemaValueType.Null;
            }

            if (context.Configuration.Nullability.HasFlag(Nullability.AllowForReferenceTypes) &&
                // see https://stackoverflow.com/a/16578846/878701
                !context.Type.IsValueType)
            {
                typeIntent.Type |= SchemaValueType.Null;
            }
        }
Esempio n. 10
0
 public void AddConstraints(SchemaGeneratorContext context)
 {
     context.Intents.Add(new TypeIntent(SchemaValueType.String));
     context.Intents.Add(new FormatIntent(Formats.JsonPointer));
 }
Esempio n. 11
0
 public void AddConstraints(SchemaGeneratorContext context)
 {
     context.Intents.Add(new TypeIntent(SchemaValueType.String));
 }
Esempio n. 12
0
        public void AddConstraints(SchemaGeneratorContext context)
        {
            context.Intents.Add(new TypeIntent(SchemaValueType.Object));

            var props                = new Dictionary <string, SchemaGeneratorContext>();
            var required             = new List <string>();
            var propertiesToGenerate = context.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                       .Where(p => p.CanRead && p.CanWrite);
            var fieldsToGenerate           = context.Type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            var hiddenPropertiesToGenerate = context.Type.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance)
                                             .Where(p => p.GetCustomAttribute <JsonIncludeAttribute>() != null);
            var hiddenFieldsToGenerate = context.Type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
                                         .Where(p => p.GetCustomAttribute <JsonIncludeAttribute>() != null);
            var membersToGenerate = propertiesToGenerate.Cast <MemberInfo>()
                                    .Concat(fieldsToGenerate)
                                    .Concat(hiddenPropertiesToGenerate)
                                    .Concat(hiddenFieldsToGenerate);

            membersToGenerate = context.Configuration.PropertyOrder switch
            {
                PropertyOrder.AsDeclared => membersToGenerate.OrderBy(m => m, context.DeclarationOrderComparer),
                PropertyOrder.ByName => membersToGenerate.OrderBy(m => m.Name),
                _ => membersToGenerate
            };

            foreach (var member in membersToGenerate)
            {
                var memberAttributes = member.GetCustomAttributes().ToList();
#pragma warning disable 8600 // Assigning null to non-null
                // ReSharper disable once AssignNullToNotNullAttribute
                var ignoreAttribute = (Attribute)memberAttributes.OfType <JsonIgnoreAttribute>().FirstOrDefault() ??
                                      memberAttributes.OfType <JsonExcludeAttribute>().FirstOrDefault();
#pragma warning restore 8600
                if (ignoreAttribute != null)
                {
                    continue;
                }

                var memberContext = SchemaGenerationContextCache.Get(member.GetMemberType(), memberAttributes, context.Configuration);

                var name          = context.Configuration.PropertyNamingMethod(member.Name);
                var nameAttribute = memberAttributes.OfType <JsonPropertyNameAttribute>().FirstOrDefault();
                if (nameAttribute != null)
                {
                    name = nameAttribute.Name;
                }

                if (memberAttributes.OfType <ObsoleteAttribute>().Any())
                {
                    memberContext.Intents.Add(new DeprecatedIntent(true));
                }

                props.Add(name, memberContext);

                if (memberAttributes.OfType <RequiredAttribute>().Any())
                {
                    required.Add(name);
                }
            }


            if (props.Count > 0)
            {
                context.Intents.Add(new PropertiesIntent(props));

                if (required.Count > 0)
                {
                    context.Intents.Add(new RequiredIntent(required));
                }
            }
        }
    }
 /// <summary>
 /// Creates a new <see cref="AdditionalPropertiesIntent"/> instance.
 /// </summary>
 /// <param name="context">The context.</param>
 public AdditionalPropertiesIntent(SchemaGeneratorContext context)
 {
     Context = context;
 }
Esempio n. 14
0
 /// <summary>
 /// Creates a new <see cref="ItemsIntent"/> instance.
 /// </summary>
 /// <param name="context">The context.</param>
 public ItemsIntent(SchemaGeneratorContext context)
 {
     Context = context;
 }
Esempio n. 15
0
 public bool ShouldRun(SchemaGeneratorContext context)
 {
     return(context.Intents.OfType <TypeIntent>().Any());
 }
 /// <summary>
 /// Creates a new <see cref="PropertyNamesIntent"/> instance.
 /// </summary>
 /// <param name="context">The context.</param>
 public PropertyNamesIntent(SchemaGeneratorContext context)
 {
     Context = context;
 }
Esempio n. 17
0
        public void AddConstraints(SchemaGeneratorContext context)
        {
            var values = Enum.GetNames(context.Type).ToList();

            context.Intents.Add(new EnumIntent(values));
        }
Esempio n. 18
0
 public void Run(SchemaGeneratorContext context)
 {
     context.Intents.Add(new ReadOnlyIntent(true));
 }
Esempio n. 19
0
 public bool ShouldRun(SchemaGeneratorContext context)
 {
     return(context.Type.GetProperties().Length % 2 == 1);
 }