AdditionalModelFilter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Jellyfin.Extensions;
  7. using Jellyfin.Server.Migrations;
  8. using MediaBrowser.Common.Plugins;
  9. using MediaBrowser.Controller.Configuration;
  10. using MediaBrowser.Controller.Net;
  11. using MediaBrowser.Controller.Net.WebSocketMessages;
  12. using MediaBrowser.Controller.Net.WebSocketMessages.Outbound;
  13. using MediaBrowser.Model.ApiClient;
  14. using MediaBrowser.Model.Session;
  15. using MediaBrowser.Model.SyncPlay;
  16. using Microsoft.OpenApi.Any;
  17. using Microsoft.OpenApi.Models;
  18. using Swashbuckle.AspNetCore.SwaggerGen;
  19. namespace Jellyfin.Server.Filters
  20. {
  21. /// <summary>
  22. /// Add models not directly used by the API, but used for discovery and websockets.
  23. /// </summary>
  24. public class AdditionalModelFilter : IDocumentFilter
  25. {
  26. // Array of options that should not be visible in the api spec.
  27. private static readonly Type[] _ignoredConfigurations = { typeof(MigrationOptions), typeof(MediaBrowser.Model.Branding.BrandingOptions) };
  28. private readonly IServerConfigurationManager _serverConfigurationManager;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="AdditionalModelFilter"/> class.
  31. /// </summary>
  32. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  33. public AdditionalModelFilter(IServerConfigurationManager serverConfigurationManager)
  34. {
  35. _serverConfigurationManager = serverConfigurationManager;
  36. }
  37. /// <inheritdoc />
  38. public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
  39. {
  40. context.SchemaGenerator.GenerateSchema(typeof(IPlugin), context.SchemaRepository);
  41. var webSocketTypes = typeof(WebSocketMessage).Assembly.GetTypes()
  42. .Where(t => t.IsSubclassOf(typeof(WebSocketMessage))
  43. && !t.IsGenericType
  44. && t != typeof(WebSocketMessageInfo))
  45. .ToList();
  46. var inboundWebSocketSchemas = new List<OpenApiSchema>();
  47. var inboundWebSocketDiscriminators = new Dictionary<string, string>();
  48. foreach (var type in webSocketTypes.Where(t => typeof(IInboundWebSocketMessage).IsAssignableFrom(t)))
  49. {
  50. var messageType = (SessionMessageType?)type.GetProperty(nameof(WebSocketMessage.MessageType))?.GetCustomAttribute<DefaultValueAttribute>()?.Value;
  51. if (messageType is null)
  52. {
  53. continue;
  54. }
  55. var schema = context.SchemaGenerator.GenerateSchema(type, context.SchemaRepository);
  56. inboundWebSocketSchemas.Add(schema);
  57. inboundWebSocketDiscriminators[messageType.ToString()!] = schema.Reference.ReferenceV3;
  58. }
  59. var inboundWebSocketMessageSchema = new OpenApiSchema
  60. {
  61. Type = "object",
  62. Description = "Represents the list of possible inbound websocket types",
  63. Reference = new OpenApiReference
  64. {
  65. Id = nameof(InboundWebSocketMessage),
  66. Type = ReferenceType.Schema
  67. },
  68. OneOf = inboundWebSocketSchemas,
  69. Discriminator = new OpenApiDiscriminator
  70. {
  71. PropertyName = nameof(WebSocketMessage.MessageType),
  72. Mapping = inboundWebSocketDiscriminators
  73. }
  74. };
  75. context.SchemaRepository.AddDefinition(nameof(InboundWebSocketMessage), inboundWebSocketMessageSchema);
  76. var outboundWebSocketSchemas = new List<OpenApiSchema>();
  77. var outboundWebSocketDiscriminators = new Dictionary<string, string>();
  78. foreach (var type in webSocketTypes.Where(t => typeof(IOutboundWebSocketMessage).IsAssignableFrom(t)))
  79. {
  80. var messageType = (SessionMessageType?)type.GetProperty(nameof(WebSocketMessage.MessageType))?.GetCustomAttribute<DefaultValueAttribute>()?.Value;
  81. if (messageType is null)
  82. {
  83. continue;
  84. }
  85. var schema = context.SchemaGenerator.GenerateSchema(type, context.SchemaRepository);
  86. outboundWebSocketSchemas.Add(schema);
  87. outboundWebSocketDiscriminators.Add(messageType.ToString()!, schema.Reference.ReferenceV3);
  88. }
  89. // Add custom "SyncPlayGroupUpdateMessage" schema because Swashbuckle cannot generate it for us
  90. var syncPlayGroupUpdateMessageSchema = new OpenApiSchema
  91. {
  92. Type = "object",
  93. Description = "Untyped sync play command.",
  94. Properties = new Dictionary<string, OpenApiSchema>
  95. {
  96. {
  97. "Data", new OpenApiSchema
  98. {
  99. AllOf =
  100. [
  101. new OpenApiSchema { Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = nameof(GroupUpdate<object>) } }
  102. ],
  103. Description = "Group update data",
  104. Nullable = false,
  105. }
  106. },
  107. { "MessageId", new OpenApiSchema { Type = "string", Format = "uuid", Description = "Gets or sets the message id." } },
  108. {
  109. "MessageType", new OpenApiSchema
  110. {
  111. Enum = Enum.GetValues<SessionMessageType>().Select(type => new OpenApiString(type.ToString())).ToList<IOpenApiAny>(),
  112. AllOf =
  113. [
  114. new OpenApiSchema { Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = nameof(SessionMessageType) } }
  115. ],
  116. Description = "The different kinds of messages that are used in the WebSocket api.",
  117. Default = new OpenApiString(nameof(SessionMessageType.SyncPlayGroupUpdate)),
  118. ReadOnly = true
  119. }
  120. },
  121. },
  122. AdditionalPropertiesAllowed = false,
  123. Reference = new OpenApiReference { Type = ReferenceType.Schema, Id = "SyncPlayGroupUpdateMessage" }
  124. };
  125. context.SchemaRepository.AddDefinition("SyncPlayGroupUpdateMessage", syncPlayGroupUpdateMessageSchema);
  126. outboundWebSocketSchemas.Add(syncPlayGroupUpdateMessageSchema);
  127. outboundWebSocketDiscriminators[nameof(SessionMessageType.SyncPlayGroupUpdate)] = syncPlayGroupUpdateMessageSchema.Reference.ReferenceV3;
  128. var outboundWebSocketMessageSchema = new OpenApiSchema
  129. {
  130. Type = "object",
  131. Description = "Represents the list of possible outbound websocket types",
  132. Reference = new OpenApiReference
  133. {
  134. Id = nameof(OutboundWebSocketMessage),
  135. Type = ReferenceType.Schema
  136. },
  137. OneOf = outboundWebSocketSchemas,
  138. Discriminator = new OpenApiDiscriminator
  139. {
  140. PropertyName = nameof(WebSocketMessage.MessageType),
  141. Mapping = outboundWebSocketDiscriminators
  142. }
  143. };
  144. context.SchemaRepository.AddDefinition(nameof(OutboundWebSocketMessage), outboundWebSocketMessageSchema);
  145. context.SchemaRepository.AddDefinition(
  146. nameof(WebSocketMessage),
  147. new OpenApiSchema
  148. {
  149. Type = "object",
  150. Description = "Represents the possible websocket types",
  151. Reference = new OpenApiReference
  152. {
  153. Id = nameof(WebSocketMessage),
  154. Type = ReferenceType.Schema
  155. },
  156. OneOf = new[]
  157. {
  158. inboundWebSocketMessageSchema,
  159. outboundWebSocketMessageSchema
  160. }
  161. });
  162. // Manually generate sync play GroupUpdate messages.
  163. var groupUpdateTypes = typeof(GroupUpdate<>).Assembly.GetTypes()
  164. .Where(t => t.BaseType != null
  165. && t.BaseType.IsGenericType
  166. && t.BaseType.GetGenericTypeDefinition() == typeof(GroupUpdate<>))
  167. .ToList();
  168. var groupUpdateSchemas = new List<OpenApiSchema>();
  169. var groupUpdateDiscriminators = new Dictionary<string, string>();
  170. foreach (var type in groupUpdateTypes)
  171. {
  172. var groupUpdateType = (GroupUpdateType?)type.GetProperty(nameof(GroupUpdate<object>.Type))?.GetCustomAttribute<DefaultValueAttribute>()?.Value;
  173. if (groupUpdateType is null)
  174. {
  175. continue;
  176. }
  177. var schema = context.SchemaGenerator.GenerateSchema(type, context.SchemaRepository);
  178. groupUpdateSchemas.Add(schema);
  179. groupUpdateDiscriminators[groupUpdateType.ToString()!] = schema.Reference.ReferenceV3;
  180. }
  181. var groupUpdateSchema = new OpenApiSchema
  182. {
  183. Type = "object",
  184. Description = "Represents the list of possible group update types",
  185. Reference = new OpenApiReference
  186. {
  187. Id = nameof(GroupUpdate<object>),
  188. Type = ReferenceType.Schema
  189. },
  190. OneOf = groupUpdateSchemas,
  191. Discriminator = new OpenApiDiscriminator
  192. {
  193. PropertyName = nameof(GroupUpdate<object>.Type),
  194. Mapping = groupUpdateDiscriminators
  195. }
  196. };
  197. context.SchemaRepository.Schemas[nameof(GroupUpdate<object>)] = groupUpdateSchema;
  198. context.SchemaGenerator.GenerateSchema(typeof(ServerDiscoveryInfo), context.SchemaRepository);
  199. foreach (var configuration in _serverConfigurationManager.GetConfigurationStores())
  200. {
  201. if (_ignoredConfigurations.IndexOf(configuration.ConfigurationType) != -1)
  202. {
  203. continue;
  204. }
  205. context.SchemaGenerator.GenerateSchema(configuration.ConfigurationType, context.SchemaRepository);
  206. }
  207. context.SchemaRepository.AddDefinition(nameof(TranscodeReason), new OpenApiSchema
  208. {
  209. Type = "string",
  210. Enum = Enum.GetNames<TranscodeReason>()
  211. .Select(e => new OpenApiString(e))
  212. .Cast<IOpenApiAny>()
  213. .ToArray()
  214. });
  215. }
  216. }
  217. }