Skip to content

Commit 4b52711

Browse files
Update Web API examples
1 parent 0013cba commit 4b52711

3 files changed

Lines changed: 148 additions & 133 deletions

File tree

examples/AspNet/OData/OpenApiODataWebApiExample/Startup.cs

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ public void Configuration( IAppBuilder builder )
3737
// "api-supported-versions" and "api-deprecated-versions"
3838
options.ReportApiVersions = true;
3939

40+
options.Policies.Deprecate( 0.9 )
41+
.Effective( DateTimeOffset.Now )
42+
.Link( "policy.html" )
43+
.Title( "Version Deprecation Policy" )
44+
.Type( "text/html" );
45+
4046
options.Policies.Sunset( 0.9 )
4147
.Effective( DateTimeOffset.Now.AddDays( 60 ) )
4248
.Link( "policy.html" )
43-
.Title( "Versioning Policy" )
49+
.Title( "Version Sunset Policy" )
4450
.Type( "text/html" );
4551
} );
4652

@@ -113,64 +119,92 @@ public void Configuration( IAppBuilder builder )
113119
foreach ( var group in apiExplorer.ApiDescriptions )
114120
{
115121
var description = new StringBuilder( "A sample application with OData, OpenAPI, Swashbuckle, and API versioning." );
122+
var links = new List<LinkHeaderValue>();
116123

117124
if ( group.IsDeprecated )
118125
{
119-
description.Append( " This API version has been deprecated." );
126+
description.Append( " The API " );
127+
128+
if ( group.DeprecationPolicy?.Date is { } when )
129+
{
130+
description.Append( when < DateTimeOffset.Now ? "will be" : "was" )
131+
.Append( " deprecated on " )
132+
.Append( when.Date.ToShortDateString() );
133+
}
134+
else
135+
{
136+
description.Append( "has been deprecated" );
137+
}
138+
139+
description.Append( '.' );
140+
141+
if ( group.DeprecationPolicy is { } deprecation && deprecation.HasLinks )
142+
{
143+
links.AddRange( deprecation.Links );
144+
}
120145
}
121146

122-
if ( group.SunsetPolicy is { } policy )
147+
if ( group.SunsetPolicy is { } sunset )
123148
{
124-
if ( policy.Date is { } when )
149+
if ( sunset.Date is { } when )
125150
{
126-
description.Append( " The API will be sunset on " )
151+
description.Append( " The API " )
152+
.Append( when < DateTimeOffset.Now ? "will be" : "was" )
153+
.Append( " sunset on " )
127154
.Append( when.Date.ToShortDateString() )
128155
.Append( '.' );
129156
}
130157

131-
if ( policy.HasLinks )
158+
if ( sunset.HasLinks )
132159
{
133-
description.AppendLine();
160+
links.AddRange( sunset.Links );
161+
}
162+
}
134163

135-
var rendered = false;
164+
description.AppendLine();
136165

137-
for ( var i = 0; i < policy.Links.Count; i++ )
166+
if ( links.Count > 0 )
167+
{
168+
var rendered = false;
169+
170+
for ( var i = 0; i < links.Count; i++ )
171+
{
172+
var link = links[i];
173+
174+
if ( link.Type != "text/html" )
175+
{
176+
continue;
177+
}
178+
179+
if ( !rendered )
138180
{
139-
var link = policy.Links[i];
181+
description.Append( "<br/><br/>" );
182+
description.Append( "**Links**" );
183+
description.AppendLine( "<br/>" );
184+
rendered = true;
185+
}
140186

141-
if ( link.Type == "text/html" )
187+
if ( StringSegment.IsNullOrEmpty( link.Title ) )
188+
{
189+
if ( link.LinkTarget.IsAbsoluteUri )
190+
{
191+
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
192+
}
193+
else
142194
{
143-
if ( !rendered )
144-
{
145-
description.AppendLine();
146-
description.Append( "**Links**" );
147-
description.AppendLine();
148-
rendered = true;
149-
}
150-
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-
}
195+
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
196+
description.AppendLine();
167197
}
168198
}
199+
else
200+
{
201+
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
202+
}
169203
}
170204
}
171205

