mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-07-06 22:32:57 +01:00
Set openapi schema type to file where possible
This commit is contained in:
@@ -16,6 +16,7 @@ using Jellyfin.Api.Auth.LocalAccessPolicy;
|
||||
using Jellyfin.Api.Auth.RequiresElevationPolicy;
|
||||
using Jellyfin.Api.Constants;
|
||||
using Jellyfin.Api.Controllers;
|
||||
using Jellyfin.Server.Filters;
|
||||
using Jellyfin.Server.Formatters;
|
||||
using Jellyfin.Server.Models;
|
||||
using MediaBrowser.Common;
|
||||
@@ -249,6 +250,8 @@ namespace Jellyfin.Server.Extensions
|
||||
|
||||
// TODO - remove when all types are supported in System.Text.Json
|
||||
c.AddSwaggerTypeMappings();
|
||||
|
||||
c.OperationFilter<FileResponseFilter>();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
52
Jellyfin.Server/Filters/FileResponseFilter.cs
Normal file
52
Jellyfin.Server/Filters/FileResponseFilter.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Jellyfin.Api.Attributes;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace Jellyfin.Server.Filters
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public class FileResponseFilter : IOperationFilter
|
||||
{
|
||||
private const string SuccessCode = "200";
|
||||
private static readonly OpenApiMediaType _openApiMediaType = new OpenApiMediaType
|
||||
{
|
||||
Schema = new OpenApiSchema
|
||||
{
|
||||
Type = "file"
|
||||
}
|
||||
};
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
foreach (var attribute in context.ApiDescription.ActionDescriptor.EndpointMetadata)
|
||||
{
|
||||
if (attribute is ProducesFileAttribute producesFileAttribute)
|
||||
{
|
||||
// Get operation response values.
|
||||
var (_, value) = operation.Responses
|
||||
.FirstOrDefault(o => o.Key.Equals(SuccessCode, StringComparison.Ordinal));
|
||||
|
||||
// Operation doesn't have a response.
|
||||
if (value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// Clear existing responses.
|
||||
value.Content.Clear();
|
||||
|
||||
// Add all content-types as file.
|
||||
foreach (var contentType in producesFileAttribute.GetContentTypes())
|
||||
{
|
||||
value.Content.Add(contentType, _openApiMediaType);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user