Skip to content

Commit 7ee1774

Browse files
committed
Produce OAS file at build time in example, referenced by client on disk (makes it easier to keep in sync during development)
1 parent 022285d commit 7ee1774

File tree

9 files changed

+55
-8
lines changed

9 files changed

+55
-8
lines changed

.gitattributes

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# When running OpenAPI tests, these committed files are downloaded and written to disk (so we'll know when something changes).
2-
# On Windows, these text files are auto-converted to crlf on fetch, while the written downloaded files use lf line endings.
2+
# On Windows, these text files are auto-converted to crlf on git fetch, while the written downloaded files use lf line endings.
33
# Therefore, running the tests on Windows creates local changes. Staging them auto-converts back to crlf, which undoes the changes.
4-
# To avoid this annoyance, the next line opts out of the auto-conversion and forces to lf.
4+
# To avoid this annoyance, the next lines opt out of the auto-conversion and force to lf.
55
swagger.g.json text eol=lf
6+
**/GeneratedSwagger/*.json text eol=lf

JsonApiDotNetCore.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ EndProject
6969
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCore.OpenApi.Client", "src\JsonApiDotNetCore.OpenApi.Client\JsonApiDotNetCore.OpenApi.Client.csproj", "{5ADAA902-5A75-4ECB-B4B4-03291D63CE9C}"
7070
EndProject
7171
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JsonApiDotNetCoreExampleClient", "src\Examples\JsonApiDotNetCoreExampleClient\JsonApiDotNetCoreExampleClient.csproj", "{7FC5DFA3-6F66-4FD8-820D-81E93856F252}"
72+
ProjectSection(ProjectDependencies) = postProject
73+
{C916EBDA-3429-4FEA-AFB3-DF7CA32A8C6A} = {C916EBDA-3429-4FEA-AFB3-DF7CA32A8C6A}
74+
EndProjectSection
7275
EndProject
7376
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenApiClientTests", "test\OpenApiClientTests\OpenApiClientTests.csproj", "{77F98215-3085-422E-B99D-4C404C2114CF}"
7477
EndProject

package-versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<SwashbuckleFrozenVersion>6.5.0</SwashbuckleFrozenVersion>
88

99
<!-- Non-published dependencies (these are safe to update, won't cause a breaking change) -->
10+
<ApiDescriptionServerVersion>8.0.*</ApiDescriptionServerVersion>
1011
<BenchmarkDotNetVersion>0.13.*</BenchmarkDotNetVersion>
1112
<BlushingPenguinVersion>1.0.*</BlushingPenguinVersion>
1213
<BogusVersion>35.2.*</BogusVersion>

src/Examples/JsonApiDotNetCoreExampleClient/OpenAPIs/swagger.json renamed to src/Examples/JsonApiDotNetCoreExample/GeneratedSwagger/JsonApiDotNetCoreExample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"servers": [
88
{
9-
"url": "http://localhost:14140"
9+
"url": "https://localhost:44340"
1010
}
1111
],
1212
"paths": {

src/Examples/JsonApiDotNetCoreExample/JsonApiDotNetCoreExample.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
33
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
4+
<OpenApiGenerateDocumentsOnBuild>true</OpenApiGenerateDocumentsOnBuild>
5+
<OpenApiDocumentsDirectory>GeneratedSwagger</OpenApiDocumentsDirectory>
46
</PropertyGroup>
57

68
<Import Project="..\..\..\package-versions.props" />
@@ -16,5 +18,6 @@
1618
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EntityFrameworkCoreVersion)" />
1719
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(EntityFrameworkCoreVersion)" />
1820
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="$(SwashbuckleVersion)" />
21+
<PackageReference Include="Microsoft.Extensions.ApiDescription.Server" Version="$(ApiDescriptionServerVersion)" PrivateAssets="all" />
1922
</ItemGroup>
2023
</Project>

src/Examples/JsonApiDotNetCoreExample/Program.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using JsonApiDotNetCore.Configuration;
55
using JsonApiDotNetCore.Diagnostics;
66
using JsonApiDotNetCore.OpenApi;
7+
using JsonApiDotNetCoreExample;
78
using JsonApiDotNetCoreExample.Data;
89
using Microsoft.EntityFrameworkCore;
910
using Microsoft.EntityFrameworkCore.Diagnostics;
@@ -16,7 +17,10 @@
1617

1718
WebApplication app = CreateWebApplication(args);
1819

19-
await CreateDatabaseAsync(app.Services);
20+
if (!IsGeneratingOpenApiDocumentAtBuildTime())
21+
{
22+
await CreateDatabaseAsync(app.Services);
23+
}
2024

2125
app.Run();
2226

@@ -83,7 +87,7 @@ static void ConfigureServices(WebApplicationBuilder builder)
8387

8488
using (CodeTimingSessionManager.Current.Measure("AddOpenApi()"))
8589
{
86-
builder.Services.AddOpenApi(mvcCoreBuilder);
90+
builder.Services.AddOpenApi(mvcCoreBuilder, options => options.DocumentFilter<SetOpenApiServerAtBuildTimeFilter>());
8791
}
8892
}
8993

@@ -112,6 +116,11 @@ static void ConfigurePipeline(WebApplication app)
112116
app.MapControllers();
113117
}
114118

119+
static bool IsGeneratingOpenApiDocumentAtBuildTime()
120+
{
121+
return Environment.GetCommandLineArgs().Any(argument => argument.Contains("GetDocument.Insider"));
122+
}
123+
115124
static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
116125
{
117126
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using JetBrains.Annotations;
2+
using Microsoft.OpenApi.Models;
3+
using Swashbuckle.AspNetCore.SwaggerGen;
4+
5+
namespace JsonApiDotNetCoreExample;
6+
7+
/// <summary>
8+
/// This is normally not needed. It ensures the server URL is added to the OpenAPI file during build.
9+
/// </summary>
10+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
11+
internal sealed class SetOpenApiServerAtBuildTimeFilter(IHttpContextAccessor httpContextAccessor) : IDocumentFilter
12+
{
13+
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
14+
15+
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
16+
{
17+
if (_httpContextAccessor.HttpContext == null)
18+
{
19+
swaggerDoc.Servers.Add(new OpenApiServer
20+
{
21+
Url = "https://localhost:44340"
22+
});
23+
}
24+
}
25+
}

src/Examples/JsonApiDotNetCoreExampleClient/JsonApiDotNetCoreExampleClient.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27-
<OpenApiReference Include="OpenAPIs\swagger.json" CodeGenerator="NSwagCSharp" ClassName="ExampleApiClient">
28-
<SourceUri>http://localhost:14140/swagger/v1/swagger.json</SourceUri>
27+
<OpenApiReference Include="..\JsonApiDotNetCoreExample\GeneratedSwagger\JsonApiDotNetCoreExample.json" CodeGenerator="NSwagCSharp" ClassName="ExampleApiClient">
2928
</OpenApiReference>
3029
</ItemGroup>
3130
</Project>

src/JsonApiDotNetCore/Diagnostics/CodeTimingSessionManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static ICodeTimer Current
3131
static CodeTimingSessionManager()
3232
{
3333
#if DEBUG
34-
IsEnabled = !IsRunningInTest() && !IsRunningInBenchmark();
34+
IsEnabled = !IsRunningInTest() && !IsRunningInBenchmark() && !IsGeneratingOpenApiDocumentAtBuildTime();
3535
#else
3636
IsEnabled = false;
3737
#endif
@@ -52,6 +52,12 @@ private static bool IsRunningInBenchmark()
5252
return Assembly.GetEntryAssembly()?.GetName().Name == "Benchmarks";
5353
}
5454

55+
// ReSharper disable once UnusedMember.Local
56+
private static bool IsGeneratingOpenApiDocumentAtBuildTime()
57+
{
58+
return Environment.GetCommandLineArgs().Any(argument => argument.Contains("GetDocument.Insider"));
59+
}
60+
5561
private static void AssertHasActiveSession()
5662
{
5763
if (_session == null)

0 commit comments

Comments
 (0)