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.

Allow to ignore property on model for generate swagger documentation - add new attribute

support7a3afcddb590
diff --git a/src/API/Grand.Api/Infrastructure/EnumSchemaFilter.cs b/src/API/Grand.Api/Infrastructure/EnumSchemaFilter.cs
deleted file mode 100644
index ee0e763c9..000000000
--- a/src/API/Grand.Api/Infrastructure/EnumSchemaFilter.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-using Microsoft.OpenApi.Any;
-using Microsoft.OpenApi.Models;
-using Swashbuckle.AspNetCore.SwaggerGen;
-
-namespace Grand.Api.Infrastructure
-{
- public class EnumSchemaFilter : ISchemaFilter
- {
- public void Apply(OpenApiSchema schema, SchemaFilterContext context)
- {
- if (context.Type.IsEnum)
- {
- var enumValues = schema.Enum.ToArray();
- foreach (var item in enumValues)
- {
- var value = (OpenApiPrimitive<int>)item;
- var name = Enum.GetName(context.Type, value.Value);
- schema.Description += $"{value.Value} - {name}; ";
- }
- }
- }
- }
-}
-
diff --git a/src/API/Grand.Api/Infrastructure/Filters/EnumSchemaFilter.cs b/src/API/Grand.Api/Infrastructure/Filters/EnumSchemaFilter.cs
new file mode 100644
index 000000000..855d3c2ac
--- /dev/null
+++ b/src/API/Grand.Api/Infrastructure/Filters/EnumSchemaFilter.cs
@@ -0,0 +1,23 @@
+using Microsoft.OpenApi.Any;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.SwaggerGen;
+
+namespace Grand.Api.Infrastructure.Filters
+{
+ public class EnumSchemaFilter : ISchemaFilter
+ {
+ public void Apply(OpenApiSchema schema, SchemaFilterContext context)
+ {
+ if (!context.Type.IsEnum) return;
+
+ var enumValues = schema.Enum.ToArray();
+ foreach (var item in enumValues)
+ {
+ var value = (OpenApiPrimitive<int>)item;
+ var name = Enum.GetName(context.Type, value.Value);
+ schema.Description += $"{value.Value} - {name}; ";
+ }
+ }
+ }
+}
+
diff --git a/src/API/Grand.Api/Infrastructure/Filters/IgnoreFieldFilter.cs b/src/API/Grand.Api/Infrastructure/Filters/IgnoreFieldFilter.cs
new file mode 100644
index 000000000..dfc88e499
--- /dev/null
+++ b/src/API/Grand.Api/Infrastructure/Filters/IgnoreFieldFilter.cs
@@ -0,0 +1,42 @@
+using Grand.SharedKernel.Attributes;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.SwaggerGen;
+using System.Reflection;
+
+namespace Grand.Api.Infrastructure.Filters;
+
+public class IgnoreFieldFilter : ISchemaFilter
+{
+ public void Apply(OpenApiSchema schema, SchemaFilterContext context)
+ {
+ var type = context.Type;
+ if (!schema.Properties.Any() || type == null)
+ {
+ return;
+ }
+
+ var excludedPropertyNames = type
+ .GetProperties()
+ .Where(
+ t => t.GetCustomAttribute<IgnoreApiAttribute>() != null
+ ).Select(d => d.Name).ToList();
+
+ if (!excludedPropertyNames.Any())
+ {
+ return;
+ }
+
+ var excludedSchemaPropertyKey = schema.Properties
+ .Where(
+ ap => excludedPropertyNames.Any(
+ pn => pn.Equals(ap.Key, StringComparison.InvariantCultureIgnoreCase)
+ )
+ ).Select(ap => ap.Key);
+
+ foreach (var propertyToExclude in excludedSchemaPropertyKey)
+ {
+ schema.Properties.Remove(propertyToExclude);
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs b/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
index 75c3b7453..a638d00cc 100644
--- a/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
+++ b/src/API/Grand.Api/Infrastructure/SwaggerStartup.cs
@@ -1,4 +1,5 @@
using Grand.Api.Extensions;
+using Grand.Api.Infrastructure.Filters;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -66,6 +67,7 @@ namespace Grand.Api.Infrastructure
c.DocumentFilter<SwaggerODataControllerDocumentFilter>();
c.EnableAnnotations();
c.SchemaFilter<EnumSchemaFilter>();
+ c.SchemaFilter<IgnoreFieldFilter>();
});
}
diff --git a/src/Web/Grand.Web.Common/Attributes/IgnoreApiAttribute.cs b/src/Core/Grand.SharedKernel/Attributes/IgnoreApiAttribute.cs
similarity index 59%
rename from src/Web/Grand.Web.Common/Attributes/IgnoreApiAttribute.cs
rename to src/Core/Grand.SharedKernel/Attributes/IgnoreApiAttribute.cs
index 31e9c8e28..2756d4e23 100644
--- a/src/Web/Grand.Web.Common/Attributes/IgnoreApiAttribute.cs
+++ b/src/Core/Grand.SharedKernel/Attributes/IgnoreApiAttribute.cs
@@ -1,6 +1,6 @@
-namespace Grand.Web.Common.Attributes;
+namespace Grand.SharedKernel.Attributes;
-[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
+[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Property)]
public class IgnoreApiAttribute : Attribute
{
}
\ No newline at end of file
diff --git a/src/Web/Grand.Web.Common/ApiExplorer/MetadataApiDescriptionProvider.cs b/src/Web/Grand.Web.Common/ApiExplorer/MetadataApiDescriptionProvider.cs
index 281ebdeff..48960ecdf 100644
--- a/src/Web/Grand.Web.Common/ApiExplorer/MetadataApiDescriptionProvider.cs
+++ b/src/Web/Grand.Web.Common/ApiExplorer/MetadataApiDescriptionProvider.cs
@@ -1,7 +1,6 @@
//https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.ApiExplorer/src/DefaultApiDescriptionProvider.cs
-
+using Grand.SharedKernel.Attributes;
using Grand.SharedKernel.Extensions;
-using Grand.Web.Common.Attributes;
using Grand.Web.Common.Controllers;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
diff --git a/src/Web/Grand.Web.Common/Controllers/BasePublicController.cs b/src/Web/Grand.Web.Common/Controllers/BasePublicController.cs
index 3773fab67..79f3a530e 100644
--- a/src/Web/Grand.Web.Common/Controllers/BasePublicController.cs
+++ b/src/Web/Grand.Web.Common/Controllers/BasePublicController.cs
@@ -1,4 +1,4 @@
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Filters;
using Microsoft.AspNetCore.Mvc;
diff --git a/src/Web/Grand.Web/Controllers/AccountController.cs b/src/Web/Grand.Web/Controllers/AccountController.cs
index 194bd26df..625b52792 100644
--- a/src/Web/Grand.Web/Controllers/AccountController.cs
+++ b/src/Web/Grand.Web/Controllers/AccountController.cs
@@ -13,7 +13,7 @@ using Grand.Domain.Stores;
using Grand.Infrastructure;
using Grand.Infrastructure.Extensions;
using Grand.Web.Commands.Models.Customers;
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Security.Captcha;
@@ -22,6 +22,7 @@ using Grand.Web.Features.Models.Common;
using Grand.Web.Features.Models.Customers;
using Grand.Web.Models.Customer;
using MediatR;
+using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@@ -550,6 +551,7 @@ namespace Grand.Web.Controllers
#region My account / Info
[HttpGet]
+ [Authorize]
public virtual async Task<IActionResult> Info()
{
if (!await _groupService.IsRegistered(_workContext.CurrentCustomer))
@@ -719,7 +721,7 @@ namespace Grand.Web.Controllers
{
var address = model.Address.ToEntity(_workContext.CurrentCustomer, addressSettings);
address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes });;
+ { SelectedAttributes = model.Address.SelectedAttributes });
address.CreatedOnUtc = DateTime.UtcNow;
customer.Addresses.Add(address);
@@ -792,7 +794,7 @@ namespace Grand.Web.Controllers
{
address = model.Address.ToEntity(address, _workContext.CurrentCustomer, addressSettings);
address.Attributes = await _mediator.Send(new GetParseCustomAddressAttributes
- { SelectedAttributes = model.Address.SelectedAttributes });;
+ { SelectedAttributes = model.Address.SelectedAttributes });
await _customerService.UpdateAddress(address, customer.Id);
if (customer.BillingAddress?.Id == address.Id)
@@ -1201,7 +1203,7 @@ namespace Grand.Web.Controllers
if (!ModelState.IsValid) return View(model);
- var result = await _mediator.Send(new SubAccountAddCommand {
+ _ = await _mediator.Send(new SubAccountAddCommand {
Customer = _workContext.CurrentCustomer,
Model = model,
Store = _workContext.CurrentStore
diff --git a/src/Web/Grand.Web/Controllers/CommonController.cs b/src/Web/Grand.Web/Controllers/CommonController.cs
index 26c2233c7..510c92c00 100644
--- a/src/Web/Grand.Web/Controllers/CommonController.cs
+++ b/src/Web/Grand.Web/Controllers/CommonController.cs
@@ -10,7 +10,7 @@ using Grand.Domain.Tax;
using Grand.Infrastructure;
using Grand.Infrastructure.Configuration;
using Grand.Web.Commands.Models.Customers;
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Themes;
diff --git a/src/Web/Grand.Web/Controllers/ContactController.cs b/src/Web/Grand.Web/Controllers/ContactController.cs
index 55babf887..88b8c8fcf 100644
--- a/src/Web/Grand.Web/Controllers/ContactController.cs
+++ b/src/Web/Grand.Web/Controllers/ContactController.cs
@@ -143,7 +143,6 @@ public class ContactController : BasePublicController
var fileBinary = uploadedFile.GetDownloadBits();
- const string qqFileNameParameter = "qqfilename";
var fileName = uploadedFile.FileName;
//remove path (passed in IE)
diff --git a/src/Web/Grand.Web/Controllers/HomeController.cs b/src/Web/Grand.Web/Controllers/HomeController.cs
index 6efd054b0..4da8f4d2b 100644
--- a/src/Web/Grand.Web/Controllers/HomeController.cs
+++ b/src/Web/Grand.Web/Controllers/HomeController.cs
@@ -1,4 +1,4 @@
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Controllers;
using Microsoft.AspNetCore.Mvc;
diff --git a/src/Web/Grand.Web/Controllers/OrderController.cs b/src/Web/Grand.Web/Controllers/OrderController.cs
index fb259c895..d745b9e8b 100644
--- a/src/Web/Grand.Web/Controllers/OrderController.cs
+++ b/src/Web/Grand.Web/Controllers/OrderController.cs
@@ -10,7 +10,7 @@ using Grand.Domain.Payments;
using Grand.Domain.Shipping;
using Grand.Infrastructure;
using Grand.Web.Commands.Models.Orders;
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Filters;
using Grand.Web.Common.Page;
diff --git a/src/Web/Grand.Web/Controllers/PushNotificationsController.cs b/src/Web/Grand.Web/Controllers/PushNotificationsController.cs
index fa410c44d..1825d0ee3 100644
--- a/src/Web/Grand.Web/Controllers/PushNotificationsController.cs
+++ b/src/Web/Grand.Web/Controllers/PushNotificationsController.cs
@@ -1,7 +1,7 @@
using Grand.Business.Core.Interfaces.Marketing.PushNotifications;
using Grand.Domain.PushNotifications;
using Grand.Infrastructure;
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Controllers;
using Microsoft.AspNetCore.Mvc;
diff --git a/src/Web/Grand.Web/Controllers/ShoppingCartController.cs b/src/Web/Grand.Web/Controllers/ShoppingCartController.cs
index b8f2a9e27..5f4954c14 100644
--- a/src/Web/Grand.Web/Controllers/ShoppingCartController.cs
+++ b/src/Web/Grand.Web/Controllers/ShoppingCartController.cs
@@ -15,7 +15,7 @@ using Grand.Domain.Media;
using Grand.Domain.Orders;
using Grand.Infrastructure;
using Grand.Web.Commands.Models.ShoppingCart;
-using Grand.Web.Common.Attributes;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Controllers;
using Grand.Web.Common.Extensions;
using Grand.Web.Common.Filters;
diff --git a/src/Web/Grand.Web/Models/Contact/ContactUsModel.cs b/src/Web/Grand.Web/Models/Contact/ContactUsModel.cs
index 3ba0a7a7a..5f9770f80 100644
--- a/src/Web/Grand.Web/Models/Contact/ContactUsModel.cs
+++ b/src/Web/Grand.Web/Models/Contact/ContactUsModel.cs
@@ -2,6 +2,7 @@
using Grand.Domain.Common;
using Grand.Infrastructure.ModelBinding;
using Grand.Infrastructure.Models;
+using Grand.SharedKernel.Attributes;
using Grand.Web.Common.Binders;
using Grand.Web.Common.Models;
using Grand.Web.Models.Common;
@@ -23,6 +24,7 @@ namespace Grand.Web.Models.Contact
[GrandResourceDisplayName("ContactUs.Subject")]
public string Subject { get; set; }
+ [IgnoreApi]
public bool SubjectEnabled { get; set; }
[GrandResourceDisplayName("ContactUs.Enquiry")]
@@ -31,17 +33,24 @@ namespace Grand.Web.Models.Contact
[GrandResourceDisplayName("ContactUs.FullName")]
public string FullName { get; set; }
+ [IgnoreApi]
public bool SuccessfullySent { get; set; }
+ [IgnoreApi]
public string Result { get; set; }
-
+
+ [IgnoreApi]
public bool DisplayCaptcha { get; set; }
public ICaptchaValidModel Captcha { get; set; } = new CaptchaModel();
[ModelBinder(BinderType = typeof(CustomAttributesBinder))]
public IList<CustomAttributeModel> Attributes { get; set; }
+ [IgnoreApi]
public string ContactAttributeInfo { get; set; }
+ [IgnoreApi]
public IList<CustomAttribute> ContactAttribute { get; set; }
+
+ [IgnoreApi]
public IList<ContactAttributeModel> ContactAttributes { get; set; }
public class ContactAttributeModel : BaseEntityModel