-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3526 from abpframework/maliming/AbpRemoteServiceA…
…piDescriptionProvider Introducing AbpRemoteServiceApiDescriptionProvider
- Loading branch information
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...etCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpRemoteServiceApiDescriptionProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Abstractions; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using Microsoft.AspNetCore.Mvc.Formatters; | ||
using Microsoft.AspNetCore.Mvc.ModelBinding; | ||
using Microsoft.Extensions.Options; | ||
using Volo.Abp.DependencyInjection; | ||
using Volo.Abp.Reflection; | ||
|
||
namespace Volo.Abp.AspNetCore.Mvc.ApiExploring | ||
{ | ||
public class AbpRemoteServiceApiDescriptionProvider : IApiDescriptionProvider, ITransientDependency | ||
{ | ||
private readonly IModelMetadataProvider _modelMetadataProvider; | ||
private readonly MvcOptions _mvcOptions; | ||
private readonly AbpRemoteServiceApiDescriptionProviderOptions _options; | ||
|
||
public AbpRemoteServiceApiDescriptionProvider( | ||
IModelMetadataProvider modelMetadataProvider, | ||
IOptions<MvcOptions> mvcOptionsAccessor, | ||
IOptions<AbpRemoteServiceApiDescriptionProviderOptions> optionsAccessor) | ||
{ | ||
_modelMetadataProvider = modelMetadataProvider; | ||
_mvcOptions = mvcOptionsAccessor.Value; | ||
_options = optionsAccessor.Value; | ||
} | ||
|
||
public void OnProvidersExecuted(ApiDescriptionProviderContext context) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// The order -999 ensures that this provider is executed right after the | ||
/// Microsoft.AspNetCore.Mvc.ApiExplorer.DefaultApiDescriptionProvider. | ||
/// </summary> | ||
public int Order => -999; | ||
|
||
public void OnProvidersExecuting(ApiDescriptionProviderContext context) | ||
{ | ||
foreach (var apiResponseType in GetApiResponseTypes()) | ||
{ | ||
foreach (var result in context.Results.Where(x => IsRemoteService(x.ActionDescriptor))) | ||
{ | ||
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); | ||
} | ||
} | ||
} | ||
|
||
protected virtual IEnumerable<ApiResponseType> GetApiResponseTypes() | ||
{ | ||
foreach (var apiResponse in _options.SupportedResponseTypes) | ||
{ | ||
apiResponse.ModelMetadata = _modelMetadataProvider.GetMetadataForType(apiResponse.Type); | ||
|
||
foreach (var responseTypeMetadataProvider in _mvcOptions.OutputFormatters.OfType<IApiResponseTypeMetadataProvider>()) | ||
{ | ||
var formatterSupportedContentTypes = responseTypeMetadataProvider.GetSupportedContentTypes(null, apiResponse.Type); | ||
if (formatterSupportedContentTypes == null) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (var formatterSupportedContentType in formatterSupportedContentTypes) | ||
{ | ||
apiResponse.ApiResponseFormats.Add(new ApiResponseFormat | ||
{ | ||
Formatter = (IOutputFormatter) responseTypeMetadataProvider, | ||
MediaType = formatterSupportedContentType | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return _options.SupportedResponseTypes; | ||
} | ||
|
||
protected virtual bool IsRemoteService(ActionDescriptor actionDescriptor) | ||
{ | ||
var remoteServiceAttr = ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<RemoteServiceAttribute>(actionDescriptor.GetMethodInfo()); | ||
return remoteServiceAttr != null && remoteServiceAttr.IsEnabled; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpRemoteServiceApiDescriptionProviderOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
|
||
namespace Volo.Abp.AspNetCore.Mvc.ApiExploring | ||
{ | ||
public class AbpRemoteServiceApiDescriptionProviderOptions | ||
{ | ||
public HashSet<ApiResponseType> SupportedResponseTypes { get; set; } = new HashSet<ApiResponseType>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters