-
-
Save yaohuang/c231426e036421d4dc78 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment