Skip to content

Commit 7749b94

Browse files
authored
Merge pull request #1 from y-code/append-version-to-type-name
have version in type names
2 parents 876f6fe + 5a522ed commit 7749b94

File tree

8 files changed

+64
-56
lines changed

8 files changed

+64
-56
lines changed

RestApi.Test/V1/ExampleTest.cs renamed to RestApi.Test/ApiTests/ExampleV1Test.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Net;
53
using System.Text.Json;
64
using System.Threading.Tasks;
75
using NUnit.Framework;
8-
using RestApi.Models.V1;
6+
using RestApi.Models;
97
using RestApi.Test.Models;
108

11-
namespace RestApi.Test.V1
9+
namespace RestApi.Test.ApiTests
1210
{
1311
[TestFixture]
14-
public class ExampleTest : TestBase
12+
public class ExampleV1Test : TestBase
1513
{
1614
[TestCase("")]
1715
[TestCase("/")]
@@ -23,7 +21,7 @@ await CatchWebException(async () =>
2321
using (var stream = await client.OpenReadTaskAsync(
2422
new Uri(SetUp.UrlToV1Example + route)))
2523
{
26-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
24+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
2725

2826
Assert.That(data.Length, Is.EqualTo(5));
2927
}
@@ -35,12 +33,12 @@ public async Task TestExampleWithDate(string date, string[] dates)
3533
{
3634
await CatchWebException(async () =>
3735
{
38-
WeatherForecast[] data;
36+
WeatherForecastV1[] data;
3937
using (var client = new WebClient())
4038
using (var stream = await client.OpenReadTaskAsync(
4139
new Uri($"{SetUp.UrlToV1Example}/{date}")))
4240
{
43-
data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
41+
data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
4442

4543
Assert.That(data.Length, Is.EqualTo(5));
4644
Assert.That(data[0].Date, Is.EqualTo(dates[0]));
@@ -63,7 +61,7 @@ public async Task TestExampleWithInvalidDate(string date)
6361
using (var stream = await client.OpenReadTaskAsync(
6462
new Uri($"{SetUp.UrlToV1Example}/{date}")))
6563
{
66-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
64+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV1[]>(stream);
6765
}
6866

6967
Assert.Fail("This test should have ended up with an error response.");

RestApi.Test/V2/ExampleTest.cs renamed to RestApi.Test/ApiTests/ExampleV2Test.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
using System.Text.Json;
55
using System.Threading.Tasks;
66
using NUnit.Framework;
7-
using RestApi.Models.V2;
7+
using RestApi.Models;
88
using RestApi.Test.Models;
99

10-
namespace RestApi.Test.V2
10+
namespace RestApi.Test.ApiTests
1111
{
1212
[TestFixture]
13-
public class ExampleTest : TestBase
13+
public class ExampleV2Test : TestBase
1414
{
1515
[TestCase("")]
1616
[TestCase("/20200303")]
@@ -22,7 +22,7 @@ public async Task TestExample(string route)
2222
using (var stream = await client.OpenReadTaskAsync(
2323
new Uri(SetUp.UrlToV2Example + route)))
2424
{
25-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
25+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
2626
}
2727

2828
Assert.Fail("This test should have ended up with an error response.");
@@ -48,7 +48,7 @@ await CatchWebException(async () =>
4848
using (var stream = await client.OpenReadTaskAsync(
4949
new Uri(SetUp.UrlToV2ExampleWF + route)))
5050
{
51-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
51+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
5252

5353
Assert.That(data.Length, Is.EqualTo(5 * 3));
5454
}
@@ -66,7 +66,7 @@ await CatchWebException(async () =>
6666
using (var stream = await client.OpenReadTaskAsync(
6767
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
6868
{
69-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
69+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
7070

7171
Assert.That(data.Length, Is.EqualTo(5 * 3));
7272
Assert.That(data.Min(d => d.Date),
@@ -86,7 +86,7 @@ public async Task TestExampleWFWithInvalidDate(string date)
8686
using (var stream = await client.OpenReadTaskAsync(
8787
new Uri($"{SetUp.UrlToV2ExampleWF}?from={date}")))
8888
{
89-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
89+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
9090
}
9191

9292
Assert.Fail("This test should have ended up with an error response.");
@@ -115,7 +115,7 @@ await CatchWebException(async () =>
115115
using (var stream = await client.OpenReadTaskAsync(
116116
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}{(days == null ? "" : $"&days={days}")}")))
117117
{
118-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
118+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
119119

120120
Assert.That(data.Length, Is.EqualTo(days ?? 5));
121121
Assert.That(data.Min(d => d.Date),
@@ -133,7 +133,7 @@ public async Task TestExampleWFWithInvalidAreaAndValidDate(string area, string d
133133
using (var stream = await client.OpenReadTaskAsync(
134134
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
135135
{
136-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
136+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
137137
}
138138
Assert.Fail("This test should have ended up with an error response.");
139139
}
@@ -159,7 +159,7 @@ public async Task TestExampleWFWithValidAreaAndInvalidDate(string area, string d
159159
using (var stream = await client.OpenReadTaskAsync(
160160
new Uri($"{SetUp.UrlToV2ExampleWF}/{area}?from={date}")))
161161
{
162-
var data = await JsonSerializer.DeserializeAsync<WeatherForecast[]>(stream);
162+
var data = await JsonSerializer.DeserializeAsync<WeatherForecastV2[]>(stream);
163163
}
164164

165165
Assert.Fail("This test should have ended up with an error response.");

RestApi.Test/SetUp.cs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Net;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.Hosting;
56
using Microsoft.Extensions.Logging;
@@ -59,29 +60,33 @@ public async Task SetUpBeforeAll()
5960
DateTime start = DateTime.UtcNow;
6061
DateTime timeout = DateTime.UtcNow + TimeSpan.FromSeconds(ApiServiceStartUpTimeoutSeconds);
6162
Exception exception = null;
62-
while (DateTime.UtcNow < timeout)
63+
while (true)
6364
{
6465
try
6566
{
6667
await Task.Delay(500);
6768
_logger.LogDebug("Checking service startup...");
68-
var client = new System.Net.WebClient();
69-
using (stream = await client.OpenReadTaskAsync(new Uri(UrlToSwagger))) { }
69+
var client = new WebClient();
70+
using (stream = await client.OpenReadTaskAsync(new Uri(ApiServiceBaseUrl))) { }
7071
var wakeup = DateTime.UtcNow - start;
7172
_logger.LogInformation("API service started successfully.");
7273
break;
7374
}
7475
catch (Exception e)
7576
{
7677
exception = e;
77-
}
78-
}
78+
if (e is WebException && ((WebException)e).Status != WebExceptionStatus.UnknownError)
79+
{
80+
break;
81+
}
7982

80-
if (stream == null)
81-
{
82-
var error = $"Test target API service did not start up within {ApiServiceStartUpTimeoutSeconds} seconds";
83-
_logger.LogError(error);
84-
throw new Exception(error, exception);
83+
if (DateTime.UtcNow > timeout)
84+
{
85+
var error = $"Test target API service did not start up within {ApiServiceStartUpTimeoutSeconds} seconds";
86+
_logger.LogError(error);
87+
throw new Exception(error, exception);
88+
}
89+
}
8590
}
8691
}
8792

RestApi/Controllers/ErrorController.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
namespace RestApi.Controllers
88
{
99
[ApiController]
10-
[ApiVersion("1")]
11-
[ApiVersion("2")]
10+
[ApiVersionNeutral]
1211
[Route("api/[controller]")]
1312
[OpenApiIgnore]
1413
public class ErrorController : ControllerBase

RestApi/Controllers/V1/ExampleController.cs renamed to RestApi/Controllers/ExampleV1Controller.cs

+12-11
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
using System.Linq;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.Extensions.Logging;
7-
using RestApi.Models.V1;
7+
using NSwag.Annotations;
8+
using RestApi.Models;
89

9-
namespace RestApi.Controllers.V1
10+
namespace RestApi.Controllers
1011
{
1112
[ApiController]
1213
[ApiVersion("1")]
13-
[Route("api/v{version:apiVersion}/[controller]")]
14-
public partial class ExampleController : ControllerBase
14+
[Route("api/v{version:apiVersion}/Example")]
15+
[OpenApiTag("API Example: Weather Forecast")]
16+
public partial class ExampleV1Controller : ControllerBase
1517
{
1618
private static readonly string[] Summaries = new[]
1719
{
@@ -20,13 +22,13 @@ public partial class ExampleController : ControllerBase
2022

2123
private readonly ILogger _logger;
2224

23-
public ExampleController(ILogger<ExampleController> logger)
25+
public ExampleV1Controller(ILogger<ExampleV1Controller> logger)
2426
{
2527
_logger = logger;
2628
}
2729

2830
[HttpGet("{date}")]
29-
public virtual IEnumerable<WeatherForecast> Get([FromRoute] string date)
31+
public IEnumerable<WeatherForecastV1> Get([FromRoute] string date)
3032
{
3133
DateTime baseDate = DateTime.UtcNow;
3234
if (date != null
@@ -42,17 +44,16 @@ public virtual IEnumerable<WeatherForecast> Get([FromRoute] string date)
4244

4345
var random = new Random();
4446
return Enumerable.Range(0, 5)
45-
.Select(index => new WeatherForecast
47+
.Select(index => new WeatherForecastV1
4648
{
4749
Date = baseDate.AddDays(index).ToString("d MMM, yyyy"),
4850
TemperatureC = random.Next(-20, 55),
4951
Summary = Summaries[random.Next(Summaries.Length)],
50-
})
51-
.ToArray();
52+
});
5253
}
5354

54-
[HttpGet()]
55-
public virtual IEnumerable<WeatherForecast> Get()
55+
[HttpGet]
56+
public IEnumerable<WeatherForecastV1> Get()
5657
=> Get(null);
5758
}
5859
}

RestApi/Controllers/V2/ExampleController.cs renamed to RestApi/Controllers/ExampleV2Controller.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
using System.Linq;
55
using Microsoft.AspNetCore.Mvc;
66
using Microsoft.Extensions.Logging;
7-
using RestApi.Models.V2;
7+
using NSwag.Annotations;
8+
using RestApi.Models;
89

9-
namespace RestApi.Controllers.V2
10+
namespace RestApi.Controllers
1011
{
1112
[ApiController]
1213
[ApiVersion("2")]
13-
[Route("api/v{version:apiVersion}/[controller]")]
14-
public partial class ExampleController : ControllerBase
14+
[Route("api/v{version:apiVersion}/Example")]
15+
[OpenApiTag("API Example: Weather Forecast")]
16+
public partial class ExampleV2Controller : ControllerBase
1517
{
1618
private static readonly string[] Areas = new[]
1719
{
@@ -25,13 +27,13 @@ public partial class ExampleController : ControllerBase
2527

2628
private readonly ILogger _logger;
2729

28-
public ExampleController(ILogger<ExampleController> logger)
30+
public ExampleV2Controller(ILogger<ExampleV2Controller> logger)
2931
{
3032
_logger = logger;
3133
}
3234

3335
[HttpGet("WeatherForecast/{area}")]
34-
public virtual IEnumerable<WeatherForecast> GetWeatherForecast(
36+
public IEnumerable<WeatherForecastV2> GetWeatherForecast(
3537
[FromRoute] string area,
3638
[FromQuery] string from,
3739
[FromQuery] int? days = null)
@@ -62,19 +64,18 @@ public virtual IEnumerable<WeatherForecast> GetWeatherForecast(
6264

6365
var random = new Random();
6466
return Enumerable.Range(0, days.Value)
65-
.Select(index => areas.Select(a => new WeatherForecast
67+
.Select(index => areas.Select(a => new WeatherForecastV2
6668
{
6769
Area = a,
6870
Date = baseDate.AddDays(index),
6971
TemperatureC = random.Next(-20, 55),
7072
Summary = Summaries[random.Next(Summaries.Length)],
7173
}))
72-
.SelectMany(w => w)
73-
.ToArray();
74+
.SelectMany(w => w);
7475
}
7576

7677
[HttpGet("WeatherForecast")]
77-
public virtual IEnumerable<WeatherForecast> GetWeatherForecast(
78+
public IEnumerable<WeatherForecastV2> GetWeatherForecast(
7879
[FromQuery] string from,
7980
[FromQuery] int? days)
8081
=> GetWeatherForecast(null, from, days);

RestApi/Models/V1/WeatherForecast.cs renamed to RestApi/Models/WeatherForecastV1.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Text.Json.Serialization;
3+
using NJsonSchema.Annotations;
34

4-
namespace RestApi.Models.V1
5+
namespace RestApi.Models
56
{
6-
public class WeatherForecast
7+
[JsonSchema(name: "WeatherForecast")]
8+
public class WeatherForecastV1
79
{
810
[JsonPropertyName("date")]
911
public string Date { get; set; }

RestApi/Models/V2/WeatherForecast.cs renamed to RestApi/Models/WeatherForecastV2.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using System.Text.Json.Serialization;
3+
using NJsonSchema.Annotations;
34

4-
namespace RestApi.Models.V2
5+
namespace RestApi.Models
56
{
6-
public class WeatherForecast
7+
[JsonSchema(name: "WeatherForecast")]
8+
public class WeatherForecastV2
79
{
810
[JsonPropertyName("date")]
911
public DateTime Date { get; set; }

0 commit comments

Comments
 (0)