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