Skip to content

Commit

Permalink
Avoid competing swagger generation during test build (#2897)
Browse files Browse the repository at this point in the history
The previous implementation caused the concurrent builds for various frameworks to compete in writing to the output `swagger.json` file, causing exceptions during builds (since it happened in csproj).

In my local development I often failed to rebuild the full solution because some projects failed to build for this reason. It has also happened in CI (#2886).

Note that the NSwag source code generator in `NswagClientExample` still outputs everything in one spot for all frameworks, but I haven't noticed failures from that. Maybe it's just less likely to write concurrently. I left it as is.

Un-skip the `Overwrites_Existing_File` test.
  • Loading branch information
patrikwlund committed May 16, 2024
1 parent e1b56e1 commit 77cde47
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/Swashbuckle.AspNetCore.Cli.Test/ToolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Can_Generate_Swagger_Json()
Assert.True(productsPath.TryGetProperty("post", out _));
}

[Fact(Skip = "Disabled because it makes CI unstable")]
[Fact]
public void Overwrites_Existing_File()
{
using var temporaryDirectory = new TemporaryDirectory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SwaggerIntegrationTests
{
[Theory]
[InlineData(typeof(Basic.Startup), "/swagger/v1/swagger.json")]
[InlineData(typeof(CliExample.Startup), "/swagger/v1/swagger.json")]
[InlineData(typeof(CliExample.Startup), "/swagger/v1/swagger_net8.0.json")]
[InlineData(typeof(ConfigFromFile.Startup), "/swagger/v1/swagger.json")]
[InlineData(typeof(CustomUIConfig.Startup), "/swagger/v1/swagger.json")]
[InlineData(typeof(CustomUIIndex.Startup), "/swagger/v1/swagger.json")]
Expand Down
2 changes: 1 addition & 1 deletion test/WebSites/CliExample/CliExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
-->

<Target Name="SwaggerToFile" AfterTargets="AfterBuild">
<Exec Command="dotnet dotnet-swagger.dll tofile --host http://localhost:51071 --output %22$(MSBuildThisFileDirectory)wwwroot/swagger/v1/swagger.json%22 %22$(OutputPath)$(AssemblyName).dll%22 v1"
<Exec Command="dotnet dotnet-swagger.dll tofile --host http://localhost:51071 --output %22$(MSBuildThisFileDirectory)wwwroot/swagger/v1/swagger_$(TargetFramework).json%22 %22$(OutputPath)$(AssemblyName).dll%22 v1"
WorkingDirectory="$(DotNetSwaggerPath)" />
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
}
}
}
}
}
54 changes: 54 additions & 0 deletions test/WebSites/CliExample/wwwroot/swagger/v1/swagger_net7.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi": "3.0.1",
"info": {
"title": "CliExample",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:51071"
}
],
"paths": {
"/products": {
"get": {
"tags": [
"Products"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Product": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"description": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}
54 changes: 54 additions & 0 deletions test/WebSites/CliExample/wwwroot/swagger/v1/swagger_net8.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi": "3.0.1",
"info": {
"title": "CliExample",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:51071"
}
],
"paths": {
"/products": {
"get": {
"tags": [
"Products"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Product": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"description": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
-->

<Target Name="SwaggerToFile" AfterTargets="AfterBuild">
<Exec Command="dotnet dotnet-swagger.dll tofile --host http://localhost:57556/ --output %22$(MSBuildThisFileDirectory)wwwroot/swagger/v1/swagger.json%22 %22$(OutputPath)$(AssemblyName).dll%22 v1"
<Exec Command="dotnet dotnet-swagger.dll tofile --host http://localhost:57556/ --output %22$(MSBuildThisFileDirectory)wwwroot/swagger/v1/swagger_$(TargetFramework).json%22 %22$(OutputPath)$(AssemblyName).dll%22 v1"
WorkingDirectory="$(DotNetSwaggerPath)" />
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi": "3.0.1",
"info": {
"title": "CliExampleWithFactory",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:57556/"
}
],
"paths": {
"/products": {
"get": {
"tags": [
"Products"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Product": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"description": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"openapi": "3.0.1",
"info": {
"title": "CliExampleWithFactory",
"version": "1.0"
},
"servers": [
{
"url": "http://localhost:57556/"
}
],
"paths": {
"/products": {
"get": {
"tags": [
"Products"
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Product"
}
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Product": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int32"
},
"description": {
"type": "string",
"nullable": true
}
},
"additionalProperties": false
}
}
}
}
4 changes: 2 additions & 2 deletions test/WebSites/NswagClientExample/NswagClientExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
</ItemGroup>

<Target Name="SwaggerToFile" AfterTargets="AfterBuild">
<Exec Command="dotnet dotnet-swagger.dll tofile --host http://example.com --output %22$(MSBuildThisFileDirectory)swagger.json%22 %22$(OutputPath)$(AssemblyName).dll%22 v1"
<Exec Command="dotnet dotnet-swagger.dll tofile --host http://example.com --output %22$(MSBuildThisFileDirectory)swagger_$(TargetFramework).json%22 %22$(OutputPath)$(AssemblyName).dll%22 v1"
WorkingDirectory="$(DotNetSwaggerPath)" />
</Target>

<Target Name="NSwag" AfterTargets="SwaggerToFile">
<Exec Command="$(_NSwagTool) openapi2csclient /input:swagger.json /namespace:NSwagClient /output:NSwagClient/Client.cs" />
<Exec Command="$(_NSwagTool) openapi2csclient /input:swagger_$(TargetFramework).json /namespace:NSwagClient /output:NSwagClient/Client.cs" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@
}
}
}
}
}

0 comments on commit 77cde47

Please sign in to comment.