172-
description.AppendLine();
173-
description.AppendLine( "**Additional Information**" );
206+
description.AppendLine().AppendLine( "<br/>" );
207+
description.AppendLine( "**Additional Information**<br/>" );
174208
info.Version( group.Name, $"Sample API {group.ApiVersion}" )
175209
.Contact( c => c.Name( "Bill Mei" ).Email( "bill.mei@somewhere.com" ) )
176210
.Description( description.ToString() )

examples/AspNet/OData/SomeOpenApiODataWebApiExample/Startup.cs

Lines changed: 6 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -85,65 +85,12 @@ public void Configuration( IAppBuilder builder )
8585
{
8686
foreach ( var group in apiExplorer.ApiDescriptions )
8787
{
88-
var description = new StringBuilder( "A sample application with some OData, OpenAPI, Swashbuckle, and API versioning." );
89-
90-
if ( group.IsDeprecated )
91-
{
92-
description.Append( " This API version has been deprecated." );
93-
}
94-
95-
if ( group.SunsetPolicy is { } policy )
96-
{
97-
if ( policy.Date is { } when )
98-
{
99-
description.Append( " The API will be sunset on " )
100-
.Append( when.Date.ToShortDateString() )
101-
.Append( '.' );
102-
}
103-
104-
if ( policy.HasLinks )
105-
{
106-
description.AppendLine();
107-
108-
var rendered = false;
109-
110-
for ( var i = 0; i < policy.Links.Count; i++ )
111-
{
112-
var link = policy.Links[i];
113-
114-
if ( link.Type == "text/html" )
115-
{
116-
if ( !rendered )
117-
{
118-
description.AppendLine();
119-
description.Append( "**Links**" );
120-
description.AppendLine();
121-
rendered = true;
122-
}
123-
124-
if ( StringSegment.IsNullOrEmpty( link.Title ) )
125-
{
126-
if ( link.LinkTarget.IsAbsoluteUri )
127-
{
128-
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
129-
}
130-
else
131-
{
132-
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
133-
description.AppendLine();
134-
}
135-
}
136-
else
137-
{
138-
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
139-
}
140-
}
141-
}
142-
}
143-
}
144-
145-
description.AppendLine();
146-
description.AppendLine( "**Additional Information**" );
88+
var description = new StringBuilder( "A sample application with some OData, OpenAPI, Swashbuckle, and API versioning." )
89+
.AppendLine()
90+
.AppendLine( "<br/><br/>" )
91+
.AppendLine( "**Additional Information**" )
92+
.AppendLine( "<br/>" );
93+
14794
info.Version( group.Name, $"Sample API {group.ApiVersion}" )
14895
.Contact( c => c.Name( "Bill Mei" ).Email( "bill.mei@somewhere.com" ) )
14996
.Description( description.ToString() )

examples/AspNet/WebApi/OpenApiWebApiExample/Startup.cs

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ public void Configuration( IAppBuilder builder )
3737
// "api-supported-versions" and "api-deprecated-versions"
3838
options.ReportApiVersions = true;
3939

40+
options.Policies.Deprecate( 0.9 )
41+
.Effective( DateTimeOffset.Now )
42+
.Link( "policy.html" )
43+
.Title( "Version Deprecation Policy" )
44+
.Type( "text/html" );
45+
4046
options.Policies.Sunset( 0.9 )
4147
.Effective( DateTimeOffset.Now.AddDays( 60 ) )
4248
.Link( "policy.html" )
43-
.Title( "Versioning Policy" )
49+
.Title( "Version Sunset Policy" )
4450
.Type( "text/html" );
4551
} );
4652
configuration.MapHttpAttributeRoutes( constraintResolver );
@@ -70,64 +76,92 @@ public void Configuration( IAppBuilder builder )
7076
foreach ( var group in apiExplorer.ApiDescriptions )
7177
{
7278
var description = new StringBuilder( "A sample application with OpenAPI, Swashbuckle, and API versioning." );
79+
var links = new List<LinkHeaderValue>();
7380

7481
if ( group.IsDeprecated )
7582
{
76-
description.Append( " This API version has been deprecated." );
83+
description.Append( " The API " );
84+
85+
if ( group.DeprecationPolicy?.Date is { } when )
86+
{
87+
description.Append( when < DateTimeOffset.Now ? "will be" : "was" )
88+
.Append( " deprecated on " )
89+
.Append( when.Date.ToShortDateString() );
90+
}
91+
else
92+
{
93+
description.Append( "has been deprecated" );
94+
}
95+
96+
description.Append( '.' );
97+
98+
if ( group.DeprecationPolicy is { } deprecation && deprecation.HasLinks )
99+
{
100+
links.AddRange( deprecation.Links );
101+
}
77102
}
78103

79-
if ( group.SunsetPolicy is { } policy )
104+
if ( group.SunsetPolicy is { } sunset )
80105
{
81-
if ( policy.Date is { } when )
106+
if ( sunset.Date is { } when )
82107
{
83-
description.Append( " The API will be sunset on " )
108+
description.Append( " The API " )
109+
.Append( when < DateTimeOffset.Now ? "will be" : "was" )
110+
.Append( " sunset on " )
84111
.Append( when.Date.ToShortDateString() )
85112
.Append( '.' );
86113
}
87114

88-
if ( policy.HasLinks )
115+
if ( sunset.HasLinks )
89116
{
90-
description.AppendLine();
117+
links.AddRange( sunset.Links );
118+
}
119+
}
91120

92-
var rendered = false;
121+
description.AppendLine();
93122

94-
for ( var i = 0; i < policy.Links.Count; i++ )
123+
if ( links.Count > 0 )
124+
{
125+
var rendered = false;
126+
127+
for ( var i = 0; i < links.Count; i++ )
128+
{
129+
var link = links[i];
130+
131+
if ( link.Type != "text/html" )
132+
{
133+
continue;
134+
}
135+
136+
if ( !rendered )
95137
{
96-
var link = policy.Links[i];
138+
description.Append( "<br/><br/>" );
139+
description.Append( "**Links**" );
140+
description.AppendLine( "<br/>" );
141+
rendered = true;
142+
}
97143

98-
if ( link.Type == "text/html" )
144+
if ( StringSegment.IsNullOrEmpty( link.Title ) )
145+
{
146+
if ( link.LinkTarget.IsAbsoluteUri )
147+
{
148+
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
149+
}
150+
else
99151
{
100-
if ( !rendered )
101-
{
102-
description.AppendLine();
103-
description.Append( "**Links**" );
104-
description.AppendLine();
105-
rendered = true;
106-
}
107-
108-
if ( StringSegment.IsNullOrEmpty( link.Title ) )
109-
{
110-
if ( link.LinkTarget.IsAbsoluteUri )
111-
{
112-
description.AppendLine( $"- {link.LinkTarget.OriginalString}" );
113-
}
114-
else
115-
{
116-
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
117-
description.AppendLine();
118-
}
119-
}
120-
else
121-
{
122-
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
123-
}
152+
description.AppendFormat( "- <a href=\"{0}\">{0}</a>", link.LinkTarget.OriginalString );
153+
description.AppendLine();
124154
}
125155
}
156+
else
157+
{
158+
description.AppendLine( $"- [{link.Title}]({link.LinkTarget.OriginalString})" );
159+
}
126160
}
127161
}
128162

129-
description.AppendLine();
130-
description.AppendLine( "**Additional Information**" );
163+
description.AppendLine().AppendLine( "<br/>" );
164+
description.AppendLine( "**Additional Information**<br/>" );
131165
info.Version( group.Name, $"Example API {group.ApiVersion}" )
132166
.Contact( c => c.Name( "Bill Mei" ).Email( "bill.mei@somewhere.com" ) )
133167
.Description( description.ToString() )

0 commit comments

Comments
 (0)