Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Improved Swagger - separate definition for backend and frontend API

support3278543bee44
diff --git a/src/API/Grand.Api/Controllers/BaseODataController.cs b/src/API/Grand.Api/Controllers/BaseODataController.cs
index 09ac5df23..73c35ae38 100644
--- a/src/API/Grand.Api/Controllers/BaseODataController.cs
+++ b/src/API/Grand.Api/Controllers/BaseODataController.cs
@@ -11,7 +11,7 @@ namespace Grand.Api.Controllers
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[ODataRouteComponent]
[Route("odata/[controller]")]
- [ApiExplorerSettings(IgnoreApi = false)]
+ [ApiExplorerSettings(IgnoreApi = false, GroupName = "v1")]
[AuthorizeApiAdmin]
[ServiceFilter(typeof(ModelValidationAttribute))]
public abstract partial class BaseODataController : ODataController
diff --git a/src/API/Grand.Api/Controllers/TokenController.cs b/src/API/Grand.Api/Controllers/TokenController.cs
index 7158b469b..7d3d30902 100644
--- a/src/API/Grand.Api/Controllers/TokenController.cs
+++ b/src/API/Grand.Api/Controllers/TokenController.cs
@@ -11,7 +11,7 @@ namespace Grand.Api.Controllers
[ApiController]
[Area("Api")]
[Route("[area]/[controller]/[action]")]
- [ApiExplorerSettings(IgnoreApi = false)]
+ [ApiExplorerSettings(IgnoreApi = false, GroupName = "v1")]
[SwaggerTag(description: "Create token")]
public class TokenController : Controller
{
diff --git a/src/API/Grand.Api/Controllers/TokenWebController.cs b/src/API/Grand.Api/Controllers/TokenWebController.cs
index fec2cfa7e..e5618b023 100644
--- a/src/API/Grand.Api/Controllers/TokenWebController.cs
+++ b/src/API/Grand.Api/Controllers/TokenWebController.cs
@@ -15,7 +15,10 @@ using System.Security.Claims;
namespace Grand.Api.Controllers
{
- public class TokenWebController : Controller
+ [ApiExplorerSettings(GroupName = "v2")]
+ [ApiController]
+ [Route("[controller]/[action]")]
+ public class TokenWebController : ControllerBase
{
private readonly ICustomerService _customerService;
private readonly IMediator _mediator;
diff --git a/src/API/Grand.Api/Extensions/AddParamOperationFilter.cs b/src/API/Grand.Api/Extensions/AddParamOperationFilter.cs
index 37d1b32d3..843dd9e8d 100644
--- a/src/API/Grand.Api/Extensions/AddParamOperationFilter.cs
+++ b/src/API/Grand.Api/Extensions/AddParamOperationFilter.cs
@@ -7,9 +7,15 @@ namespace Grand.Api.Extensions
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
- if (context.ApiDescription.HttpMethod == "GET" && context.ApiDescription.ParameterDescriptions.Count == 0)
+ if (context.ApiDescription.GroupName == "v1" && context.ApiDescription.HttpMethod == "GET" && context.ApiDescription.ParameterDescriptions.Count == 0)
{
- operation.Parameters.Add(new OpenApiParameter() {
+ ApplyV1(operation, context);
+ }
+ }
+
+ private void ApplyV1(OpenApiOperation operation, OperationFilterContext context)
+ {
+ operation.Parameters.Add(new OpenApiParameter() {
Name = "$top",
AllowReserved = true,
In = ParameterLocation.Query,
@@ -61,7 +67,6 @@ namespace Grand.Api.Extensions
Type = "string"
}
});
- }
}
}
}
diff --git a/src/API/Grand.Api/Extensions/SwaggerODataControllerDocumentFilter.cs b/src/API/Grand.Api/Extensions/SwaggerODataControllerDocumentFilter.cs
new file mode 100644
index 000000000..8d64550a9
--- /dev/null
+++ b/src/API/Grand.Api/Extensions/SwaggerODataControllerDocumentFilter.cs
@@ -0,0 +1,31 @@
+using Microsoft.AspNetCore.Mvc.ApiExplorer;
+using Microsoft.AspNetCore.Mvc.Controllers;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.SwaggerGen;
+
+namespace Grand.Api.Extensions;
+
+public class SwaggerODataControllerDocumentFilter : IDocumentFilter
+{
+ public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
+ {
+ // remove controller
+ foreach (ApiDescription apiDescription in context.ApiDescriptions)
+ {
+ var actionDescriptor = (ControllerActionDescriptor)apiDescription.ActionDescriptor;
+ if (actionDescriptor.ControllerName == "Metadata")
+ {
+ swaggerDoc.Paths.Remove($"/{apiDescription.RelativePath}");
+ }
+ }
+
+ // remove schemas
+ foreach (var (key, _) in swaggerDoc.Components.Schemas)
+ {
+ if (key.Contains("Edm") || key.Contains("OData"))
+ {
+ swaggerDoc.Components.Schemas.Remove(key);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Infrastructure/ODataStartup.cs b/src/API/Grand.Api/Infrastructure/ODataStartup.cs
index 5e3acbd1d..6e4d084ad 100644
--- a/src/API/Grand.Api/Infrastructure/ODataStartup.cs
+++ b/src/API/Grand.Api/Infrastructure/ODataStartup.cs
@@ -91,9 +91,6 @@ namespace Grand.Api.Infrastructure
private void RegisterRequestHandler(IServiceCollection services)
{
-
- //Workaround - there is a problem with register generic type with IRequestHandler
-
services.AddScoped(typeof(IRequestHandler<GetGenericQuery<CountryDto, Domain.Directory.Country>,
IQueryable<CountryDto>>), typeof(GetGenericQueryHandler<CountryDto, Domain.Directory.Country>));
diff --git a/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs b/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
index 8366da4af..75c3b7453 100644
--- a/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
+++ b/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
@@ -25,7 +25,9 @@ namespace Grand.Api.Infrastructure
application.UseSwagger();
application.UseSwaggerUI(c =>
{
- c.SwaggerEndpoint("/swagger/v1/swagger.json", "Grandnode API V1");
+ c.SwaggerEndpoint("/swagger/v1/swagger.json", "Grandnode Backend API V1");
+ c.SwaggerEndpoint("/swagger/v2/swagger.json", "Grandnode Frontend API V1");
+
});
}
}
@@ -38,7 +40,9 @@ namespace Grand.Api.Infrastructure
services.AddSwaggerGen(c =>
{
- c.SwaggerDoc("v1", new OpenApiInfo { Title = "Grandnode API", Version = "v1" });
+ c.SwaggerDoc("v1", new OpenApiInfo { Title = "Grandnode Backend API", Version = "v1" });
+ c.SwaggerDoc("v2", new OpenApiInfo { Title = "Grandnode Frontend API", Version = "v2" });
+
c.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, //Name the security scheme
new OpenApiSecurityScheme
{
@@ -59,10 +63,11 @@ namespace Grand.Api.Infrastructure
}
});
c.OperationFilter<AddParamOperationFilter>();
+ c.DocumentFilter<SwaggerODataControllerDocumentFilter>();
c.EnableAnnotations();
c.SchemaFilter<EnumSchemaFilter>();
});
-
+
}
}