private static object GeneratePageResult(HelpPageSampleGenerator sampleGenerator, Type type) { if (!type.IsGenericType) return null; var openGenericType = type.GetGenericTypeDefinition(); if (openGenericType != typeof (PageResult<>)) return null; // Get the T in PageResult<T> var typeParameters = type.GetGenericArguments(); Debug.Assert(typeParameters.Length == 1); // Create an enumeration to pass as the first parameter to the PageResult<T> constuctor var itemsType = typeof(List<>).MakeGenericType(typeParameters); var items = sampleGenerator.GetSampleObject(itemsType); // Fill in the other information needed to invoke the PageResult<T> constuctor var parameterTypes = new[] { itemsType, typeof(Uri), typeof(long?), }; var parameters = new[] { items, null, (long)ObjectGenerator.DefaultCollectionSize, }; // Call PageResult(IEnumerable<T> items, Uri nextPageLink, long? count) constructor var constructor = type.GetConstructor(parameterTypes); return constructor?.Invoke(parameters); }
private static void GenerateSamples(HelpPageApiModel apiModel, HelpPageSampleGenerator sampleGenerator) { try { foreach (var item in sampleGenerator.GetSampleRequests(apiModel.ApiDescription)) { apiModel.SampleRequests.Add(item.Key, item.Value); LogInvalidSampleAsError(apiModel, item.Value); } foreach (var item in sampleGenerator.GetSampleResponses(apiModel.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}", HelpPageSampleGenerator.UnwrapException(e).Message)); } }
public static ModelDescription GenerateRequestModelDescription(this ApiDescription apiDescription, ModelDescriptionGenerator modelGenerator, HelpPageSampleGenerator sampleGenerator) { foreach (var apiParameter in apiDescription.ParameterDescriptions) { if (apiParameter.Source == ApiParameterSource.FromBody) { var parameterType = apiParameter.ParameterDescriptor.ParameterType; var result = modelGenerator.GetOrCreateModelDescription(parameterType); result.ParameterDocumentation = apiParameter.Documentation; return result; } if (apiParameter.ParameterDescriptor != null && apiParameter.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage)) { var parameterType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription); if (parameterType != null) { var result = modelGenerator.GetOrCreateModelDescription(parameterType); result.ParameterDocumentation = apiParameter.Documentation; return result; } } } return null; }
/// <summary> /// Sets the help page sample generator. /// </summary> /// <param name="config">The <see cref="HttpConfiguration"/>.</param> /// <param name="sampleGenerator">The help page sample generator.</param> public static void SetHelpPageSampleGenerator(this HttpConfiguration config, HelpPageSampleGenerator sampleGenerator) { config.Properties.AddOrUpdate( typeof(HelpPageSampleGenerator), k => sampleGenerator, (k, o) => sampleGenerator); }
// Default factory for sample objects private static object DefaultSampleObjectFactory(HelpPageSampleGenerator sampleGenerator, Type type) { // Try to create a default sample object var objectGenerator = new ObjectGenerator(); return objectGenerator.GenerateObject(type); }