diff options
| author | JP Dillingham <jp@dillingham.me> | 2025-11-29 14:20:13 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2025-11-29 14:20:13 -0600 |
| commit | 368cc845e147ed1badff14e76dd393449e4182e3 (patch) | |
| tree | 35f14266d9ee84d4bd9a1879341790396ea49d8f /src | |
| parent | d81404d6f8dfdfcb1d29dcf3a5e6be230046f138 (diff) | |
| download | slskd-368cc845e147ed1badff14e76dd393449e4182e3.tar.gz | |
make content negotiation work properly
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/OpenAPI/ContentNegotiationOperationFilter.cs | 100 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 3 |
2 files changed, 103 insertions, 0 deletions
diff --git a/src/slskd/Common/OpenAPI/ContentNegotiationOperationFilter.cs b/src/slskd/Common/OpenAPI/ContentNegotiationOperationFilter.cs new file mode 100644 index 00000000..db5c4baa --- /dev/null +++ b/src/slskd/Common/OpenAPI/ContentNegotiationOperationFilter.cs @@ -0,0 +1,100 @@ +// <copyright file="ContentNegotiationOperationFilter.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd; + +using System.Linq; +using System.Reflection; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Formatters; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; + +/// <summary> +/// OpenAPI operation filter that properly handles content negotiation with different response schemas. +/// </summary> +public class ContentNegotiationOperationFilter : IOperationFilter +{ + public void Apply(OpenApiOperation operation, OperationFilterContext context) + { + // check to see if the current operation is decorated with [Produces], which is what enables content negotiation + var producesAttribute = context.MethodInfo + .GetCustomAttributes(typeof(ProducesAttribute), false) + .Cast<ProducesAttribute>() + .FirstOrDefault(); + + if (producesAttribute == null || producesAttribute.ContentTypes.Count == 0) + { + return; + } + + // next, get all of the [ProducesResponseType] attributes, if any + var producesResponseTypeAttributes = context.MethodInfo + .GetCustomAttributes(typeof(ProducesResponseTypeAttribute), false) + .Cast<ProducesResponseTypeAttribute>() + .ToList(); + + if (producesResponseTypeAttributes.Count == 0) + { + return; + } + + // map over the list of status codes; we need to (attempt to) perform this mapping for each status code and each + // content type listed in [Produces] + foreach (var statusCodeGroup in producesResponseTypeAttributes.GroupBy(x => x.StatusCode)) + { + foreach (var contentType in producesAttribute.ContentTypes) + { + // find the first [ProducesResponseType] that contains this content type in its list of content types + var attribute = statusCodeGroup.FirstOrDefault(x => GetContentTypes(x).Contains(contentType)); + + if (attribute is not null) + { + var response = operation.Responses[statusCodeGroup.Key.ToString()]; + response.Content[contentType] = new OpenApiMediaType + { + Schema = context.SchemaGenerator.GenerateSchema(attribute.Type, context.SchemaRepository), + }; + } + } + } + } + + /// <summary> + /// Extracts the value of the _contentTypes field of <see cref="ProducesResponseTypeAttribute"/>. + /// + /// This is necessary because Microsoft didn't see fit to bless us with the ability to retrieve the content types + /// we specifify along with our [ProducesResponseType] attributes. + /// + /// It will most likely break at some point. + /// </summary> + /// <param name="attribute">The attribute from which to extract the _contentTypes field.</param> + /// <returns>The value of the extracted field.</returns> + private MediaTypeCollection GetContentTypes(ProducesResponseTypeAttribute attribute) + { + var contentTypesField = typeof(ProducesResponseTypeAttribute).GetField("_contentTypes", BindingFlags.NonPublic | BindingFlags.Instance); + + MediaTypeCollection value = (MediaTypeCollection)contentTypesField.GetValue(attribute); + + if (value is null) + { + return []; + } + + return value; + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index c5646df8..3af09a42 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -903,6 +903,9 @@ namespace slskd }, }); + // allow endpoints marked with multiple content types in [Produces] to generate properly + options.OperationFilter<ContentNegotiationOperationFilter>(); + if (IOFile.Exists(XmlDocumentationFile)) { options.IncludeXmlComments(XmlDocumentationFile); |
