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