Skip to content

Commit f43844f

Browse files
authored
Merge pull request #5 from PandaTechAM/development
swagger support enhancement
2 parents 123b306 + 5e529f7 commit f43844f

File tree

17 files changed

+150
-105
lines changed

17 files changed

+150
-105
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using DistributedCache.Extensions;
21
using DistributedCache.Options;
32
using FluentMinimalApiMapper;
43
using Microsoft.AspNetCore.Mvc;
5-
using SharedKernel.Demo;
4+
using SharedKernel.Demo2;
65
using ResponseCrafter.Enums;
76
using ResponseCrafter.Extensions;
87
using SharedKernel.Extensions;
@@ -18,7 +17,7 @@
1817
AssemblyRegistry.Add(typeof(Program).Assembly);
1918

2019
builder
21-
.AddPandaVault()
20+
// .AddPandaVault()
2221
.AddSerilog()
2322
.AddResponseCrafter(NamingConvention.ToSnakeCase)
2423
.AddOpenApi()
@@ -48,14 +47,15 @@
4847
.UseOpenApi()
4948
.MapControllers();
5049

50+
5151
app.MapPost("/params", ([AsParameters] TestTypes testTypes) => TypedResults.Ok(testTypes));
5252
app.MapPost("/body", ([FromBody] TestTypes testTypes) => TypedResults.Ok(testTypes));
5353

5454

5555
app.LogStartSuccess();
5656
app.Run();
5757

