[Swashbuckle and API Versioning] Obsolete attribute is not rendered as deprecated in Swagger UI
Tags: aspnetcore, dotnet
Published: 2019-05-22 15:00:28

---
title: [Swashbuckle and API Versioning] Obsolete attribute is not rendered as deprecated in Swagger UI
published: true
tags: aspnetcore,dotnet
canonical_url: https://medium.com/@joni2nja/swashbuckle-and-api-versioning-obsolete-attribute-is-not-rendered-as-deprecated-in-swagger-ui-c44415111085
---
This post originally appeared on [Medium](https://medium.com/@joni2nja/swashbuckle-and-api-versioning-obsolete-attribute-is-not-rendered-as-deprecated-in-swagger-ui-c44415111085)

---

### Upgraded ASP.NET Core 2.1 to 2.2: [Swashbuckle and API Versioning] Obsolete attribute is not rendered as deprecated in Swagger UI

#### Experiencing Obsolete action is not rendered as deprecated in Swagger UI?

So you’ve upgraded your ASP.NET Core 2.1 app to 2.2 and you are using:

- [Swashbuckle.AspNetCore](https://github.com/domaindrivendev/Swashbuckle.AspNetCore) version 3.x
- [API versioning (Microsoft.AspNetCore.Mvc.Versioning)](https://github.com/Microsoft/aspnet-api-versioning) version 3.x

AND, you have one or more of your API controller **_actions_** decorated as `Obsolete`?

Chances are, Swagger UI will render your `Obsolete` actions (endpoints) as ordinary API endpoints, not as “deprecated” as you would expect.

![](https://cdn-images-1.medium.com/max/728/1*vdw7IPfcOtyBR60vrznVzw.png)<figcaption>“Delete” action method marked as Obsolete</figcaption>

![](https://cdn-images-1.medium.com/max/947/1*n-aRhnMVz4Xn1O6MJxZGXA.png)<figcaption>DELETE /api/v3/People/{id} should be rendered as “deprecated”…</figcaption>

So, what’s wrong? I just followed the change history from the [API versioning](https://github.com/Microsoft/aspnet-api-versioning) sample:

[Microsoft/aspnet-api-versioning](https://github.com/Microsoft/aspnet-api-versioning/blob/1a1a6b6629ee0801d3334fbceeb8412aa3b7ce1f/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs)

We can even see the git change history with a [cool animation](https://github.com/pomber/git-history) like this:

![](https://cdn-images-1.medium.com/max/1024/1*04OPJR_wGT4fQo44FzRIJQ.gif)

Use this [link](https://github.githistory.xyz/Microsoft/aspnet-api-versioning/blob/1a1a6b6629ee0801d3334fbceeb8412aa3b7ce1f/samples/aspnetcore/SwaggerSample/SwaggerDefaultValues.cs) to see it yourself.

Still here? Okay, so here we have this line:

```
operation.Deprecated = apiDescription.IsDeprecated();
```

It should do the work to deprecate the endpoints. But there is one flaw. It is _overwriting the previous value_ assigned somewhere by `SwaggerGenerator`.

![](https://cdn-images-1.medium.com/max/1024/1*AHeDsXLX8TiuNbH2zpsqAw.png)<figcaption>Deprecated is true before the evaluation of “apiDescription.IsDeprecated()”</figcaption>

Here we can see that `operation.Deprecated` is already `true!` Let’s see what was happened before this.

![](https://cdn-images-1.medium.com/max/1024/1*kOxBeGnbkCDHLLz4NsTfuw.png)<figcaption><a href="https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/192aa2d70f057fc2805f58d51b6e859f1f6b07ce/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SwaggerGenerator.cs">https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/192aa2d70f057fc2805f58d51b6e859f1f6b07ce/src/Swashbuckle.AspNetCore.SwaggerGen/Generator/SwaggerGenerator.cs</a></figcaption>

After executing `apiDescription.IsDeprecated()`, the result will be `false`. Bingo. We should prevent it from executing if it’s already `true`.

Instead of `=`, we could [short-circuit](https://en.wikipedia.org/wiki/Short-circuit_evaluation) it by using `|=` so that once it has been marked as `true` — deprecated, we won’t even bother to look at it.

Now, the Swagger UI should render as expected.

![](https://cdn-images-1.medium.com/max/939/1*TTH9IjJJsYKiY_v2hgncIw.png)<figcaption>DELETE /api/v3/People/{id} is rendered as “deprecated”</figcaption>

Problem solved!

I have spent a few hours digging into this wondering what the culprit was, since it was working before the upgrade. Is the sample code provided by the [API versioning](https://github.com/Microsoft/aspnet-api-versioning) wrong? Not really. It’s merely a sample. Please understand that the sample won’t cover all of the possible cases. So we can’t blame it. But hey, I think the sample could be better. I know that this might be an edge case, but I will try to report it to the repo owner and maybe send a Pull Request to fix the sample code in the hope that it will save hours of debugging time of future users!

_[_ **_Update_** _: I opened the issue here:_ [_https://github.com/microsoft/aspnet-api-versioning/issues/470_](https://github.com/microsoft/aspnet-api-versioning/issues/470)_]_