Skip to content

Commit 274d97c

Browse files
committed
Parameter examples with APIWeaver
1 parent a41eaa6 commit 274d97c

File tree

5 files changed

+27
-9
lines changed

5 files changed

+27
-9
lines changed

App/Api/Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<ItemGroup>
3+
<PackageReference Include="APIWeaver.OpenApi" />
34
<ProjectReference Include="..\Services\Services.csproj" />
45
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" />
56
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />

App/Api/Setup/ApiEndpoints.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ namespace Api.Setup;
88

99
internal static class ApiEndpoints
1010
{
11-
private static readonly OpenApiString s_exampleFromDate = new(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
12-
private static readonly OpenApiString s_exampleToDate = new(DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
13-
1411
public static void ConfigureEndpoints(this WebApplication app)
1512
{
1613
var apiVersionSet = app.NewApiVersionSet()
@@ -42,8 +39,8 @@ public static void ConfigureEndpoints(this WebApplication app)
4239
.WithDescription("Get longest downward trend in days between given dates")
4340
.WithOpenApi(operation =>
4441
{
45-
operation.Parameters[0].Example = s_exampleFromDate;
46-
operation.Parameters[^1].Example = s_exampleToDate;
42+
operation.Parameters[0].Example = GetExampleString(-1);
43+
operation.Parameters[^1].Example = GetExampleString();
4744
return operation;
4845
})
4946
.Produces<HighestTradingVolumeResponse>((int)HttpStatusCode.OK)
@@ -76,8 +73,8 @@ public static void ConfigureEndpoints(this WebApplication app)
7673
.WithDescription("Get the date with the highest trading volume between given dates")
7774
.WithOpenApi(operation =>
7875
{
79-
operation.Parameters[0].Example = s_exampleFromDate;
80-
operation.Parameters[^1].Example = s_exampleToDate;
76+
operation.Parameters[0].Example = GetExampleString(-1);
77+
operation.Parameters[^1].Example = GetExampleString();
8178
return operation;
8279
})
8380
.Produces<HighestTradingVolumeResponse>((int)HttpStatusCode.OK)
@@ -110,8 +107,8 @@ public static void ConfigureEndpoints(this WebApplication app)
110107
.WithDescription("Get pair of dates when it is best to buy and sell between given dates")
111108
.WithOpenApi(operation =>
112109
{
113-
operation.Parameters[0].Example = s_exampleFromDate;
114-
operation.Parameters[^1].Example = s_exampleToDate;
110+
operation.Parameters[0].Example = GetExampleString(-1);
111+
operation.Parameters[^1].Example = GetExampleString();
115112
return operation;
116113
})
117114
.Produces<BuyAndSellResponse>((int)HttpStatusCode.OK)
@@ -120,6 +117,11 @@ public static void ConfigureEndpoints(this WebApplication app)
120117
.ProducesProblem((int)HttpStatusCode.TooManyRequests)
121118
.ProducesProblem((int)HttpStatusCode.InternalServerError);
122119
}
120+
121+
private static OpenApiString GetExampleString(int addMonths = 0)
122+
{
123+
return new(DateOnlyExampleProvider.GetExample().AddMonths(addMonths).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
124+
}
123125
}
124126

125127
public record LongestDownwardTrendResponse(int Days);

App/Api/Setup/ApiServices.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22
using System.Text.Json.Serialization;
33
using System.Threading.RateLimiting;
4+
using APIWeaver;
45
using Asp.Versioning;
56
using Microsoft.AspNetCore.Http.Json;
67
using Microsoft.AspNetCore.Server.Kestrel.Core;
@@ -30,6 +31,11 @@ public static void ConfigureServices(this IServiceCollection services)
3031

3132
services.AddOpenApi();
3233

34+
services.AddApiWeaver(opt =>
35+
{
36+
opt.AddExample<DateOnly, DateOnlyExampleProvider>();
37+
});
38+
3339
services.AddSwaggerGen();
3440

3541
services.Configure<JsonOptions>(opt =>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using APIWeaver;
2+
3+
namespace Api.Setup;
4+
5+
public sealed class DateOnlyExampleProvider : IExampleProvider<DateOnly>
6+
{
7+
public static DateOnly GetExample() => DateOnly.FromDateTime(DateTime.UtcNow);
8+
}

App/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
55
</PropertyGroup>
66
<ItemGroup>
7+
<PackageVersion Include="APIWeaver.OpenApi" Version="2.7.0" />
78
<PackageVersion Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
89
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="9.0.4" />
910
<PackageVersion Include="Microsoft.AspNetCore.WebUtilities" Version="9.0.4" />

0 commit comments

Comments
 (0)