Skip to content

Commit

Permalink
Use api versioning deprecation flag, closes #2012
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Jul 2, 2019
1 parent 932f06b commit 42a8aed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace NSwag.Generation.AspNetCore.Tests.Web.Controllers
{
[ApiVersion("1")]
[ApiVersion("2")]
[ApiVersion("2", Deprecated = true)]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
[SwaggerTag("VersionedValues", Description = "Old operations")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using NSwag.Generation.AspNetCore.Tests.Web.Controllers;
using NSwag.Generation.Processors;
using Xunit;

namespace NSwag.Generation.AspNetCore.Tests
Expand Down Expand Up @@ -60,6 +59,7 @@ public async Task When_generating_v2_then_only_v2_operations_are_included()

Assert.Equal(2, operations.Count());
Assert.True(operations.All(o => o.Path.Contains("/v2/")));
Assert.True(operations.All(o => o.Operation.IsDeprecated));

// VersionedIgnoredValues tag should not be in json document
Assert.Equal(1, document.Tags.Count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//-----------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -159,7 +160,7 @@ private List<Type> GenerateForControllers(
Method = httpMethod,
Operation = new OpenApiOperation
{
IsDeprecated = method.GetCustomAttribute<ObsoleteAttribute>() != null,
IsDeprecated = IsOperationDeprecated(item.Item1, controllerActionDescriptor, method),
OperationId = GetOperationId(document, controllerActionDescriptor, method),
Consumes = apiDescription.SupportedRequestFormats
.Select(f => f.MediaType)
Expand Down Expand Up @@ -189,6 +190,25 @@ private List<Type> GenerateForControllers(
return usedControllerTypes;
}

private bool IsOperationDeprecated(ApiDescription apiDescription, ControllerActionDescriptor controllerActionDescriptor, MethodInfo methodInfo)
{
if (methodInfo.GetCustomAttribute<ObsoleteAttribute>() != null)
{
return true;
}

dynamic apiVersionModel = controllerActionDescriptor?
.Properties
.FirstOrDefault(p => p.Key is Type type && type.Name == "ApiVersionModel")
.Value;

var isDeprecated = (bool)(apiVersionModel != null) &&
((IEnumerable)apiVersionModel.DeprecatedApiVersions).OfType<object>()
.Any(v => v.ToString() == apiDescription.GroupName);

return isDeprecated;
}

private List<Tuple<OpenApiOperationDescription, ApiDescription, MethodInfo>> AddOperationDescriptionsToDocument(
OpenApiDocument document, Type controllerType,
List<Tuple<OpenApiOperationDescription, ApiDescription, MethodInfo>> operations,
Expand Down

0 comments on commit 42a8aed

Please sign in to comment.