AdditionalModelFilter.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // Additional discriminator needed for GroupUpdate models...
  86. if (messageType == SessionMessageType.SyncPlayGroupUpdate && type != typeof(SyncPlayGroupUpdateCommandMessage))
  87. {
  88. continue;
  89. }
  90. var schema = context.SchemaGenerator.GenerateSchema(type, context.SchemaRepository);
  91. outboundWebSocketSchemas.Add(schema);
  92. outboundWebSocketDiscriminators.Add(messageType.ToString()!, schema.Reference.ReferenceV3);
  93. }
  94. var outboundWebSocketMessageSchema = new OpenApiSchema
  95. {
  96. Type = "object",
  97. Description = "Represents the list of possible outbound websocket types",
  98. Reference = new OpenApiReference
  99. {
  100. Id = nameof(OutboundWebSocketMessage),
  101. Type = ReferenceType.Schema
  102. },
  103. OneOf = outboundWebSocketSchemas,
  104. Discriminator = new OpenApiDiscriminator
  105. {
  106. PropertyName = nameof(WebSocketMessage.MessageType),
  107. Mapping = outboundWebSocketDiscriminators
  108. }
  109. };
  110. context.SchemaRepository.AddDefinition(nameof(OutboundWebSocketMessage), outboundWebSocketMessageSchema);
  111. context.SchemaRepository.AddDefinition(
  112. nameof(WebSocketMessage),
  113. new OpenApiSchema
  114. {
  115. Type = "object",
  116. Description = "Represents the possible websocket types",
  117. Reference = new OpenApiReference
  118. {
  119. Id = nameof(WebSocketMessage),
  120. Type = ReferenceType.Schema
  121. },
  122. OneOf = new[]
  123. {
  124. inboundWebSocketMessageSchema,
  125. outboundWebSocketMessageSchema
  126. }
  127. });
  128. // Manually generate sync play GroupUpdate messages.
  129. if (!context.SchemaRepository.Schemas.TryGetValue(nameof(GroupUpdate), out var groupUpdateSchema))
  130. {
  131. groupUpdateSchema = context.SchemaGenerator.GenerateSchema(typeof(GroupUpdate), context.SchemaRepository);
  132. }
  133. var groupUpdateOfGroupInfoSchema = context.SchemaGenerator.GenerateSchema(typeof(GroupUpdate<GroupInfoDto>), context.SchemaRepository);
  134. var groupUpdateOfGroupStateSchema = context.SchemaGenerator.GenerateSchema(typeof(GroupUpdate<GroupStateUpdate>), context.SchemaRepository);
  135. var groupUpdateOfStringSchema = context.SchemaGenerator.GenerateSchema(typeof(GroupUpdate<string>), context.SchemaRepository);
  136. var groupUpdateOfPlayQueueSchema = context.SchemaGenerator.GenerateSchema(typeof(GroupUpdate<PlayQueueUpdate>), context.SchemaRepository);
  137. groupUpdateSchema.OneOf = new List<OpenApiSchema>
  138. {
  139. groupUpdateOfGroupInfoSchema,
  140. groupUpdateOfGroupStateSchema,
  141. groupUpdateOfStringSchema,
  142. groupUpdateOfPlayQueueSchema
  143. };
  144. groupUpdateSchema.Discriminator = new OpenApiDiscriminator
  145. {
  146. PropertyName = nameof(GroupUpdate.Type),
  147. Mapping = new Dictionary<string, string>
  148. {
  149. { GroupUpdateType.UserJoined.ToString(), groupUpdateOfStringSchema.Reference.ReferenceV3 },
  150. { GroupUpdateType.UserLeft.ToString(), groupUpdateOfStringSchema.Reference.ReferenceV3 },
  151. { GroupUpdateType.GroupJoined.ToString(), groupUpdateOfGroupInfoSchema.Reference.ReferenceV3 },
  152. { GroupUpdateType.GroupLeft.ToString(), groupUpdateOfStringSchema.Reference.ReferenceV3 },
  153. { GroupUpdateType.StateUpdate.ToString(), groupUpdateOfGroupStateSchema.Reference.ReferenceV3 },
  154. { GroupUpdateType.PlayQueue.ToString(), groupUpdateOfPlayQueueSchema.Reference.ReferenceV3 },
  155. { GroupUpdateType.NotInGroup.ToString(), groupUpdateOfStringSchema.Reference.ReferenceV3 },
  156. { GroupUpdateType.GroupDoesNotExist.ToString(), groupUpdateOfStringSchema.Reference.ReferenceV3 },
  157. { GroupUpdateType.LibraryAccessDenied.ToString(), groupUpdateOfStringSchema.Reference.ReferenceV3 }
  158. }
  159. };
  160. context.SchemaGenerator.GenerateSchema(typeof(ServerDiscoveryInfo), context.SchemaRepository);
  161. foreach (var configuration in _serverConfigurationManager.GetConfigurationStores())
  162. {
  163. if (_ignoredConfigurations.IndexOf(configuration.ConfigurationType) != -1)
  164. {
  165. continue;
  166. }
  167. context.SchemaGenerator.GenerateSchema(configuration.ConfigurationType, context.SchemaRepository);
  168. }
  169. context.SchemaRepository.AddDefinition(nameof(TranscodeReason), new OpenApiSchema
  170. {
  171. Type = "string",
  172. Enum = Enum.GetNames<TranscodeReason>()
  173. .Select(e => new OpenApiString(e))
  174. .Cast<IOpenApiAny>()
  175. .ToArray()
  176. });
  177. }
  178. }
  179. }