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));
        }
예제 #2
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));
            }
        }
        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));
            }
        }
예제 #5
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));
        }
예제 #6
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));
                }
            }
        }
    }