blob: e4eb5be2b997d05dd1fee0e8d5acde27e871c27d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace Jellyfin.Server.Filters;
/// <summary>
/// Document filter that fixes security scheme references after document generation.
/// </summary>
/// <remarks>
/// In Microsoft.OpenApi v2, <see cref="OpenApiSecuritySchemeReference"/> requires a resolved
/// <c>Target</c> to serialize correctly. References created without a host document (as in
/// operation filters) serialize as empty objects. This filter re-creates all security scheme
/// references with the document context so they resolve properly during serialization.
/// </remarks>
internal class SecuritySchemeReferenceFixupFilter : IDocumentFilter
{
/// <inheritdoc />
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
swaggerDoc.RegisterComponents();
if (swaggerDoc.Paths is null)
{
return;
}
foreach (var pathItem in swaggerDoc.Paths.Values)
{
if (pathItem.Operations is null)
{
continue;
}
foreach (var operation in pathItem.Operations.Values)
{
if (operation.Security is null)
{
continue;
}
for (int i = 0; i < operation.Security.Count; i++)
{
var oldReq = operation.Security[i];
var newReq = new OpenApiSecurityRequirement();
foreach (var kvp in oldReq)
{
var fixedRef = new OpenApiSecuritySchemeReference(kvp.Key.Reference.Id!, swaggerDoc);
newReq[fixedRef] = kvp.Value;
}
operation.Security[i] = newReq;
}
}
}
}
}
|