Skip to content

Commit 4bd1c2d

Browse files
Render Swagger UI links in a fancier, more useful way
1 parent 22a775b commit 4bd1c2d

File tree

7 files changed

+155
-31
lines changed

7 files changed

+155
-31
lines changed

examples/AspNet/OData/OpenApiODataWebApiExample/Startup.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Asp.Versioning.Conventions;
66
using Asp.Versioning.OData;
77
using Microsoft.AspNet.OData.Extensions;
8+
using Microsoft.Extensions.Primitives;
89
using Microsoft.OData;
910
using Newtonsoft.Json.Serialization;
1011
using Owin;
@@ -131,25 +132,45 @@ public void Configuration( IAppBuilder builder )
131132
{
132133
description.AppendLine();
133134

135+
var rendered = false;
136+
134137
for ( var i = 0; i < policy.Links.Count; i++ )
135138
{
136139
var link = policy.Links[i];
137140

138141
if ( link.Type == "text/html" )
139142
{
140-
description.AppendLine();
141-
142-
if ( link.Title.HasValue )
143+
if ( !rendered )
143144
{
144-
description.Append( link.Title.Value ).Append( ": " );
145+
description.AppendLine();
146+
description.Append( "**Links**" );
147+
description.AppendLine();
148+
rendered = true;
145149
}
146150

147-
description.Append( link.LinkTarget.OriginalString );
151+
if ( StringSegment.IsNullOrEmpty( link.Title ) )
152+
{
153+
if ( link.LinkTarget.IsAbsoluteUri )
154+
{
155+
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
156+
}
157+
else
158+
{
159+
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
160+
description.AppendLine();
161+
}
162+
}
163+
else
164+
{
165+
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
166+
}
148167
}
149168
}
150169
}
151170
}
152171

172+
description.AppendLine();
173+
description.AppendLine( "**Additional Information**" );
153174
info.Version( group.Name, $"Sample API {group.ApiVersion}" )
154175
.Contact( c => c.Name( "Bill Mei" ).Email( "[email protected]" ) )
155176
.Description( description.ToString() )

examples/AspNet/OData/SomeOpenApiODataWebApiExample/Startup.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Asp.Versioning;
44
using Asp.Versioning.Conventions;
55
using Microsoft.AspNet.OData.Extensions;
6+
using Microsoft.Extensions.Primitives;
67
using Microsoft.OData;
78
using Newtonsoft.Json.Serialization;
89
using Owin;
@@ -105,25 +106,45 @@ public void Configuration( IAppBuilder builder )
105106
{
106107
description.AppendLine();
107108

109+
var rendered = false;
110+
108111
for ( var i = 0; i < policy.Links.Count; i++ )
109112
{
110113
var link = policy.Links[i];
111114

112115
if ( link.Type == "text/html" )
113116
{
114-
description.AppendLine();
115-
116-
if ( link.Title.HasValue )
117+
if ( !rendered )
117118
{
118-
description.Append( link.Title.Value ).Append( ": " );
119+
description.AppendLine();
120+
description.Append( "**Links**" );
121+
description.AppendLine();
122+
rendered = true;
119123
}
120124

121-
description.Append( link.LinkTarget.OriginalString );
125+
if ( StringSegment.IsNullOrEmpty( link.Title ) )
126+
{
127+
if ( link.LinkTarget.IsAbsoluteUri )
128+
{
129+
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
130+
}
131+
else
132+
{
133+
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
134+
description.AppendLine();
135+
}
136+
}
137+
else
138+
{
139+
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
140+
}
122141
}
123142
}
124143
}
125144
}
126145

146+
description.AppendLine();
147+
description.AppendLine( "**Additional Information**" );
127148
info.Version( group.Name, $"Sample API {group.ApiVersion}" )
128149
.Contact( c => c.Name( "Bill Mei" ).Email( "[email protected]" ) )
129150
.Description( description.ToString() )

