HelpPageSampleGenerator.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Globalization;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Http;
  10. using System.Net.Http.Formatting;
  11. using System.Net.Http.Headers;
  12. using System.Xml.Linq;
  13. using Newtonsoft.Json;
  14. namespace EVCB_OCPP.WEBAPI.Areas.HelpPage
  15. {
  16. /// <summary>
  17. /// This class will generate the samples for the help page.
  18. /// </summary>
  19. public class HelpPageSampleGenerator
  20. {
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="HelpPageSampleGenerator"/> class.
  23. /// </summary>
  24. public HelpPageSampleGenerator()
  25. {
  26. ActualHttpMessageTypes = new Dictionary<HelpPageSampleKey, Type>();
  27. ActionSamples = new Dictionary<HelpPageSampleKey, object>();
  28. SampleObjects = new Dictionary<Type, object>();
  29. SampleObjectFactories = new List<Func<HelpPageSampleGenerator, Type, object>>
  30. {
  31. DefaultSampleObjectFactory,
  32. };
  33. }
  34. /// <summary>
  35. /// Gets CLR types that are used as the content of <see cref="HttpRequestMessage"/> or <see cref="HttpResponseMessage"/>.
  36. /// </summary>
  37. public IDictionary<HelpPageSampleKey, Type> ActualHttpMessageTypes { get; internal set; }
  38. /// <summary>
  39. /// Gets the objects that are used directly as samples for certain actions.
  40. /// </summary>
  41. public IDictionary<HelpPageSampleKey, object> ActionSamples { get; internal set; }
  42. /// <summary>
  43. /// Gets the objects that are serialized as samples by the supported formatters.
  44. /// </summary>
  45. public IDictionary<Type, object> SampleObjects { get; internal set; }
  46. /// <summary>
  47. /// Gets factories for the objects that the supported formatters will serialize as samples. Processed in order,
  48. /// stopping when the factory successfully returns a non-<see langref="null"/> object.
  49. /// </summary>
  50. /// <remarks>
  51. /// Collection includes just <see cref="ObjectGenerator.GenerateObject(Type)"/> initially. Use
  52. /// <code>SampleObjectFactories.Insert(0, func)</code> to provide an override and
  53. /// <code>SampleObjectFactories.Add(func)</code> to provide a fallback.</remarks>
  54. [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures",
  55. Justification = "This is an appropriate nesting of generic types")]
  56. public IList<Func<HelpPageSampleGenerator, Type, object>> SampleObjectFactories { get; private set; }
  57. /// <summary>
  58. /// Gets the request body samples for a given <see cref="ApiDescription"/>.
  59. /// </summary>
  60. /// <param name="api">The <see cref="ApiDescription"/>.</param>
  61. /// <returns>The samples keyed by media type.</returns>
  62. public IDictionary<MediaTypeHeaderValue, object> GetSampleRequests(ApiDescription api)
  63. {
  64. return GetSample(api, SampleDirection.Request);
  65. }
  66. /// <summary>
  67. /// Gets the response body samples for a given <see cref="ApiDescription"/>.
  68. /// </summary>
  69. /// <param name="api">The <see cref="ApiDescription"/>.</param>
  70. /// <returns>The samples keyed by media type.</returns>
  71. public IDictionary<MediaTypeHeaderValue, object> GetSampleResponses(ApiDescription api)
  72. {
  73. return GetSample(api, SampleDirection.Response);
  74. }
  75. /// <summary>
  76. /// Gets the request or response body samples.
  77. /// </summary>
  78. /// <param name="api">The <see cref="ApiDescription"/>.</param>
  79. /// <param name="sampleDirection">The value indicating whether the sample is for a request or for a response.</param>
  80. /// <returns>The samples keyed by media type.</returns>
  81. public virtual IDictionary<MediaTypeHeaderValue, object> GetSample(ApiDescription api, SampleDirection sampleDirection)
  82. {
  83. if (api == null)
  84. {
  85. throw new ArgumentNullException("api");
  86. }
  87. string controllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName;
  88. string actionName = api.ActionDescriptor.ActionName;
  89. IEnumerable<string> parameterNames = api.ParameterDescriptions.Select(p => p.Name);
  90. Collection<MediaTypeFormatter> formatters;
  91. Type type = ResolveType(api, controllerName, actionName, parameterNames, sampleDirection, out formatters);
  92. var samples = new Dictionary<MediaTypeHeaderValue, object>();
  93. // Use the samples provided directly for actions
  94. var actionSamples = GetAllActionSamples(controllerName, actionName, parameterNames, sampleDirection);
  95. foreach (var actionSample in actionSamples)
  96. {
  97. samples.Add(actionSample.Key.MediaType, WrapSampleIfString(actionSample.Value));
  98. }
  99. // Do the sample generation based on formatters only if an action doesn't return an HttpResponseMessage.
  100. // Here we cannot rely on formatters because we don't know what's in the HttpResponseMessage, it might not even use formatters.
  101. if (type != null && !typeof(HttpResponseMessage).IsAssignableFrom(type))
  102. {
  103. object sampleObject = GetSampleObject(type);
  104. foreach (var formatter in formatters)
  105. {
  106. foreach (MediaTypeHeaderValue mediaType in formatter.SupportedMediaTypes)
  107. {
  108. if (!samples.ContainsKey(mediaType))
  109. {
  110. object sample = GetActionSample(controllerName, actionName, parameterNames, type, formatter, mediaType, sampleDirection);
  111. // If no sample found, try generate sample using formatter and sample object
  112. if (sample == null && sampleObject != null)
  113. {
  114. sample = WriteSampleObjectUsingFormatter(formatter, sampleObject, type, mediaType);
  115. }
  116. samples.Add(mediaType, WrapSampleIfString(sample));
  117. }
  118. }
  119. }
  120. }
  121. return samples;
  122. }
  123. /// <summary>
  124. /// Search for samples that are provided directly through <see cref="ActionSamples"/>.
  125. /// </summary>
  126. /// <param name="controllerName">Name of the controller.</param>
  127. /// <param name="actionName">Name of the action.</param>
  128. /// <param name="parameterNames">The parameter names.</param>
  129. /// <param name="type">The CLR type.</param>
  130. /// <param name="formatter">The formatter.</param>
  131. /// <param name="mediaType">The media type.</param>
  132. /// <param name="sampleDirection">The value indicating whether the sample is for a request or for a response.</param>
  133. /// <returns>The sample that matches the parameters.</returns>
  134. public virtual object GetActionSample(string controllerName, string actionName, IEnumerable<string> parameterNames, Type type, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, SampleDirection sampleDirection)
  135. {
  136. object sample;
  137. // First, try to get the sample provided for the specified mediaType, sampleDirection, controllerName, actionName and parameterNames.
  138. // If not found, try to get the sample provided for the specified mediaType, sampleDirection, controllerName and actionName regardless of the parameterNames.
  139. // If still not found, try to get the sample provided for the specified mediaType and type.
  140. // Finally, try to get the sample provided for the specified mediaType.
  141. if (ActionSamples.TryGetValue(new HelpPageSampleKey(mediaType, sampleDirection, controllerName, actionName, parameterNames), out sample) ||
  142. ActionSamples.TryGetValue(new HelpPageSampleKey(mediaType, sampleDirection, controllerName, actionName, new[] { "*" }), out sample) ||
  143. ActionSamples.TryGetValue(new HelpPageSampleKey(mediaType, type), out sample) ||
  144. ActionSamples.TryGetValue(new HelpPageSampleKey(mediaType), out sample))
  145. {
  146. return sample;
  147. }
  148. return null;
  149. }
  150. /// <summary>
  151. /// Gets the sample object that will be serialized by the formatters.
  152. /// First, it will look at the <see cref="SampleObjects"/>. If no sample object is found, it will try to create
  153. /// one using <see cref="DefaultSampleObjectFactory"/> (which wraps an <see cref="ObjectGenerator"/>) and other
  154. /// factories in <see cref="SampleObjectFactories"/>.
  155. /// </summary>
  156. /// <param name="type">The type.</param>
  157. /// <returns>The sample object.</returns>
  158. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
  159. Justification = "Even if all items in SampleObjectFactories throw, problem will be visible as missing sample.")]
  160. public virtual object GetSampleObject(Type type)
  161. {
  162. object sampleObject;
  163. if (!SampleObjects.TryGetValue(type, out sampleObject))
  164. {
  165. // No specific object available, try our factories.
  166. foreach (Func<HelpPageSampleGenerator, Type, object> factory in SampleObjectFactories)
  167. {
  168. if (factory == null)
  169. {
  170. continue;
  171. }
  172. try
  173. {
  174. sampleObject = factory(this, type);
  175. if (sampleObject != null)
  176. {
  177. break;
  178. }
  179. }
  180. catch
  181. {
  182. // Ignore any problems encountered in the factory; go on to the next one (if any).
  183. }
  184. }
  185. }
  186. return sampleObject;
  187. }
  188. /// <summary>
  189. /// Resolves the actual type of <see cref="System.Net.Http.ObjectContent{T}"/> passed to the <see cref="System.Net.Http.HttpRequestMessage"/> in an action.
  190. /// </summary>
  191. /// <param name="api">The <see cref="ApiDescription"/>.</param>
  192. /// <returns>The type.</returns>
  193. public virtual Type ResolveHttpRequestMessageType(ApiDescription api)
  194. {
  195. string controllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName;
  196. string actionName = api.ActionDescriptor.ActionName;
  197. IEnumerable<string> parameterNames = api.ParameterDescriptions.Select(p => p.Name);
  198. Collection<MediaTypeFormatter> formatters;
  199. return ResolveType(api, controllerName, actionName, parameterNames, SampleDirection.Request, out formatters);
  200. }
  201. /// <summary>
  202. /// Resolves the type of the action parameter or return value when <see cref="HttpRequestMessage"/> or <see cref="HttpResponseMessage"/> is used.
  203. /// </summary>
  204. /// <param name="api">The <see cref="ApiDescription"/>.</param>
  205. /// <param name="controllerName">Name of the controller.</param>
  206. /// <param name="actionName">Name of the action.</param>
  207. /// <param name="parameterNames">The parameter names.</param>
  208. /// <param name="sampleDirection">The value indicating whether the sample is for a request or a response.</param>
  209. /// <param name="formatters">The formatters.</param>
  210. [SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", Justification = "This is only used in advanced scenarios.")]
  211. public virtual Type ResolveType(ApiDescription api, string controllerName, string actionName, IEnumerable<string> parameterNames, SampleDirection sampleDirection, out Collection<MediaTypeFormatter> formatters)
  212. {
  213. if (!Enum.IsDefined(typeof(SampleDirection), sampleDirection))
  214. {
  215. throw new InvalidEnumArgumentException("sampleDirection", (int)sampleDirection, typeof(SampleDirection));
  216. }
  217. if (api == null)
  218. {
  219. throw new ArgumentNullException("api");
  220. }
  221. Type type;
  222. if (ActualHttpMessageTypes.TryGetValue(new HelpPageSampleKey(sampleDirection, controllerName, actionName, parameterNames), out type) ||
  223. ActualHttpMessageTypes.TryGetValue(new HelpPageSampleKey(sampleDirection, controllerName, actionName, new[] { "*" }), out type))
  224. {
  225. // Re-compute the supported formatters based on type
  226. Collection<MediaTypeFormatter> newFormatters = new Collection<MediaTypeFormatter>();
  227. foreach (var formatter in api.ActionDescriptor.Configuration.Formatters)
  228. {
  229. if (IsFormatSupported(sampleDirection, formatter, type))
  230. {
  231. newFormatters.Add(formatter);
  232. }
  233. }
  234. formatters = newFormatters;
  235. }
  236. else
  237. {
  238. switch (sampleDirection)
  239. {
  240. case SampleDirection.Request:
  241. ApiParameterDescription requestBodyParameter = api.ParameterDescriptions.FirstOrDefault(p => p.Source == ApiParameterSource.FromBody);
  242. type = requestBodyParameter == null ? null : requestBodyParameter.ParameterDescriptor.ParameterType;
  243. formatters = api.SupportedRequestBodyFormatters;
  244. break;
  245. case SampleDirection.Response:
  246. default:
  247. type = api.ResponseDescription.ResponseType ?? api.ResponseDescription.DeclaredType;
  248. formatters = api.SupportedResponseFormatters;
  249. break;
  250. }
  251. }
  252. return type;
  253. }
  254. /// <summary>
  255. /// Writes the sample object using formatter.
  256. /// </summary>
  257. /// <param name="formatter">The formatter.</param>
  258. /// <param name="value">The value.</param>
  259. /// <param name="type">The type.</param>
  260. /// <param name="mediaType">Type of the media.</param>
  261. /// <returns></returns>
  262. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "The exception is recorded as InvalidSample.")]
  263. public virtual object WriteSampleObjectUsingFormatter(MediaTypeFormatter formatter, object value, Type type, MediaTypeHeaderValue mediaType)
  264. {
  265. if (formatter == null)
  266. {
  267. throw new ArgumentNullException("formatter");
  268. }
  269. if (mediaType == null)
  270. {
  271. throw new ArgumentNullException("mediaType");
  272. }
  273. object sample = String.Empty;
  274. MemoryStream ms = null;
  275. HttpContent content = null;
  276. try
  277. {
  278. if (formatter.CanWriteType(type))
  279. {
  280. ms = new MemoryStream();
  281. content = new ObjectContent(type, value, formatter, mediaType);
  282. formatter.WriteToStreamAsync(type, value, ms, content, null).Wait();
  283. ms.Position = 0;
  284. StreamReader reader = new StreamReader(ms);
  285. string serializedSampleString = reader.ReadToEnd();
  286. if (mediaType.MediaType.ToUpperInvariant().Contains("XML"))
  287. {
  288. serializedSampleString = TryFormatXml(serializedSampleString);
  289. }
  290. else if (mediaType.MediaType.ToUpperInvariant().Contains("JSON"))
  291. {
  292. serializedSampleString = TryFormatJson(serializedSampleString);
  293. }
  294. sample = new TextSample(serializedSampleString);
  295. }
  296. else
  297. {
  298. sample = new InvalidSample(String.Format(
  299. CultureInfo.CurrentCulture,
  300. "Failed to generate the sample for media type '{0}'. Cannot use formatter '{1}' to write type '{2}'.",
  301. mediaType,
  302. formatter.GetType().Name,
  303. type.Name));
  304. }
  305. }
  306. catch (Exception e)
  307. {
  308. sample = new InvalidSample(String.Format(
  309. CultureInfo.CurrentCulture,
  310. "An exception has occurred while using the formatter '{0}' to generate sample for media type '{1}'. Exception message: {2}",
  311. formatter.GetType().Name,
  312. mediaType.MediaType,
  313. UnwrapException(e).Message));
  314. }
  315. finally
  316. {
  317. if (ms != null)
  318. {
  319. ms.Dispose();
  320. }
  321. if (content != null)
  322. {
  323. content.Dispose();
  324. }
  325. }
  326. return sample;
  327. }
  328. internal static Exception UnwrapException(Exception exception)
  329. {
  330. AggregateException aggregateException = exception as AggregateException;
  331. if (aggregateException != null)
  332. {
  333. return aggregateException.Flatten().InnerException;
  334. }
  335. return exception;
  336. }
  337. // Default factory for sample objects
  338. private static object DefaultSampleObjectFactory(HelpPageSampleGenerator sampleGenerator, Type type)
  339. {
  340. // Try to create a default sample object
  341. ObjectGenerator objectGenerator = new ObjectGenerator();
  342. return objectGenerator.GenerateObject(type);
  343. }
  344. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Handling the failure by returning the original string.")]
  345. private static string TryFormatJson(string str)
  346. {
  347. try
  348. {
  349. object parsedJson = JsonConvert.DeserializeObject(str);
  350. return JsonConvert.SerializeObject(parsedJson, Formatting.Indented);
  351. }
  352. catch
  353. {
  354. // can't parse JSON, return the original string
  355. return str;
  356. }
  357. }
  358. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Handling the failure by returning the original string.")]
  359. private static string TryFormatXml(string str)
  360. {
  361. try
  362. {
  363. XDocument xml = XDocument.Parse(str);
  364. return xml.ToString();
  365. }
  366. catch
  367. {
  368. // can't parse XML, return the original string
  369. return str;
  370. }
  371. }
  372. private static bool IsFormatSupported(SampleDirection sampleDirection, MediaTypeFormatter formatter, Type type)
  373. {
  374. switch (sampleDirection)
  375. {
  376. case SampleDirection.Request:
  377. return formatter.CanReadType(type);
  378. case SampleDirection.Response:
  379. return formatter.CanWriteType(type);
  380. }
  381. return false;
  382. }
  383. private IEnumerable<KeyValuePair<HelpPageSampleKey, object>> GetAllActionSamples(string controllerName, string actionName, IEnumerable<string> parameterNames, SampleDirection sampleDirection)
  384. {
  385. HashSet<string> parameterNamesSet = new HashSet<string>(parameterNames, StringComparer.OrdinalIgnoreCase);
  386. foreach (var sample in ActionSamples)
  387. {
  388. HelpPageSampleKey sampleKey = sample.Key;
  389. if (String.Equals(controllerName, sampleKey.ControllerName, StringComparison.OrdinalIgnoreCase) &&
  390. String.Equals(actionName, sampleKey.ActionName, StringComparison.OrdinalIgnoreCase) &&
  391. (sampleKey.ParameterNames.SetEquals(new[] { "*" }) || parameterNamesSet.SetEquals(sampleKey.ParameterNames)) &&
  392. sampleDirection == sampleKey.SampleDirection)
  393. {
  394. yield return sample;
  395. }
  396. }
  397. }
  398. private static object WrapSampleIfString(object sample)
  399. {
  400. string stringSample = sample as string;
  401. if (stringSample != null)
  402. {
  403. return new TextSample(stringSample);
  404. }
  405. return sample;
  406. }
  407. }
  408. }