aspnet-api-versioning icon indicating copy to clipboard operation
aspnet-api-versioning copied to clipboard

Can't deprecate a action in a controller - ApiVersionAttribute is not applicable and MapToApiVersion does not have Deprecated value that can be read from it.

Open KRZ303 opened this issue 9 years ago • 4 comments

I want to show info about what controllers or actions are deprecated on the API Help Page. This can be easily done for whole deprecated controllers but not for single actions.

Deprecating a controller is easy [ApiVersion("1", Deprecated = true)] We can later retrieve that info using apiDescription.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<ApiVersionAttribute>(); and check attributes inside using attribute.Deprecated.

This does not work for actions in a controller. ApiVersionAttribute is applicable only to classes (controllers) so methods can't be decorated with it. Actions can be decorated with MapToApiVersionAttribute but it doesn't enable us to check if this action is deprecated. It doesn't have Deprecated property like ApiVersionAttribute that we can easily retrieve from apiDescription. And it doesn't have any method to return ApiVersionAttribute it maps to - on which the we could check for deprecation.

KRZ303 avatar Sep 29 '17 14:09 KRZ303

I see how this might be confusing. Let me try to help you get sorted out.

First, let's talk semantics. An action is not a service; it's the operation of a service. You can version a service. You cannot version individual actions. In a much earlier version of the framework (before GitHub), both attributes were simply called [ApiVersion]; however, this led to a lot of confusion. Many authors thought that you can version individual actions. In addition, the Deprecated member for actions did nothing. To alleviate that and make things more clear, the action-level attribute was renamed to [MapToApiVersion] to help make it clear that an action maps to a particular service version, but doesn't define it. Functionally, the two attributes provide nearly the same metadata and both implement IApiVersionProvider, which is what the framework cares about. How they are used is though is different.

On that note, I should point out that while you can use the attributes for your own scenarios, you should know that the framework doesn't directly care about attributes; it cares about IApiVersionProvider. That is how the conventions configuration approach works without any attributes. Furthermore, you don't even need to use the out-of-the-box attributes, you can roll your own if you really wanted to. The framework of course, will still ignore IApiVersionProvider.Deprecated on actions though. ;)

I haven't really invested much in the Web API Help Page because most developers seem to be preferring Swagger these days. If there is enough demand, I might consider it, but as far as I can tell, the Help Pages usage is on the decline. Nevertheless, you can still use the IApiExplorer to generate your pages. It would seem that you are not using the API Explorer package that I provide. It was meant to support the Swagger use case, but there's nothing Swagger-specific about it. You can easily use it for your scenario. In fact, I would recommend using it over trying collate everything yourself.

The VersionedApiExplorer will know how to collect all the API version information correctly and:

  • Collate all APIs into groups by API version
  • Determine whether an entire API version is deprecated (e.g. all services)
  • Determine whether a specific service/API is deprecated, including all of it's actions

Once you've added the Microsoft.AspNet.WebApi.Versioning.ApiExplorer package, you use it like this:

// register and optionally hold on to the instance
var apiExplorer = configuration.AddVersionedApiExplorer();

OR

// register the API explorer
configuration.AddVersionedApiExplorer();
// cast to the strong-type to get all of the easy grouping features
var apiExplorer = (VersionedApiExplorer) configuration.Services.GetApiExplorer();

I hope that helps get you started. Let me know if you have more questions.

commonsensesoftware avatar Sep 29 '17 16:09 commonsensesoftware

Hi there,

I really appreciate your feedback!

Now it makes sense to me after what you've sad. Thanks for detailed explanation. I will look into Api Explorer - I don't know what is used now. I just started getting to know our Web Framework in our enterprise product and wanted to make a few requested improvements. Hopefully using swagger will be an option ;)

Take care! Krzysztof

29.09.2017 18:08 "Chris Martinez" [email protected] napisał(a):

I see how this might be confusing. Let me try to help you get sorted out.

First, let's talk semantics. An action is not a service; it's the operation of a service. You can version a service. You cannot version individual actions. In a much earlier version of the framework (before GitHub), both attributes were simply called [ApiVersion]; however, this led to a lot of confusion. Many authors thought that you can version individual actions. In addition, the Deprecated member for actions did nothing. To alleviate that and make things more clear, the action-level attribute was renamed to [MapToApiVersion] to help make it clear that an action maps to a particular service version, but doesn't define it. Functionally, the two attributes provide nearly the same metadata and both implement IApiVersionProvider, which is what the framework cares about. How they are used is though is different.

On that note, I should point out that while you can use the attributes for your own scenarios, you should know that the framework doesn't directly care about attributes; it cares about IApiVersionProvider. That is how the conventions configuration approach works without any attributes. Furthermore, you don't even need to use the out-of-the-box attributes, you can roll your own if you really wanted to. The framework of course, will still ignore IApiVersionProvider.Deprecated on actions though. ;)

I haven't really invested much in the Web API Help Page because most developers seem to be preferring Swagger these days. If there is enough demand, I might consider it, but as far as I can tell, the Help Pages usage is on the decline. Nevertheless, you can still use the IApiExplorer to generate your pages. It would seem that you are not using the API Explorer package that I provide. It was meant to support the Swagger use case, but there's nothing Swagger-specific about it. You can easily use it for your scenario. In fact, I would recommend using it over trying collate everything yourself.

The VersionedApiExplorer will know how to collect all the API version information correctly and:

  • Collate all APIs into groups by API version
  • Determine whether an entire API version is deprecated (e.g. all services)
  • Determine whether a specific service/API is deprecated, including all of it's actions

Once you've added the Microsoft.AspNet.WebApi.Versioning.ApiExplorer https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Versioning.ApiExplorer/ package, you use it like this:

// register and optionally hold on to the instancevar apiExplorer = configuration.AddVersionedApiExplorer();

OR

// register the API explorerconfiguration.AddVersionedApiExplorer();// cast to the strong-type to get all of the easy grouping featuresvar apiExplorer = (VersionedApiExplorer) configuration.Services.GetApiExplorer();

I hope that helps get you started. Let me know if you have more questions.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Microsoft/aspnet-api-versioning/issues/197#issuecomment-333169070, or mute the thread https://github.com/notifications/unsubscribe-auth/AEUriGn1jIT0krq7BZZlbfqAR8ZP-Z6hks5snRYXgaJpZM4Pow6d .

KRZ303 avatar Sep 29 '17 17:09 KRZ303

Good to hear. By default, Web API provides its own implementation of IApiExplorer. This implementation is not API version-aware. By adding this additional package, you'll get one that is aware. The call to AddVersionedApiExplorer will replace the default implementation.

There's a complete end-to-end Swagger example for Web API if you want to take a look. Even if you're not using Swagger, it's worth looking the Startup.cs so you can see how the VersionedApiExplorer is used. It will definitely be relevant to your scenario.

Let me know if you get stuck or run into something that doesn't support your scenario.

commonsensesoftware avatar Sep 29 '17 18:09 commonsensesoftware

Seems like you are on your way. If you continue to have issues or have new questions, feel free to come back. Thanks.

commonsensesoftware avatar Oct 13 '17 15:10 commonsensesoftware