Skip to content

Commit 537dfe6

Browse files
Refactor OpenAPI setup and clean up code
1 parent bc64b02 commit 537dfe6

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

src/Modules/Grand.Module.Api/Extensions/OpenApiOptionsExtensions.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@ public static void AddOperationTransformer(this OpenApiOptions options)
1111
{
1212
options.AddOperationTransformer((operation, context, cancellationToken) =>
1313
{
14-
/*if (context.Description.ActionDescriptor.EndpointMetadata.OfType<IAuthorizeData>().Any())
15-
{
16-
operation.Security = new List<OpenApiSecurityRequirement>
17-
{
18-
new OpenApiSecurityRequirement
19-
{
20-
[new OpenApiSecurityScheme { Scheme = "Bearer" }] = new List<string>()
21-
}
22-
};
23-
}*/
2414
var enableQuery = context.Description.ActionDescriptor?.FilterDescriptors.Where(x => x.Filter.GetType() == typeof(EnableQueryAttribute)).FirstOrDefault();
2515
if (enableQuery != null)
2616
{
@@ -105,19 +95,25 @@ public static void AddCsrfTokenTransformer(this OpenApiOptions options)
10595
return Task.CompletedTask;
10696
});
10797
}
108-
public static void AddContactDocumentTransformer(this OpenApiOptions options, string name)
98+
public static void AddContactDocumentTransformer(this OpenApiOptions options, string name, string version)
10999
{
110100
options.AddDocumentTransformer((document, context, cancellationToken) =>
111101
{
112-
document.Info.Contact = new OpenApiContact {
113-
Name = name,
114-
Email = "[email protected]",
115-
Url = new Uri("https://grandnode.com")
116-
};
117-
document.Info.License = new OpenApiLicense {
118-
Name = "GNU General Public License v3.0",
119-
Url = new Uri("https://github.com/grandnode/grandnode2/blob/main/LICENSE")
102+
document.Info = new OpenApiInfo {
103+
Description = "Grandnode API",
104+
Title = name,
105+
Version = version,
106+
Contact = new OpenApiContact {
107+
Name = name,
108+
Email = "[email protected]",
109+
Url = new Uri("https://grandnode.com")
110+
},
111+
License = new OpenApiLicense {
112+
Name = "GNU General Public License v3.0",
113+
Url = new Uri("https://github.com/grandnode/grandnode2/blob/main/LICENSE")
114+
}
120115
};
116+
121117
return Task.CompletedTask;
122118
});
123119
}

src/Modules/Grand.Module.Api/Infrastructure/OpenApiStartup.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Grand.Module.Api.Attributes;
55
using Grand.Module.Api.Infrastructure.Extensions;
66
using Grand.Module.Api.Infrastructure.Transformers;
7+
using Grand.SharedKernel.Extensions;
78
using Microsoft.AspNetCore.Builder;
89
using Microsoft.AspNetCore.Hosting;
910
using Microsoft.AspNetCore.Mvc.ApiExplorer;
@@ -66,10 +67,10 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config
6667

6768
private void ConfigureBackendApi(IServiceCollection services)
6869
{
69-
services.AddOpenApi("v1", options =>
70+
services.AddOpenApi(ApiConstants.ApiGroupNameV1, options =>
7071
{
7172
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
72-
options.AddContactDocumentTransformer("Grandnode Backend API");
73+
options.AddContactDocumentTransformer("Grandnode Backend API", ApiConstants.ApiGroupNameV1);
7374
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
7475
options.AddSchemaTransformer<EnumSchemaTransformer>();
7576
options.AddOperationTransformer();
@@ -78,10 +79,10 @@ private void ConfigureBackendApi(IServiceCollection services)
7879

7980
private void ConfigureFrontendApi(IServiceCollection services)
8081
{
81-
services.AddOpenApi("v2", options =>
82+
services.AddOpenApi(ApiConstants.ApiGroupNameV2, options =>
8283
{
8384
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
84-
options.AddContactDocumentTransformer("Grandnode Frontend API");
85+
options.AddContactDocumentTransformer("Grandnode Frontend API", ApiConstants.ApiGroupNameV2);
8586
options.AddDocumentTransformer<BearerSecuritySchemeTransformer>();
8687
options.AddSchemaTransformer<EnumSchemaTransformer>();
8788
options.AddSchemaTransformer<IgnoreFieldSchemaTransformer>();

0 commit comments

Comments
 (0)