Skip to content

Commit 4c5c545

Browse files
author
Matthias Gessinger
committed
Include DeprecationPolicy in Swagger generation
1 parent fb39354 commit 4c5c545

File tree

17 files changed

+508
-50
lines changed

17 files changed

+508
-50
lines changed

examples/AspNetCore/OData/ODataOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,84 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
5050
text.Append( " This API version has been deprecated." );
5151
}
5252

53-
if ( description.SunsetPolicy is { } policy )
53+
if ( description.DeprecationPolicy is { } deprecationPolicy )
5454
{
55-
if ( policy.Date is { } when )
55+
if ( deprecationPolicy.Date is { } when )
5656
{
57-
text.Append( " The API will be sunset on " )
58-
.Append( when.Date.ToShortDateString() )
59-
.Append( '.' );
57+
if ( when < DateTime.Now )
58+
{
59+
text.Append( " The API has been deprecated on " )
60+
.Append( when.Date.ToShortDateString() )
61+
.Append( '.' );
62+
}
63+
else
64+
{
65+
text.Append( " The API will be deprecated on " )
66+
.Append( when.Date.ToShortDateString() )
67+
.Append( '.' );
68+
}
6069
}
6170

62-
if ( policy.HasLinks )
71+
if ( deprecationPolicy.HasLinks )
6372
{
6473
text.AppendLine();
6574

6675
var rendered = false;
6776

68-
for ( var i = 0; i < policy.Links.Count; i++ )
77+
foreach ( var link in deprecationPolicy.Links )
6978
{
70-
var link = policy.Links[i];
79+
if ( link.Type == "text/html" )
80+
{
81+
if ( !rendered )
82+
{
83+
text.Append( "<h4>Links</h4><ul>" );
84+
rendered = true;
85+
}
86+
87+
text.Append( "<li><a href=\"" );
88+
text.Append( link.LinkTarget.OriginalString );
89+
text.Append( "\">" );
90+
text.Append(
91+
StringSegment.IsNullOrEmpty( link.Title )
92+
? link.LinkTarget.OriginalString
93+
: link.Title.ToString() );
94+
text.Append( "</a></li>" );
95+
}
96+
}
7197

98+
if ( rendered )
99+
{
100+
text.Append( "</ul>" );
101+
}
102+
}
103+
}
104+
105+
if ( description.SunsetPolicy is { } sunsetPolicy )
106+
{
107+
if ( sunsetPolicy.Date is { } when )
108+
{
109+
if ( when < DateTime.Now )
110+
{
111+
text.Append( " The API has been sunset on " )
112+
.Append( when.Date.ToShortDateString() )
113+
.Append( '.' );
114+
}
115+
else
116+
{
117+
text.Append( " The API will be sunset on " )
118+
.Append( when.Date.ToShortDateString() )
119+
.Append( '.' );
120+
}
121+
}
122+
123+
if ( sunsetPolicy.HasLinks )
124+
{
125+
text.AppendLine();
126+
127+
var rendered = false;
128+
129+
foreach ( var link in sunsetPolicy.Links )
130+
{
72131
if ( link.Type == "text/html" )
73132
{
74133
if ( !rendered )

examples/AspNetCore/OData/SomeODataOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,84 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
5050
text.Append( " This API version has been deprecated." );
5151
}
5252

53-
if ( description.SunsetPolicy is { } policy )
53+
if ( description.DeprecationPolicy is { } deprecationPolicy )
5454
{
55-
if ( policy.Date is { } when )
55+
if ( deprecationPolicy.Date is { } when )
5656
{
57-
text.Append( " The API will be sunset on " )
58-
.Append( when.Date.ToShortDateString() )
59-
.Append( '.' );
57+
if ( when < DateTime.Now )
58+
{
59+
text.Append( " The API has been deprecated on " )
60+
.Append( when.Date.ToShortDateString() )
61+
.Append( '.' );
62+
}
63+
else
64+
{
65+
text.Append( " The API will be deprecated on " )
66+
.Append( when.Date.ToShortDateString() )
67+
.Append( '.' );
68+
}
6069
}
6170

62-
if ( policy.HasLinks )
71+
if ( deprecationPolicy.HasLinks )
6372
{
6473
text.AppendLine();
6574

6675
var rendered = false;
6776

68-
for ( var i = 0; i < policy.Links.Count; i++ )
77+
foreach ( var link in deprecationPolicy.Links )
6978
{
70-
var link = policy.Links[i];
79+
if ( link.Type == "text/html" )
80+
{
81+
if ( !rendered )
82+
{
83+
text.Append( "<h4>Links</h4><ul>" );
84+
rendered = true;
85+
}
86+
87+
text.Append( "<li><a href=\"" );
88+
text.Append( link.LinkTarget.OriginalString );
89+
text.Append( "\">" );
90+
text.Append(
91+
StringSegment.IsNullOrEmpty( link.Title )
92+
? link.LinkTarget.OriginalString
93+
: link.Title.ToString() );
94+
text.Append( "</a></li>" );
95+
}
96+
}
7197

98+
if ( rendered )
99+
{
100+
text.Append( "</ul>" );
101+
}
102+
}
103+
}
104+
105+
if ( description.SunsetPolicy is { } sunsetPolicy )
106+
{
107+
if ( sunsetPolicy.Date is { } when )
108+
{
109+
if ( when < DateTime.Now )
110+
{
111+
text.Append( " The API has been sunset on " )
112+
.Append( when.Date.ToShortDateString() )
113+
.Append( '.' );
114+
}
115+
else
116+
{
117+
text.Append( " The API will be sunset on " )
118+
.Append( when.Date.ToShortDateString() )
119+
.Append( '.' );
120+
}
121+
}
122+
123+
if ( sunsetPolicy.HasLinks )
124+
{
125+
text.AppendLine();
126+
127+
var rendered = false;
128+
129+
foreach ( var link in sunsetPolicy.Links )
130+
{
72131
if ( link.Type == "text/html" )
73132
{
74133
if ( !rendered )

examples/AspNetCore/WebApi/MinimalOpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,84 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
5050
text.Append( " This API version has been deprecated." );
5151
}
5252

53-
if ( description.SunsetPolicy is { } policy )
53+
if ( description.DeprecationPolicy is { } deprecationPolicy )
5454
{
55-
if ( policy.Date is { } when )
55+
if ( deprecationPolicy.Date is { } when )
5656
{
57-
text.Append( " The API will be sunset on " )
58-
.Append( when.Date.ToShortDateString() )
59-
.Append( '.' );
57+
if ( when < DateTime.Now )
58+
{
59+
text.Append( " The API has been deprecated on " )
60+
.Append( when.Date.ToShortDateString() )
61+
.Append( '.' );
62+
}
63+
else
64+
{
65+
text.Append( " The API will be deprecated on " )
66+
.Append( when.Date.ToShortDateString() )
67+
.Append( '.' );
68+
}
6069
}
6170

62-
if ( policy.HasLinks )
71+
if ( deprecationPolicy.HasLinks )
6372
{
6473
text.AppendLine();
6574

6675
var rendered = false;
6776

68-
for ( var i = 0; i < policy.Links.Count; i++ )
77+
foreach ( var link in deprecationPolicy.Links )
6978
{
70-
var link = policy.Links[i];
79+
if ( link.Type == "text/html" )
80+
{
81+
if ( !rendered )
82+
{
83+
text.Append( "<h4>Links</h4><ul>" );
84+
rendered = true;
85+
}
86+
87+
text.Append( "<li><a href=\"" );
88+
text.Append( link.LinkTarget.OriginalString );
89+
text.Append( "\">" );
90+
text.Append(
91+
StringSegment.IsNullOrEmpty( link.Title )
92+
? link.LinkTarget.OriginalString
93+
: link.Title.ToString() );
94+
text.Append( "</a></li>" );
95+
}
96+
}
7197

98+
if ( rendered )
99+
{
100+
text.Append( "</ul>" );
101+
}
102+
}
103+
}
104+
105+
if ( description.SunsetPolicy is { } sunsetPolicy )
106+
{
107+
if ( sunsetPolicy.Date is { } when )
108+
{
109+
if ( when < DateTime.Now )
110+
{
111+
text.Append( " The API has been sunset on " )
112+
.Append( when.Date.ToShortDateString() )
113+
.Append( '.' );
114+
}
115+
else
116+
{
117+
text.Append( " The API will be sunset on " )
118+
.Append( when.Date.ToShortDateString() )
119+
.Append( '.' );
120+
}
121+
}
122+
123+
if ( sunsetPolicy.HasLinks )
124+
{
125+
text.AppendLine();
126+
127+
var rendered = false;
128+
129+
foreach ( var link in sunsetPolicy.Links )
130+
{
72131
if ( link.Type == "text/html" )
73132
{
74133
if ( !rendered )

examples/AspNetCore/WebApi/OpenApiExample/ConfigureSwaggerOptions.cs

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,84 @@ private static OpenApiInfo CreateInfoForApiVersion( ApiVersionDescription descri
5050
text.Append( " This API version has been deprecated." );
5151
}
5252

53-
if ( description.SunsetPolicy is { } policy )
53+
if ( description.DeprecationPolicy is { } deprecationPolicy )
5454
{
55-
if ( policy.Date is { } when )
55+
if ( deprecationPolicy.Date is { } when )
5656
{
57-
text.Append( " The API will be sunset on " )
58-
.Append( when.Date.ToShortDateString() )
59-
.Append( '.' );
57+
if ( when < DateTime.Now )
58+
{
59+
text.Append( " The API has been deprecated on " )
60+
.Append( when.Date.ToShortDateString() )
61+
.Append( '.' );
62+
}
63+
else
64+
{
65+
text.Append( " The API will be deprecated on " )
66+
.Append( when.Date.ToShortDateString() )
67+
.Append( '.' );
68+
}
6069
}
6170

62-
if ( policy.HasLinks )
71+
if ( deprecationPolicy.HasLinks )
6372
{
6473
text.AppendLine();
6574

6675
var rendered = false;
6776

68-
for ( var i = 0; i < policy.Links.Count; i++ )
77+
foreach ( var link in deprecationPolicy.Links )
6978
{
70-
var link = policy.Links[i];
79+
if ( link.Type == "text/html" )
80+
{
81+
if ( !rendered )
82+
{
83+
text.Append( "<h4>Links</h4><ul>" );
84+
rendered = true;
85+
}
86+
87+
text.Append( "<li><a href=\"" );
88+
text.Append( link.LinkTarget.OriginalString );
89+
text.Append( "\">" );
90+
text.Append(
91+
StringSegment.IsNullOrEmpty( link.Title )
92+
? link.LinkTarget.OriginalString
93+
: link.Title.ToString() );
94+
text.Append( "</a></li>" );
95+
}
96+
}
7197

98+
if ( rendered )
99+
{
100+
text.Append( "</ul>" );
101+
}
102+
}
103+
}
104+
105+
if ( description.SunsetPolicy is { } sunsetPolicy )
106+
{
107+
if ( sunsetPolicy.Date is { } when )
108+
{
109+
if ( when < DateTime.Now )
110+
{
111+
text.Append( " The API has been sunset on " )
112+
.Append( when.Date.ToShortDateString() )
113+
.Append( '.' );
114+
}
115+
else
116+
{
117+
text.Append( " The API will be sunset on " )
118+
.Append( when.Date.ToShortDateString() )
119+
.Append( '.' );
120+
}
121+
}
122+
123+
if ( sunsetPolicy.HasLinks )
124+
{
125+
text.AppendLine();
126+
127+
var rendered = false;
128+
129+
foreach ( var link in sunsetPolicy.Links )
130+
{
72131
if ( link.Type == "text/html" )
73132
{
74133
if ( !rendered )

0 commit comments

Comments
 (0)