Skip to content

Commit 5c2e5c3

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 6d58b0a commit 5c2e5c3

File tree

7 files changed

+25
-9
lines changed

7 files changed

+25
-9
lines changed

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: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
"title": "JsonApiDotNetCoreExample",
55
"version": "1.0"
66
},
7-
"servers": [
8-
{
9-
"url": "http://localhost:14140"
10-
}
11-
],
127
"paths": {
138
"/api/people": {
149
"get": {

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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
WebApplication app = CreateWebApplication(args);
1818

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

2124
app.Run();
2225

@@ -112,6 +115,11 @@ static void ConfigurePipeline(WebApplication app)
112115
app.MapControllers();
113116
}
114117

118+
static bool IsGeneratingOpenApiDocumentAtBuildTime()
119+
{
120+
return Environment.GetCommandLineArgs().Any(argument => argument.Contains("GetDocument.Insider"));
121+
}
122+
115123
static async Task CreateDatabaseAsync(IServiceProvider serviceProvider)
116124
{
117125
await using AsyncServiceScope scope = serviceProvider.CreateAsyncScope();

src/Examples/JsonApiDotNetCoreExampleClient/JsonApiDotNetCoreExampleClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
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">
28+
<Options>/UseBaseUrl:false</Options>
2929
</OpenApiReference>
3030
</ItemGroup>
3131
</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)