From ae51f83cca7a2fa474b931c2898a5cab96277873 Mon Sep 17 00:00:00 2001 From: Havunen Date: Sun, 25 Feb 2024 08:25:05 +0200 Subject: [PATCH] Fixes an issue where interface chain is not checked for parameters, this resolves issue: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2596 for https://github.com/Havunen/DotSwashbuckle/issues/3 --- .../ApiParameterDescriptionExtensions.cs | 22 +++++++++++++++++- .../Controllers/CrudActionsController.cs | 23 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiParameterDescriptionExtensions.cs b/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiParameterDescriptionExtensions.cs index e0f8dd980a..7b6d48c939 100644 --- a/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiParameterDescriptionExtensions.cs +++ b/src/DotSwashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/ApiParameterDescriptionExtensions.cs @@ -44,7 +44,27 @@ public static PropertyInfo PropertyInfo(this ApiParameterDescription apiParamete { var modelMetadata = apiParameter.ModelMetadata; - return modelMetadata?.ContainerType?.GetProperty(modelMetadata.PropertyName); + if (modelMetadata.ContainerType != null) + { + PropertyInfo propertyInfo = modelMetadata.ContainerType.GetProperty(modelMetadata.PropertyName); + + if (propertyInfo != null) + { + return propertyInfo; + } + + foreach (var type in modelMetadata.ContainerType.GetInterfaces()) + { + propertyInfo = type.GetProperty(modelMetadata.PropertyName); + + if (propertyInfo != null) + { + return propertyInfo; + } + } + } + + return null; } public static IEnumerable CustomAttributes(this ApiParameterDescription apiParameter) diff --git a/test/WebSites/Basic/Controllers/CrudActionsController.cs b/test/WebSites/Basic/Controllers/CrudActionsController.cs index b5e38e2460..56784c267f 100644 --- a/test/WebSites/Basic/Controllers/CrudActionsController.cs +++ b/test/WebSites/Basic/Controllers/CrudActionsController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Net.Mime; using Microsoft.AspNetCore.Mvc; namespace Basic.Controllers @@ -97,6 +98,28 @@ public ProductStatus PostStatus([FromBody] ProductStatus status) { return status; } + + [HttpGet("GetDoc")] + [Consumes(typeof(IChild), MediaTypeNames.Application.Json)] + [Produces(typeof(IChild))] + public IActionResult GetDoc([FromQuery] IChild query) + { + return null; + } + } + + /// The parent. + public interface IParent + { + /// The parent value. + string? ParentValue { get; set; } + } + + /// The child. + public interface IChild : IParent + { + /// The child value. + string? Value { get; set; } } public enum ProductStatus