Add a Custom Description In Swagger For Each API Version #991
Replies: 4 comments 1 reply
-
I'm running into a very similar issue. Is there a way in the current package version to get descriptive text included at the top of the API page in swagger next to my api-level deprecated / sunset content by way of the current ApiVersionDescription object? I'd think there should be, if only to enhance the swagger page with explanatory text on how to use (and how not to use) the APIs we're writing. |
Beta Was this translation helpful? Give feedback.
-
Interesting request. I'm trying to envision how this would be configured. API Versioning very intentionally limits its extension points to the API Explorer surface area and only bridges gaps that are not otherwise supportable; for example, deprecation and sunset policies. The extensions can be (and most often are) used for OpenAPI, but that is not their objective. There are scenarios outside of documentation (ex: test automation) that they are useful for. The goal has always been to derive as much from convention as possible. A description for an API cannot really be defined. This is already an issue without API Versioning, but is further conflated by a combination of API + API version. Honestly, this type of issue feels more in the realm of OpenAPI/Swagger than API Versioning. I'm not saying "No", but I need to understand what this would look like. Swashbuckle already provides a couple of ways to provide detailed API-level documentation. Why wouldn't you just use that approach? If you need more than can be conveyed in the description, you'll need custom code as you already have. If the built-in description capabilities don't work, this is where you could add that hook today. There's only two limitations in the current way Swashbuckle would pick up the description from today. First, if you interleave API versions on a single type or endpoint and want different descriptions, that's not supported. The easiest solution there is to simply split the APIs out into different controllers or endpoint mappings. The other issue would be if you want more control over the order and arrangement of the text. The simplest solution there seems to be back at creating custom code to build the description the way you want. It appears that even if you had a new description property, you'd still need user-defined code to stitch the pieces together. Since it would only be used for OpenAPI descriptions (best as I can tell), the extension points in the OpenAPI generator such as Swashbuckle or NSwag are better suited to address it. |
Beta Was this translation helpful? Give feedback.
-
@kwinmill7 , not sure if this is relevant, but in my case I add the change log for each version to the OpenAPI document's description. To do this, each version has a class which returns the markdown string for the entire description property.
Example of version 3.1
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'm looking for a way to add a custom description or comment to each API version which could then be added to the top of the swagger documentation in OpenApiInfo.Description.
I've been trying to find if that use case is possible using the existing package. Adding a status to the version almost meets my needs in order to convey a message to consumers of the API, but I want to convey a message through the description instead and avoid the status being included in the API endpoint URLs, as this would require subsequent code changes by consumers when the status is finalized. My preference would be to have a property added to ApiVersionDescription that could be used to add this custom description, which I believes resolves this in a simple manner that is in line with the rest of the package's logic. The custom description would be added to the OpenApiInfo.Description similar to how the SunsetPolicy/IsDeprecated properties allow us to update the Description. My sample code snippet below follows the CreateInfoForApiVersion method here: https://github.com/dotnet/aspnet-api-versioning/blob/main/examples/AspNetCore/WebApi/OpenApiExample/ConfigureSwaggerOptions.cs. At the end I've added use of a CustomDescription property that doesn't exist right now, but is what I'm looking for. The specific name of the property is less important, of course, than the functionality - it could be called 'comment'.
I found a discussion that is very similar to what I'm looking for: #899.
In my situation I loop through provider.ApiVersionDescriptions and call CreateInfoForApiVersion from an in-house nuget package that is used by all of our APIs. I'm hoping to leave this generic code in place, but be able to create in AddAPIVersioning a per-version property I can consume when I process each iteration of this loop. A string would work well, a dictionary<string,string> would also work and may be more future-proof should we need more than one property.
Beta Was this translation helpful? Give feedback.
All reactions