private IEnumerable<PropertyDocumentation> CreatePropertyDocumentation(DtoDocumentationMetadata dtoDocumentationMetadata, String propertyPrefix)
{
if (propertyPrefix == null)
{
propertyPrefix = String.Empty;
}
IList<PropertyDocumentation> propertyDocumentation = new List<PropertyDocumentation>();
var propertyDescriptors = TypeDescriptor.GetProperties(dtoDocumentationMetadata.Type)
.Cast<PropertyDescriptor>()
.Where(pi => pi.PropertyType.GetCustomAttributes(true)
.All(attribute =>
!(attribute is NonSerializedAttribute) &&
!(attribute is IgnoreDataMemberAttribute)))
.Select(pi => pi)
.ToList();
foreach (var propertyDescriptor in propertyDescriptors)
{
var documentedProperty =
dtoDocumentationMetadata.PropertyDocumentationMetadata
.SingleOrDefault(
metadata =>
metadata.Property.Name.Equals(propertyDescriptor.Name) &&
metadata.Property.PropertyType.Equals(propertyDescriptor.PropertyType));
if (!TypeHelper.CanConvertFromString(propertyDescriptor.PropertyType) &&
_WebApiDocumentationMetadata.DtoDocumentation.ContainsKey(propertyDescriptor.PropertyType))
{
propertyDocumentation.Add(
new PropertyDocumentation(
GetPropertyNamePrefix(propertyDescriptor, propertyPrefix),
GetProperyTypeName(propertyDescriptor.PropertyType),
_WebApiDocumentationMetadata.DtoDocumentation[propertyDescriptor.PropertyType].Summary));
propertyDocumentation.AddRange(
CreatePropertyDocumentation(
_WebApiDocumentationMetadata.DtoDocumentation[propertyDescriptor.PropertyType],
String.Format("{0}{1}{2}", propertyPrefix, String.IsNullOrWhiteSpace(propertyPrefix) ? String.Empty : ".", propertyDescriptor.Name)));
}
else if (!TypeHelper.CanConvertFromString(propertyDescriptor.PropertyType) &&
GetEnumerableType(propertyDescriptor.PropertyType) != null &&
(propertyDescriptor.PropertyType.IsGenericType &&
_WebApiDocumentationMetadata.DtoDocumentation.ContainsKey(propertyDescriptor.PropertyType.GetGenericArguments().First())))
{
var genericPropertyType = propertyDescriptor.PropertyType.GetGenericArguments().First();
propertyDocumentation.Add(
new PropertyDocumentation(
GetPropertyNamePrefix(propertyDescriptor, propertyPrefix),
GetProperyTypeName(propertyDescriptor.PropertyType),
_WebApiDocumentationMetadata.DtoDocumentation[genericPropertyType].Summary));
if (propertyPrefix.Contains(propertyDescriptor.Name) || genericPropertyType.Equals(dtoDocumentationMetadata.Type))
{
continue; // Prevent circular dependencies.
}
propertyDocumentation.AddRange(
CreatePropertyDocumentation(
_WebApiDocumentationMetadata.DtoDocumentation[genericPropertyType],
String.Format("{0}{1}{2}", propertyPrefix, String.IsNullOrWhiteSpace(propertyPrefix) ? String.Empty : ".", propertyDescriptor.Name)));
}
else
{
propertyDocumentation.Add(
new PropertyDocumentation(
GetPropertyNamePrefix(propertyDescriptor, propertyPrefix),
GetProperyTypeName(propertyDescriptor.PropertyType),
documentedProperty == null ? String.Empty : documentedProperty.Description));
}
}
return propertyDocumentation;
}