当前,自定义授权属性上的NSwag生成存在问题。我在其他地方完全找不到关于这个问题的任何东西。
我为我的WebAPI创建了一个自定义属性,大致遵循了推荐的这里模式
internal class AuthorizeWithPermissionAttribute : AuthorizeAttribute
{
const string POLICY_PREFIX = "HasPermission";
public AuthorizeWithPermissionAttribute(string permission) { // logic excluded for brevity }
public string Permission
{
get { // code }
set { // code }
}
}然后我用这样的属性来装饰我的控制器
[AuthorizeWithPermission("MY_PERMISSION_NAME")]
public class MyController: ControllerBase
{
// Controller logic.
}到目前为止我已经试过了
[OpenApiIgnore]应用于类型,然后在Startup.cs中写入自定义筛选器时,此筛选器不起作用下面是我遇到的错误:
Executing file 'nswag.json' with variables 'Configuration=Debug'...
3> Launcher directory: %UserDirectory%\.nuget\packages\nswag.msbuild\13.8.2\tools\NetCore31
3> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
3> ---> System.NullReferenceException: Object reference not set to an instance of an object.
3> at %SLNNAME%.Authorization.Attributes.AuthorizeWithPermissionAttribute..ctor(String permission) in %SLNDirectory%\Authorization\Attributes\AuthorizeWithPermissionAttribute.cs:line 14
3> at System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, RuntimeType type, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
3> at System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, RuntimeType type, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
3> at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes)
3> at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType)
3> at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
3> at System.RuntimeType.GetCustomAttributes(Boolean inherit)
3> at Microsoft.AspNetCore.Mvc.ApplicationModels.DefaultApplicationModelProvider.CreateControllerModel(TypeInfo typeInfo)
3> at Microsoft.AspNetCore.Mvc.ApplicationModels.DefaultApplicationModelProvider.OnProvidersExecuting(ApplicationModelProviderContext context)
3> at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory.CreateApplicationModel(IEnumerable`1 controllerTypes)
3> at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
3> at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
3> at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
3> at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
3> at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.get_ActionDescriptors()
3> at Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescriptionGroupCollectionProvider.get_ApiDescriptionGroups()
3> at NSwag.Generation.AspNetCore.AspNetCoreOpenApiDocumentGenerator.GenerateAsync(Object serviceProvider)
3> at NSwag.AspNetCore.OpenApiDocumentProvider.GenerateAsync(String documentName)
3> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.GenerateDocumentWithDocumentProviderAsync(IServiceProvider serviceProvider) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 287
3> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.GenerateDocumentAsync(AssemblyLoader assemblyLoader, IServiceProvider serviceProvider, String currentWorkingDirectory) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 276
3> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.<>c__DisplayClass0_0.<<Process>b__0>d.MoveNext() in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 30
3> --- End of stack trace from previous location where exception was thrown ---
3> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToOpenApiGeneratorCommandEntryPoint.Process(String commandContent, String outputFile, String applicationName) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiGeneratorCommandEntryPoint.cs:line 29
3> --- End of inner exception stack trace ---
3> at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
3> at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
3> at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
3> at NSwag.AspNetCore.Launcher.Program.Main(String[] args) in C:\projects\nswag\src\NSwag.AspNetCore.Launcher\Program.cs:line 170
3> System.InvalidOperationException: Swagger generation failed with non-zero exit code '1'.
3> at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 221
3> at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 280
3> at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81
3> at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 86
3> at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32
3> at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
3> at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
3> at NConsole.CommandLineProcessor.Process(String[] args, Object input)
3> at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56
3> %SLNDirectory%.csproj(59,5): error MSB3073: The command "dotnet "%UserDirectory%\.nuget\packages\nswag.msbuild\13.8.2\build\../tools/NetCore31/dotnet-nswag.dll" run nswag.json /variables:Configuration=Debug" exited with code -1.
3> Done executing task "Exec" -- FAILED.发布于 2021-02-24 19:52:01
逻辑!构造函数中的逻辑是问题所在。在这里这个很管用。希望有人不会像我一样花两个小时在这上面
public class AuthorizeWithPermissionAttribute : AuthorizeAttribute
{
const string POLICY_PREFIX = "HasPermission";
public AuthorizeWithPermissionAttribute(string permission)
{
this.Permission = permission;
}
public string Permission { get => Policy; set => Policy = $"{POLICY_PREFIX}{value}"; }
}https://stackoverflow.com/questions/66357618
复制相似问题