examples/AspNet/WebApi/OpenApiWebApiExample/Startup.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Asp.Versioning;
44
using Asp.Versioning.Routing;
5+
using Microsoft.Extensions.Primitives;
56
using Owin;
67
using Swashbuckle.Application;
78
using System.IO;
@@ -10,6 +11,7 @@
1011
using System.Web.Http;
1112
using System.Web.Http.Description;
1213
using System.Web.Http.Routing;
14+
using static System.Net.Mime.MediaTypeNames;
1315

1416
/// <summary>
1517
/// Represents the startup process for the application.
@@ -88,25 +90,45 @@ public void Configuration( IAppBuilder builder )
8890
{
8991
description.AppendLine();
9092

93+
var rendered = false;
94+
9195
for ( var i = 0; i < policy.Links.Count; i++ )
9296
{
9397
var link = policy.Links[i];
9498

9599
if ( link.Type == "text/html" )
96100
{
97-
description.AppendLine();
98-
99-
if ( link.Title.HasValue )
101+
if ( !rendered )
100102
{
101-
description.Append( link.Title.Value ).Append( ": " );
103+
description.AppendLine();
104+
description.Append( "**Links**" );
105+
description.AppendLine();
106+
rendered = true;
102107
}
103108

104-
description.Append( link.LinkTarget.OriginalString );
109+
if ( StringSegment.IsNullOrEmpty( link.Title ) )
110+
{
111+
if ( link.LinkTarget.IsAbsoluteUri )
112+
{
113+
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
114+
}
115+
else
116+
{
117+
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
118+
description.AppendLine();
119+
}
120+
}
121+
else
122+
{
123+
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
124+
}
105125
}
106126
}
107127
}
108128
}
109129

130+
description.AppendLine();
131+
description.AppendLine( "**Additional Information**" );
110132
info.Version( group.Name, $"Example API {group.ApiVersion}" )
111133
.Contact( c => c.Name( "Bill Mei" ).Email( "[email protected]" ) )
112134
.Description( description.ToString() )

examples/AspNetCore/OData/ODataOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Asp.Versioning.ApiExplorer;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
7+
using Microsoft.Extensions.Primitives;
78
using Microsoft.OpenApi.Models;
89
using Swashbuckle.AspNetCore.SwaggerGen;
910
using System.Text;
@@ -63,25 +64,39 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
6364
{
6465
text.AppendLine();
6566

67+
var rendered = false;
68+
6669
for ( var i = 0; i < policy.Links.Count; i++ )
6770
{
6871
var link = policy.Links[i];
6972

7073
if ( link.Type == "text/html" )
7174
{
72-
text.AppendLine();
73-
74-
if ( link.Title.HasValue )
75+
if ( !rendered )
7576
{
76-
text.Append( link.Title.Value ).Append( ": " );
77+
text.Append( "<h4>Links</h4><ul>" );
78+
rendered = true;
7779
}
7880

81+
text.Append( "<li><a href=\"" );
7982
text.Append( link.LinkTarget.OriginalString );
83+
text.Append( "\">" );
84+
text.Append(
85+
StringSegment.IsNullOrEmpty( link.Title )
86+
? link.LinkTarget.OriginalString
87+
: link.Title.ToString() );
88+
text.Append( "</a></li>" );
8089
}
8190
}
91+
92+
if ( rendered )
93+
{
94+
text.Append( "</ul>" );
95+
}
8296
}
8397
}
8498

99+
text.Append( "<h4>Additional Information</h4>" );
85100
info.Description = text.ToString();
86101

87102
return info;

