secret
Created

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
internal static bool IsSimpleType(Type type)
{
return type.IsPrimitive ||
type.Equals(typeof(string)) ||
type.Equals(typeof(DateTime)) ||
type.Equals(typeof(Decimal)) ||
type.Equals(typeof(Guid)) ||
type.Equals(typeof(DateTimeOffset)) ||
type.Equals(typeof(TimeSpan));
}
 
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The exception is recorded as ErrorMessages.")]
private static HelpPageApiModel GenerateApiModel(ApiDescription apiDescription, HelpPageSampleGenerator sampleGenerator)
{
HelpPageApiModel apiModel = new HelpPageApiModel();
apiModel.ApiDescription = apiDescription;
 
// Extra code to handle parameter properties ///////////////////////////////////////
List<ApiParameterDescription> paramsToRemove = new List<ApiParameterDescription>();
List<ApiParameterDescription> paramsToAdd = new List<ApiParameterDescription>();
foreach (var parameter in apiDescription.ParameterDescriptions)
{
Type parameterType = parameter.ParameterDescriptor.ParameterType;
if (parameter.Source == ApiParameterSource.FromUri && !IsSimpleType(parameterType))
{
paramsToRemove.Add(parameter);
foreach (var property in parameterType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly))
{
var newParam = new ApiParameterDescription();
newParam.Name = property.Name;
newParam.Source = ApiParameterSource.FromUri;
newParam.Documentation = "Property documentation";
newParam.ParameterDescriptor = parameter.ParameterDescriptor;
paramsToAdd.Add(newParam);
}
}
}
foreach (var param in paramsToRemove)
{
apiDescription.ParameterDescriptions.Remove(param);
}
foreach (var param in paramsToAdd)
{
apiDescription.ParameterDescriptions.Add(param);
}
// End of Extra code ///////////////////////////////////////
 
try
{
foreach (var item in sampleGenerator.GetSampleRequests(apiDescription))
{
apiModel.SampleRequests.Add(item.Key, item.Value);
LogInvalidSampleAsError(apiModel, item.Value);
 
}
 
foreach (var item in sampleGenerator.GetSampleResponses(apiDescription))
{
apiModel.SampleResponses.Add(item.Key, item.Value);
LogInvalidSampleAsError(apiModel, item.Value);
}
}
catch (Exception e)
{
apiModel.ErrorMessages.Add(String.Format(CultureInfo.CurrentCulture, "An exception has occurred while generating the sample. Exception Message: {0}", e.Message));
}
 
return apiModel;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.