Replace ProblemDetailsAttribute with ApiControllerAttribute

This commit is contained in:
Pranav K 2017-09-22 09:49:34 -07:00
parent 97fab8711a
commit 5d1603c37f
6 changed files with 13 additions and 28 deletions

View File

@ -5,15 +5,16 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace Microsoft.AspNetCore.Mvc.ApiExplorer namespace Microsoft.AspNetCore.Mvc.ApiExplorer
{ {
public class ProblemDetailsApiDescriptionProvider : IApiDescriptionProvider public class ApiControllerApiDescriptionProvider : IApiDescriptionProvider
{ {
private readonly IModelMetadataProvider _modelMetadaProvider; private readonly IModelMetadataProvider _modelMetadaProvider;
public ProblemDetailsApiDescriptionProvider(IModelMetadataProvider modelMetadataProvider) public ApiControllerApiDescriptionProvider(IModelMetadataProvider modelMetadataProvider)
{ {
_modelMetadaProvider = modelMetadataProvider; _modelMetadaProvider = modelMetadataProvider;
} }
@ -31,7 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.ApiExplorer
{ {
foreach (var apiDescription in context.Results) foreach (var apiDescription in context.Results)
{ {
if (!apiDescription.ActionDescriptor.FilterDescriptors.Any(f => f.Filter is ProblemDetailsAttribute)) if (!apiDescription.ActionDescriptor.FilterDescriptors.Any(f => f.Filter is IApiBehaviorMetadata))
{ {
continue; continue;
} }

View File

@ -27,7 +27,7 @@ namespace Microsoft.Extensions.DependencyInjection
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>()); ServiceDescriptor.Transient<IApiDescriptionProvider, DefaultApiDescriptionProvider>());
services.TryAddEnumerable( services.TryAddEnumerable(
ServiceDescriptor.Transient<IApiDescriptionProvider, ProblemDetailsApiDescriptionProvider>()); ServiceDescriptor.Transient<IApiDescriptionProvider, ApiControllerApiDescriptionProvider>());
} }
} }
} }

View File

@ -1,16 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// Adds an <see cref="IFilterMetadata"/> that indicates to the framework that the current action conforms to well-known API behavior.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class ProblemDetailsAttribute : Attribute, IFilterMetadata
{
}
}

View File

@ -1069,7 +1069,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public async Task ProblemDetails_AddsProblemAsDefaultErrorResult() public async Task ProblemDetails_AddsProblemAsDefaultErrorResult()
{ {
// Act // Act
var body = await Client.GetStringAsync("ApiExplorerProblemDetails/ActionWithoutParameters"); var body = await Client.GetStringAsync("ApiExplorerApiController/ActionWithoutParameters");
var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body); var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body);
// Assert // Assert
@ -1087,7 +1087,7 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
public async Task ProblemDetails_AddsProblemAsErrorResultForBadResult_WhenActionHasParameters() public async Task ProblemDetails_AddsProblemAsErrorResultForBadResult_WhenActionHasParameters()
{ {
// Act // Act
var body = await Client.GetStringAsync("ApiExplorerProblemDetails/ActionWithSomeParameters"); var body = await Client.GetStringAsync("ApiExplorerApiController/ActionWithSomeParameters");
var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body); var result = JsonConvert.DeserializeObject<List<ApiExplorerData>>(body);
// Assert // Assert
@ -1108,8 +1108,8 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
} }
[Theory] [Theory]
[InlineData("ApiExplorerProblemDetails/ActionWithIdParameter")] [InlineData("ApiExplorerApiController/ActionWithIdParameter")]
[InlineData("ApiExplorerProblemDetails/ActionWithIdSuffixParameter")] [InlineData("ApiExplorerApiController/ActionWithIdSuffixParameter")]
public async Task ProblemDetails_AddsProblemAsErrorResultForNotFoundResult_WhenActionHasAnIdParameters(string url) public async Task ProblemDetails_AddsProblemAsErrorResultForNotFoundResult_WhenActionHasAnIdParameters(string url)
{ {
// Act // Act

View File

@ -425,7 +425,7 @@ namespace Microsoft.AspNetCore.Mvc
new Type[] new Type[]
{ {
typeof(DefaultApiDescriptionProvider), typeof(DefaultApiDescriptionProvider),
typeof(ProblemDetailsApiDescriptionProvider), typeof(ApiControllerApiDescriptionProvider),
typeof(JsonPatchOperationsArrayProvider), typeof(JsonPatchOperationsArrayProvider),
} }
}, },

View File

@ -5,9 +5,9 @@ using Microsoft.AspNetCore.Mvc;
namespace ApiExplorerWebSite namespace ApiExplorerWebSite
{ {
[Route("ApiExplorerProblemDetails/[action]")] [Route("ApiExplorerApiController/[action]")]
[ProblemDetails] [ApiController]
public class ApiExplorerProblemDetailsController : Controller public class ApiExplorerApiController : Controller
{ {
public IActionResult ActionWithoutParameters() => Ok(); public IActionResult ActionWithoutParameters() => Ok();