private Object CreateRequestBodyExample(HttpActionDescriptor httpActionDescriptor, DtoDocumentationMetadata dtoDocumentationMetadata)
{
// Create an example DTO based off AutoFixture / ObjectHydrator conventions to be used as reference if properties aren't documented.
var exampleObject = CreateInstanceOfType(dtoDocumentationMetadata.Type);
IDictionary<String, Object> requestBodyExample = new ExpandoObject();
foreach (DtoPropertyDocumentationMetadata dtoProperty in dtoDocumentationMetadata.PropertyDocumentationMetadata)
{
if (!TypeHelper.CanConvertFromString(dtoProperty.Property.PropertyType) &&
_WebApiDocumentationMetadata.DtoDocumentation.ContainsKey(dtoProperty.Property.PropertyType))
{
requestBodyExample[dtoProperty.Property.Name] =
CreateRequestBodyExample(httpActionDescriptor, _WebApiDocumentationMetadata.DtoDocumentation[dtoProperty.Property.PropertyType]);
}
else if (!httpActionDescriptor.SupportedHttpMethods.Intersect(dtoProperty.ExcludedMethods ?? Enumerable.Empty<HttpMethod>()).Any())
{
if (_WebApiDocumentationMetadata.Settings
.RequestBuilderConventions
.Any(c => !c.IncludeProperty(
httpActionDescriptor.SupportedHttpMethods,
dtoDocumentationMetadata.Type.GetProperty(dtoProperty.Property.Name),
dtoDocumentationMetadata.Type)))
{
continue;
}
var propertyDocumentationMetadata =
dtoDocumentationMetadata.PropertyDocumentationMetadata
.SingleOrDefault(pdm => pdm.Property.Equals(dtoProperty.Property) && pdm.ExampleValue != null);
if (propertyDocumentationMetadata != null)
{
requestBodyExample[dtoProperty.Property.Name] = propertyDocumentationMetadata.ExampleValue;
}
else
{
requestBodyExample[dtoProperty.Property.Name] = dtoProperty.Property.GetValue(exampleObject);
}
}
}
return requestBodyExample;
}