Example #1
0
        private IDictionary <string, JsonSchemaProperty> GetActualProperties(bool includeInherited)
#endif
        {
            var ignoredSchema = includeInherited ? null : InheritedSchema;
            var properties    = Properties
                                .Union(AllOf.Where(s => s.ActualSchema != ignoredSchema).SelectMany(s => s.ActualSchema.GetActualProperties(false)))
                                .Union(AnyOf.Where(s => s.ActualSchema != ignoredSchema).SelectMany(s => s.ActualSchema.GetActualProperties(true)))
                                .Union(OneOf.Where(s => s.ActualSchema != ignoredSchema).SelectMany(s => s.ActualSchema.GetActualProperties(true)))
                                .ToList();

            // Collapse all duplicated properties, checking that the duplicated ones are compatible
            var duplicatedProperties = properties
                                       .GroupBy(p => p.Key)
                                       .Where(g => g.Count() > 1)
                                       .Select(g => g.ToList())
                                       .ToList();
            var invalidDuplicates = new List <string>();

            foreach (var duped in duplicatedProperties)
            {
                // Make sure all the properties are the same type as each other so they are compatible primitive types
                var toKeep = duped[0].Value;
                if (duped.Any(dupe => dupe.Value.Type != toKeep.Type))
                {
                    invalidDuplicates.Add(duped[0].Key);
                    continue;
                }

                // All good, so remove the duplicates here
                foreach (var c in duped.Skip(1))
                {
                    properties.Remove(c);
                }
            }

            if (invalidDuplicates.Count > 0)
            {
                throw new InvalidOperationException("The properties " + string.Join(", ", invalidDuplicates.Select(key => "'" + key + "'")) + " are defined multiple times and are not the same type.");
            }

#if !LEGACY
            return(new ReadOnlyDictionary <string, JsonSchemaProperty>(properties.ToDictionary(p => p.Key, p => p.Value)));
#else
            return(new Dictionary <string, JsonSchemaProperty>(properties.ToDictionary(p => p.Key, p => p.Value)));
#endif
        }