Have you heard of Deep Linking in Swagger?

.NET CORE & SWAGGER PROGRAMMING

Deep linking is using a URL to get to a specific piece of content. A deep link is a URL that can be parsed by an application, for particular data & app navigation.

This article demonstrates steps to enable deep linking in swagger UI inside a .Net Core API.

What is deep linking?

Deep linking allows the user to provide UI fragments at runtime, i.e., it will automatically expand and scroll to specific tag & operation.

Similarly, collapsing the UI, both tag and operation will be removed from UI.

The below example demonstrates how deep linking looks like in swagger URL.

Deep Linking URL example

Fragment formats:

  • #/{tagName} : trigger focus to specific tag.
  • #/{tagName}/{operationId} : trigger focus to specific operation inside a tag.

NOTE: By default, deep linking is disabled.

Pros

  • Consider a scenario where there are too many tag names & operation ids inside a swagger UI. In that case, deep linking is beneficial to share particular operations inside a tag name with other developers.
  • Fragmentation is more flexible & allows us to focus on a particular section.

Demo

Please find the below-attached demo. Observe URL changes with each expands and collapse.

Deep Linking Demo

Prerequisites

How to enable Swagger UI in a .Net Core solution?

View at Medium.com

How to enable deep linking?

Please find below-attached code, which shows deep linking can be enabled by calling out EnableDeepLinking() method.

app.UseSwaggerUI(c =>{    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger Configuration V1");    
c.EnableDeepLinking();
c.DisplayOperationId();
});

NOTE: DisplayOperationId() function is used to display operation ids on swagger UI.

GitHub Sample

View at Medium.com


Thank you for reading. Keep visiting and share this in your network. Please put your thoughts and feedback in the comments section.

Follow me on LinkedIn Instagram Facebook Twitter

Leave a comment