2
0

AdditionalModelFilter.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Linq;
  3. using Jellyfin.Extensions;
  4. using Jellyfin.Server.Migrations;
  5. using MediaBrowser.Common.Plugins;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.LiveTv;
  8. using MediaBrowser.Model.ApiClient;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Session;
  11. using MediaBrowser.Model.SyncPlay;
  12. using Microsoft.OpenApi.Any;
  13. using Microsoft.OpenApi.Models;
  14. using Swashbuckle.AspNetCore.SwaggerGen;
  15. namespace Jellyfin.Server.Filters
  16. {
  17. /// <summary>
  18. /// Add models not directly used by the API, but used for discovery and websockets.
  19. /// </summary>
  20. public class AdditionalModelFilter : IDocumentFilter
  21. {
  22. // Array of options that should not be visible in the api spec.
  23. private static readonly Type[] _ignoredConfigurations = { typeof(MigrationOptions) };
  24. private readonly IServerConfigurationManager _serverConfigurationManager;
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="AdditionalModelFilter"/> class.
  27. /// </summary>
  28. /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
  29. public AdditionalModelFilter(IServerConfigurationManager serverConfigurationManager)
  30. {
  31. _serverConfigurationManager = serverConfigurationManager;
  32. }
  33. /// <inheritdoc />
  34. public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
  35. {
  36. context.SchemaGenerator.GenerateSchema(typeof(LibraryUpdateInfo), context.SchemaRepository);
  37. context.SchemaGenerator.GenerateSchema(typeof(IPlugin), context.SchemaRepository);
  38. context.SchemaGenerator.GenerateSchema(typeof(PlayRequest), context.SchemaRepository);
  39. context.SchemaGenerator.GenerateSchema(typeof(PlaystateRequest), context.SchemaRepository);
  40. context.SchemaGenerator.GenerateSchema(typeof(TimerEventInfo), context.SchemaRepository);
  41. context.SchemaGenerator.GenerateSchema(typeof(SendCommand), context.SchemaRepository);
  42. context.SchemaGenerator.GenerateSchema(typeof(GeneralCommandType), context.SchemaRepository);
  43. context.SchemaGenerator.GenerateSchema(typeof(GroupUpdate<object>), context.SchemaRepository);
  44. context.SchemaGenerator.GenerateSchema(typeof(SessionMessageType), context.SchemaRepository);
  45. context.SchemaGenerator.GenerateSchema(typeof(ServerDiscoveryInfo), context.SchemaRepository);
  46. foreach (var configuration in _serverConfigurationManager.GetConfigurationStores())
  47. {
  48. if (_ignoredConfigurations.IndexOf(configuration.ConfigurationType) != -1)
  49. {
  50. continue;
  51. }
  52. context.SchemaGenerator.GenerateSchema(configuration.ConfigurationType, context.SchemaRepository);
  53. }
  54. context.SchemaRepository.AddDefinition(nameof(TranscodeReason), new OpenApiSchema
  55. {
  56. Type = "string",
  57. Enum = Enum.GetNames<TranscodeReason>()
  58. .Select(e => new OpenApiString(e))
  59. .Cast<IOpenApiAny>()
  60. .ToArray()
  61. });
  62. }
  63. }
  64. }