NSwag 无法识别自定义授权属性

问题描述

当前在自定义授权属性上生成 NSwag 有问题。我完全无法在其他地方找到有关此问题的任何信息。

我为我的 WebAPI 创建了一个自定义属性,并且或多或少地遵循了推荐的模式 here

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.
}

到目前为止我已经尝试过

  • 在属性上创建一个公共的无参数构造函数
  • 更改构造函数参数的类型(enum、int、string)
  • [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,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 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.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)