-
Notifications
You must be signed in to change notification settings - Fork 707
/
Program.cs
280 lines (248 loc) · 9.33 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using ApiVersioning.Examples;
using Asp.Versioning;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
using OrderV1 = ApiVersioning.Examples.Models.V1.Order;
using OrderV2 = ApiVersioning.Examples.Models.V2.Order;
using OrderV3 = ApiVersioning.Examples.Models.V3.Order;
using PersonV1 = ApiVersioning.Examples.Models.V1.Person;
using PersonV2 = ApiVersioning.Examples.Models.V2.Person;
using PersonV3 = ApiVersioning.Examples.Models.V3.Person;
var builder = WebApplication.CreateBuilder( args );
var services = builder.Services;
// Add services to the container.
services.AddEndpointsApiExplorer();
services.AddApiVersioning(
options =>
{
// reporting api versions will return the headers
// "api-supported-versions" and "api-deprecated-versions"
options.ReportApiVersions = true;
options.Policies.Sunset( 0.9 )
.Effective( DateTimeOffset.Now.AddDays( 60 ) )
.Link( "policy.html" )
.Title( "Versioning Policy" )
.Type( "text/html" );
} )
.AddApiExplorer(
options =>
{
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
// note: the specified format code will format the version as "'v'major[.minor][-status]"
options.GroupNameFormat = "'v'VVV";
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
// can also be used to control the format of the API version in route templates
options.SubstituteApiVersionInUrl = true;
} )
// this enables binding ApiVersion as a endpoint callback parameter. if you don't use it, then
// you should remove this configuration.
.EnableApiVersionBinding();
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
services.AddSwaggerGen( options => options.OperationFilter<SwaggerDefaultValues>() );
// Configure the HTTP request pipeline.
var app = builder.Build();
var orders = app.MapApiGroup( "Orders" );
var people = app.MapApiGroup( "People" );
// 1.0
var ordersV1 = orders.MapGroup( "/api/orders" )
.HasDeprecatedApiVersion( 0.9 )
.HasApiVersion( 1.0 );
ordersV1.MapGet( "/{id:int}", ( int id ) => new OrderV1() { Id = id, Customer = "John Doe" } )
.Produces<OrderV1>()
.Produces( 404 );
ordersV1.MapPost( "/", ( HttpRequest request, OrderV1 order ) =>
{
order.Id = 42;
var scheme = request.Scheme;
var host = request.Host;
var location = new Uri( $"{scheme}{Uri.SchemeDelimiter}{host}/api/orders/{order.Id}" );
return Results.Created( location, order );
} )
.Accepts<OrderV1>( "application/json" )
.Produces<OrderV1>( 201 )
.Produces( 400 )
.MapToApiVersion( 1.0 );
ordersV1.MapPatch( "/{id:int}", ( int id, OrderV1 order ) => Results.NoContent() )
.Accepts<OrderV1>( "application/json" )
.Produces( 204 )
.Produces( 400 )
.Produces( 404 )
.MapToApiVersion( 1.0 );
// 2.0
var ordersV2 = orders.MapGroup( "/api/orders" )
.HasApiVersion( 2.0 );
ordersV2.MapGet( "/", () =>
new OrderV2[]
{
new(){ Id = 1, Customer = "John Doe" },
new(){ Id = 2, Customer = "Bob Smith" },
new(){ Id = 3, Customer = "Jane Doe", EffectiveDate = DateTimeOffset.UtcNow.AddDays( 7d ) },
} )
.Produces<IEnumerable<OrderV2>>()
.Produces( 404 );
ordersV2.MapGet( "/{id:int}", ( int id ) => new OrderV2() { Id = id, Customer = "John Doe" } )
.Produces<OrderV2>()
.Produces( 404 );
ordersV2.MapPost( "/", ( HttpRequest request, OrderV2 order ) =>
{
order.Id = 42;
var scheme = request.Scheme;
var host = request.Host;
var location = new Uri( $"{scheme}{Uri.SchemeDelimiter}{host}/api/orders/{order.Id}" );
return Results.Created( location, order );
} )
.Accepts<OrderV2>( "application/json" )
.Produces<OrderV2>( 201 )
.Produces( 400 );
ordersV2.MapPatch( "/{id:int}", ( int id, OrderV2 order ) => Results.NoContent() )
.Accepts<OrderV2>( "application/json" )
.Produces( 204 )
.Produces( 400 )
.Produces( 404 );
// 3.0
var ordersV3 = orders.MapGroup( "/api/orders" )
.HasApiVersion( 3.0 );
ordersV3.MapGet( "/", () =>
new OrderV3[]
{
new(){ Id = 1, Customer = "John Doe" },
new(){ Id = 2, Customer = "Bob Smith" },
new(){ Id = 3, Customer = "Jane Doe", EffectiveDate = DateTimeOffset.UtcNow.AddDays( 7d ) },
} )
.Produces<IEnumerable<OrderV3>>();
ordersV3.MapGet( "/{id:int}", ( int id ) => new OrderV3() { Id = id, Customer = "John Doe" } )
.Produces<OrderV3>()
.Produces( 404 );
ordersV3.MapPost( "/", ( HttpRequest request, OrderV3 order ) =>
{
order.Id = 42;
var scheme = request.Scheme;
var host = request.Host;
var location = new Uri( $"{scheme}{Uri.SchemeDelimiter}{host}/api/orders/{order.Id}" );
return Results.Created( location, order );
} )
.Accepts<OrderV3>( "application/json" )
.Produces<OrderV3>( 201 )
.Produces( 400 );
ordersV3.MapDelete( "/{id:int}", ( int id ) => Results.NoContent() )
.Produces( 204 );
// 1.0
var peopleV1 = people.MapGroup( "/api/v{version:apiVersion}/people" )
.HasDeprecatedApiVersion( 0.9 )
.HasApiVersion( 1.0 );
peopleV1.MapGet( "/{id:int}", ( int id ) =>
new PersonV1()
{
Id = id,
FirstName = "John",
LastName = "Doe",
} )
.Produces<PersonV1>()
.Produces( 404 );
// 2.0
var peopleV2 = people.MapGroup( "/api/v{version:apiVersion}/people" )
.HasApiVersion( 2.0 );
peopleV2.MapGet( "/", () =>
new PersonV2[]
{
new()
{
Id = 1,
FirstName = "John",
LastName = "Doe",
Email = "john.doe@somewhere.com",
},
new()
{
Id = 2,
FirstName = "Bob",
LastName = "Smith",
Email = "bob.smith@somewhere.com",
},
new()
{
Id = 3,
FirstName = "Jane",
LastName = "Doe",
Email = "jane.doe@somewhere.com",
},
} )
.Produces<IEnumerable<PersonV2>>();
peopleV2.MapGet( "/{id:int}", ( int id ) =>
new PersonV2()
{
Id = id,
FirstName = "John",
LastName = "Doe",
Email = "john.doe@somewhere.com",
} )
.Produces<PersonV2>()
.Produces( 404 );
// 3.0
var peopleV3 = people.MapGroup( "/api/v{version:apiVersion}/people" )
.HasApiVersion( 3.0 );
peopleV3.MapGet( "/", () =>
new PersonV3[]
{
new()
{
Id = 1,
FirstName = "John",
LastName = "Doe",
Email = "john.doe@somewhere.com",
Phone = "555-987-1234",
},
new()
{
Id = 2,
FirstName = "Bob",
LastName = "Smith",
Email = "bob.smith@somewhere.com",
Phone = "555-654-4321",
},
new()
{
Id = 3,
FirstName = "Jane",
LastName = "Doe",
Email = "jane.doe@somewhere.com",
Phone = "555-789-3456",
},
} )
.Produces<IEnumerable<PersonV3>>();
peopleV3.MapGet( "/{id:int}", ( int id ) =>
new PersonV3()
{
Id = id,
FirstName = "John",
LastName = "Doe",
Email = "john.doe@somewhere.com",
Phone = "555-987-1234",
} )
.Produces<PersonV3>()
.Produces( 404 );
peopleV3.MapPost( "/", ( HttpRequest request, ApiVersion version, PersonV3 person ) =>
{
person.Id = 42;
var scheme = request.Scheme;
var host = request.Host;
var location = new Uri( $"{scheme}{Uri.SchemeDelimiter}{host}/v{version}/api/people/{person.Id}" );
return Results.Created( location, person );
} )
.Accepts<PersonV3>( "application/json" )
.Produces<PersonV3>( 201 )
.Produces( 400 );
app.UseSwagger();
app.UseSwaggerUI(
options =>
{
var descriptions = app.DescribeApiVersions();
// build a swagger endpoint for each discovered API version
foreach ( var description in descriptions )
{
var url = $"/swagger/{description.GroupName}/swagger.json";
var name = description.GroupName.ToUpperInvariant();
options.SwaggerEndpoint( url, name );
}
} );
app.Run();