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