examples/AspNetCore/OData/SomeODataOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Asp.Versioning.ApiExplorer;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
7+
using Microsoft.Extensions.Primitives;
78
using Microsoft.OpenApi.Models;
89
using Swashbuckle.AspNetCore.SwaggerGen;
910
using System.Text;
@@ -63,25 +64,39 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
6364
{
6465
text.AppendLine();
6566

67+
var rendered = false;
68+
6669
for ( var i = 0; i < policy.Links.Count; i++ )
6770
{
6871
var link = policy.Links[i];
6972

7073
if ( link.Type == "text/html" )
7174
{
72-
text.AppendLine();
73-
74-
if ( link.Title.HasValue )
75+
if ( !rendered )
7576
{
76-
text.Append( link.Title.Value ).Append( ": " );
77+
text.Append( "<h4>Links</h4><ul>" );
78+
rendered = true;
7779
}
7880

81+
text.Append( "<li><a href=\"" );
7982
text.Append( link.LinkTarget.OriginalString );
83+
text.Append( "\">" );
84+
text.Append(
85+
StringSegment.IsNullOrEmpty( link.Title )
86+
? link.LinkTarget.OriginalString
87+
: link.Title.ToString() );
88+
text.Append( "</a></li>" );
8089
}
8190
}
91+
92+
if ( rendered )
93+
{
94+
text.Append( "</ul>" );
95+
}
8296
}
8397
}
8498

99+
text.Append( "<h4>Additional Information</h4>" );
85100
info.Description = text.ToString();
86101

87102
return info;

examples/AspNetCore/WebApi/MinimalOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Asp.Versioning.ApiExplorer;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
7+
using Microsoft.Extensions.Primitives;
78
using Microsoft.OpenApi.Models;
89
using Swashbuckle.AspNetCore.SwaggerGen;
910
using System.Text;
@@ -63,25 +64,39 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
6364
{
6465
text.AppendLine();
6566

67+
var rendered = false;
68+
6669
for ( var i = 0; i < policy.Links.Count; i++ )
6770
{
6871
var link = policy.Links[i];
6972

7073
if ( link.Type == "text/html" )
7174
{
72-
text.AppendLine();
73-
74-
if ( link.Title.HasValue )
75+
if ( !rendered )
7576
{
76-
text.Append( link.Title.Value ).Append( ": " );
77+
text.Append( "<h4>Links</h4><ul>" );
78+
rendered = true;
7779
}
7880

81+
text.Append( "<li><a href=\"" );
7982
text.Append( link.LinkTarget.OriginalString );
83+
text.Append( "\">" );
84+
text.Append(
85+
StringSegment.IsNullOrEmpty( link.Title )
86+
? link.LinkTarget.OriginalString
87+
: link.Title.ToString() );
88+
text.Append( "</a></li>" );
8089
}
8190
}
91+
92+
if ( rendered )
93+
{
94+
text.Append( "</ul>" );
95+
}
8296
}
8397
}
8498

99+
text.Append( "<h4>Additional Information</h4>" );
85100
info.Description = text.ToString();
86101

87102
return info;

examples/AspNetCore/WebApi/OpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Asp.Versioning.ApiExplorer;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
7+
using Microsoft.Extensions.Primitives;
78
using Microsoft.OpenApi.Models;
89
using Swashbuckle.AspNetCore.SwaggerGen;
910
using System.Text;
@@ -63,25 +64,39 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
6364
{
6465
text.AppendLine();
6566

67+
var rendered = false;
68+
6669
for ( var i = 0; i < policy.Links.Count; i++ )
6770
{
6871
var link = policy.Links[i];
6972

7073
if ( link.Type == "text/html" )
7174
{
72-
text.AppendLine();
73-
74-
if ( link.Title.HasValue )
75+
if ( !rendered )
7576
{
76-
text.Append( link.Title.Value ).Append( ": " );
77+
text.Append( "<h4>Links</h4><ul>" );
78+
rendered = true;
7779
}
7880

81+
text.Append( "<li><a href=\"" );
7982
text.Append( link.LinkTarget.OriginalString );
83+
text.Append( "\">" );
84+
text.Append(
85+
StringSegment.IsNullOrEmpty( link.Title )
86+
? link.LinkTarget.OriginalString
87+
: link.Title.ToString() );
88+
text.Append( "</a></li>" );
8089
}
8190
}
91+
92+
if ( rendered )
93+
{
94+
text.Append( "</ul>" );
95+
}
8296
}
8397
}
8498

99+
text.Append( "<h4>Additional Information</h4>" );
85100
info.Description = text.ToString();
86101

87102
return info;

0 commit comments

Comments
 (0)