Browse Source

Filter ProducesResponseType.

pull/3526/head
maliming 7 years ago
parent
commit
a04805a607
  1. 22
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpRemoteServiceApiDescriptionProvider.cs
  2. 17
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs

22
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpRemoteServiceApiDescriptionProvider.cs

@ -3,7 +3,6 @@ using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.Options;
@ -42,9 +41,18 @@ namespace Volo.Abp.AspNetCore.Mvc.ApiExploring
{
foreach (var apiResponseType in GetApiResponseTypes())
{
foreach (var result in context.Results.Where(x => ShouldAddResponseTypes(x.ActionDescriptor)))
foreach (var result in context.Results.Where(x => IsRemoteService(x.ActionDescriptor)))
{
result.SupportedResponseTypes.AddIfNotContains(x => x.StatusCode == apiResponseType.StatusCode, () => apiResponseType);
var actionProducesResponseTypeAttributes =
ReflectionHelper.GetAttributesOfMemberOrDeclaringType<ProducesResponseTypeAttribute>(
result.ActionDescriptor.GetMethodInfo());
if (actionProducesResponseTypeAttributes.Any(x => x.StatusCode == apiResponseType.StatusCode))
{
continue;
}
result.SupportedResponseTypes.AddIfNotContains(x => x.StatusCode == apiResponseType.StatusCode,
() => apiResponseType);
}
}
}
@ -77,14 +85,8 @@ namespace Volo.Abp.AspNetCore.Mvc.ApiExploring
return _options.SupportedResponseTypes;
}
protected virtual bool ShouldAddResponseTypes(ActionDescriptor actionDescriptor)
protected virtual bool IsRemoteService(ActionDescriptor actionDescriptor)
{
if (ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<ProducesResponseTypeAttribute>(actionDescriptor.GetMethodInfo()) != null ||
ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<ProducesErrorResponseTypeAttribute>(actionDescriptor.GetMethodInfo()) != null)
{
return false;
}
var remoteServiceAttr = ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<RemoteServiceAttribute>(actionDescriptor.GetMethodInfo());
return remoteServiceAttr != null && remoteServiceAttr.IsEnabled;
}

17
framework/src/Volo.Abp.Core/Volo/Abp/Reflection/ReflectionHelper.cs

@ -108,6 +108,23 @@ namespace Volo.Abp.Reflection
?? defaultValue;
}
/// <summary>
/// Tries to gets attributes defined for a class member and it's declaring type including inherited attributes.
/// </summary>
/// <typeparam name="TAttribute">Type of the attribute</typeparam>
/// <param name="memberInfo">MemberInfo</param>
/// <param name="inherit">Inherit attribute from base classes</param>
public static IEnumerable<TAttribute> GetAttributesOfMemberOrDeclaringType<TAttribute>(MemberInfo memberInfo, bool inherit = true)
where TAttribute : class
{
var customAttributes = memberInfo.GetCustomAttributes(true).OfType<TAttribute>();
var declaringTypeCustomAttributes =
memberInfo.DeclaringType?.GetTypeInfo().GetCustomAttributes(true).OfType<TAttribute>();
return declaringTypeCustomAttributes != null
? customAttributes.Concat(declaringTypeCustomAttributes).Distinct()
: customAttributes;
}
/// <summary>
/// Gets value of a property by it's full path from given object
/// </summary>

Loading…
Cancel
Save