-
Notifications
You must be signed in to change notification settings - Fork 525
Expand file tree
/
Copy pathIgnoreFieldSchemaTransformer.cs
More file actions
35 lines (27 loc) · 1.24 KB
/
IgnoreFieldSchemaTransformer.cs
File metadata and controls
35 lines (27 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using Grand.SharedKernel.Attributes;
using Microsoft.AspNetCore.OpenApi;
using Microsoft.OpenApi;
using System.Reflection;
namespace Grand.Module.Api.Infrastructure.Transformers;
public class IgnoreFieldSchemaTransformer : IOpenApiSchemaTransformer
{
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken)
{
var type = context.JsonTypeInfo.Type;
if (type == null || schema.Properties == null || !schema.Properties.Any()) return Task.CompletedTask;
var excludedPropertyNames = type
.GetProperties()
.Where(
t => t.GetCustomAttribute<IgnoreApiAttribute>() != null
).Select(d => d.Name).ToList();
if (!excludedPropertyNames.Any()) return Task.CompletedTask;
var excludedSchemaPropertyKey = schema.Properties
.Where(
ap => excludedPropertyNames.Any(
pn => pn.Equals(ap.Key, StringComparison.InvariantCultureIgnoreCase)
)
).Select(ap => ap.Key).ToList();
foreach (var propertyToExclude in excludedSchemaPropertyKey) schema.Properties.Remove(propertyToExclude);
return Task.CompletedTask;
}
}