58-
namespace SharedKernel.Demo
58+
namespace SharedKernel.Demo2
5959
{
6060
public class TestTypes
6161
{

test/SharedKernel.Demo/Properties/launchSettings.json renamed to Shared.Kernel.Demo/Properties/launchSettings.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
2-
"$schema": "http://json.schemastore.org/launchsettings.json",
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
33
"profiles": {
44
"http": {
55
"commandName": "Project",
66
"dotnetRunMessages": true,
7-
"launchBrowser": true,
8-
"launchUrl": "swagger",
7+
"launchBrowser": false,
98
"applicationUrl": "http://localhost:80",
109
"environmentVariables": {
1110
"ASPNETCORE_ENVIRONMENT": "Development"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\src\SharedKernel\SharedKernel.csproj" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<Content Update="appsettings.json">
19+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
20+
</Content>
21+
<Content Update="appsettings.Development.json">
22+
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
23+
</Content>
24+
</ItemGroup>
25+
26+
</Project>
File renamed without changes.

SharedKernel.sln

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedKernel", "src\SharedK
44
EndProject
55
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedKernel.Tests", "test\SharedKernel.Tests\SharedKernel.Tests.csproj", "{0305E58F-1C47-454C-B10B-A223F2561A85}"
66
EndProject
7-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedKernel.Demo", "test\SharedKernel.Demo\SharedKernel.Demo.csproj", "{8A6AA36D-1CEF-4018-9C9D-7D029F3EAECE}"
8-
EndProject
97
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F8A6DCFE-8924-49A4-B3E9-2034593F54E5}"
108
EndProject
119
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{FEE159A2-74A0-4469-9B93-52987CA1A3CA}"
@@ -19,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1917
.editorconfig = .editorconfig
2018
EndProjectSection
2119
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared.Kernel.Demo", "Shared.Kernel.Demo\Shared.Kernel.Demo.csproj", "{1CD76A30-4A74-4F54-AC0C-AEDD92408553}"
21+
EndProject
2222
Global
2323
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2424
Debug|Any CPU = Debug|Any CPU
@@ -33,14 +33,14 @@ Global
3333
{0305E58F-1C47-454C-B10B-A223F2561A85}.Debug|Any CPU.Build.0 = Debug|Any CPU
3434
{0305E58F-1C47-454C-B10B-A223F2561A85}.Release|Any CPU.ActiveCfg = Release|Any CPU
3535
{0305E58F-1C47-454C-B10B-A223F2561A85}.Release|Any CPU.Build.0 = Release|Any CPU
36-
{8A6AA36D-1CEF-4018-9C9D-7D029F3EAECE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37-
{8A6AA36D-1CEF-4018-9C9D-7D029F3EAECE}.Debug|Any CPU.Build.0 = Debug|Any CPU
38-
{8A6AA36D-1CEF-4018-9C9D-7D029F3EAECE}.Release|Any CPU.ActiveCfg = Release|Any CPU
39-
{8A6AA36D-1CEF-4018-9C9D-7D029F3EAECE}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{1CD76A30-4A74-4F54-AC0C-AEDD92408553}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{1CD76A30-4A74-4F54-AC0C-AEDD92408553}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{1CD76A30-4A74-4F54-AC0C-AEDD92408553}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{1CD76A30-4A74-4F54-AC0C-AEDD92408553}.Release|Any CPU.Build.0 = Release|Any CPU
4040
EndGlobalSection
4141
GlobalSection(NestedProjects) = preSolution
4242
{25001943-A870-4E17-A9B9-0D190CEC819B} = {F8A6DCFE-8924-49A4-B3E9-2034593F54E5}
43-
{8A6AA36D-1CEF-4018-9C9D-7D029F3EAECE} = {FEE159A2-74A0-4469-9B93-52987CA1A3CA}
4443
{0305E58F-1C47-454C-B10B-A223F2561A85} = {FEE159A2-74A0-4469-9B93-52987CA1A3CA}
44+
{1CD76A30-4A74-4F54-AC0C-AEDD92408553} = {FEE159A2-74A0-4469-9B93-52987CA1A3CA}
4545
EndGlobalSection
4646
EndGlobal
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Http;
3+
4+
namespace SharedKernel.OpenApi;
5+
6+
internal static class EmbeddedFilesExtension
7+
{
8+
private static readonly HashSet<string> AllowedResources = new(StringComparer.OrdinalIgnoreCase)
9+
{
10+
"panda-style.css",
11+
"panda-style.js",
12+
"favicon.svg",
13+
"logo.svg",
14+
"logo-wording.svg"
15+
};
16+
17+
internal static WebApplication MapSwaggerUiAssetEndpoint(this WebApplication app)
18+
{
19+
app.Map("/swagger-resources/{resourceName}", async (HttpContext context, string resourceName) =>
20+
{
21+
if (!AllowedResources.Contains(resourceName))
22+
{
23+
context.Response.StatusCode = StatusCodes.Status404NotFound;
24+
await context.Response.WriteAsync($"Resource '{resourceName}' not found.");
25+
return;
26+
}
27+
28+
var assembly = typeof(EmbeddedFilesExtension).Assembly;
29+
var resourcePath = assembly.GetManifestResourceNames()
30+
.FirstOrDefault(x => x.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));
31+
32+
if (resourcePath == null)
33+
{
34+
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
35+
await context.Response.WriteAsync($"Resource '{resourceName}' not found in assembly.");
36+
return;
37+
}
38+
39+
await using var stream = assembly.GetManifestResourceStream(resourcePath);
40+
if (stream == null)
41+
{
42+
context.Response.StatusCode = StatusCodes.Status500InternalServerError;
43+
await context.Response.WriteAsync($"Failed to load resource '{resourceName}'.");
44+
return;
45+
}
46+
47+
context.Response.ContentType = GetContentType(resourceName);
48+
await stream.CopyToAsync(context.Response.Body);
49+
})
50+
.WithGroupName("SwaggerUiAssetEndpoint");
51+
52+
return app;
53+
}
54+
55+
private static string GetContentType(string resourceName) =>
56+
Path.GetExtension(resourceName).ToLowerInvariant() switch
57+
{
58+
".css" => "text/css",
59+
".js" => "application/javascript",
60+
".svg" => "image/svg+xml",
61+
".png" => "image/png",
62+
".jpg" or ".jpeg" => "image/jpeg",
63+
_ => "application/octet-stream"
64+
};
65+
}

src/SharedKernel/OpenApi/OpenApiExtensions.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class OpenApiExtensions
1515
public static WebApplicationBuilder AddOpenApi(this WebApplicationBuilder builder,
1616
Action<OpenApiOptions>? configureOptions = null)
1717
{
18+
1819
var openApiConfiguration = builder.Configuration
1920
.GetSection("OpenApi")
2021
.Get<OpenApiConfig>();
@@ -51,14 +52,10 @@ public static WebApplication UseOpenApi(this WebApplication app)
5152
return app;
5253
}
5354

54-
app.MapStaticAssets();
5555
app.MapOpenApi();
56+
app.MapSwaggerUiAssetEndpoint();
5657
app.MapSwaggerUi(openApiConfiguration);
57-
app.MapScalarApiReference(options =>
58-
{
59-
options.Theme = ScalarTheme.Kepler;
60-
options.Favicon = "/assets/images/favicon.svg";
61-
});
58+
app.MapScalarUi();
6259
return app;
6360
}
6461

File renamed without changes.

test/SharedKernel.Demo/wwwroot/assets/images/logo-wording.svg renamed to src/SharedKernel/OpenApi/UiAssets/logo-wording.svg

File renamed without changes.

0 commit comments

Comments
 (0)