【问题标题】:Swagger.ObjectModel.Builders.RequiredFieldException: 'Paths' is requiredSwagger.ObjectModel.Builders.RequiredFieldException:“路径”是必需的
【发布时间】:2017-09-15 09:24:01
【问题描述】:

使用 Nancy v 1.4.1 和 Nancy.Swagger v 2.1.1(支持 Nancy v1 的最后一个),导航到 /api-docs 路径时出现上述错误。有任何想法吗?我没有看到任何设置步骤说明“路径”字段。

我的模块:

public class General : NancyModule
{
    public General()
    {


        Get["/","Home"] = parameters =>
        {
            try
            {
                return "home";// View["view/index.html"];
            }
            catch (Exception ex)
            {
                return ExceptionHelper.ExceptionResponse(Negotiate, ex);
            }
        };

        Get["/test/", "Test"] = parameters => {
            return "testie";
        };
    }
}

我的模块元数据:

public class GeneralMetadataModule : MetadataModule<PathItem>
{
    public GeneralMetadataModule(ISwaggerModelCatalog modelCatalog)
    {
        Describe["Test"] = description => description.AsSwagger(
            with => with.Operation(
                op => op.OperationId("Test")
                        .Tag("Users")
                        .Summary("The list of users")
                        .Description("This returns a list of users from our awesome app")));
    }
}

堆栈跟踪:

Nancy.RequestExecutionException:哦不! ---> Swagger.ObjectModel.Builders.RequiredFieldException: 'Paths' 是必需的。 在 C:\projects\nancy-swagger\src\Swagger.ObjectModel\Builders\SwaggerRootBuilder.cs:line 123 中的 Swagger.ObjectModel.Builders.SwaggerRootBuilder.Build() 在 C:\projects\nancy-swagger\src\Nancy.Swagger\Services\SwaggerMetadataProvider.cs:line 91 中的 Nancy.Swagger.Services.SwaggerMetadataProvider.GetSwaggerJson() 在 C:\projects\nancy-swagger\src\Nancy.Swagger\Modules\SwaggerModule.cs:line 11 中的 Nancy.Swagger.Modules.SwaggerModule.c__DisplayClass0_0.<.ctor>b__0(Object _) 在 CallSite.Target(闭包,CallSite,Func`2,对象) 在 Nancy.Routing.Route.c__DisplayClass4.b__3(对象参数,CancellationToken 上下文) --- 内部异常堆栈跟踪结束 --- 在 Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)

【问题讨论】:

    标签: asp.net swagger swagger-ui nancy


    【解决方案1】:

    您的顺序错误 - 名称应该是第一个,然后是路径

    Get["Home", "/"] = parameters =>  //this is right
    {
        try
        {
            return "home";// View["view/index.html"];
        }
        catch (Exception ex)
        {
            return ExceptionHelper.ExceptionResponse(Negotiate, ex);
        }
    };
    
    Get["Test", "/test/"] = parameters => {  //and this is right
        return "testie";
    };
    

    【讨论】:

    • 谢谢。希望你一个月前在这里 LOL
    【解决方案2】:

    好吧,不管怎样,事实证明你需要这样做:

    public class General : NancyModule
    {
        public General() : base("/v1/general/")//this part
        {
            ...
        {
    }
    

    即使示例 nancy.swagger 项目实际上并没有这样做,并且由于某种原因仍然可以运行。不管怎样,继承 base(path) 帮我解决了。

    【讨论】:

      猜你喜欢
      • 2016-10-30
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 2022-10-14
      • 2018-12-18
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      相关资源
      最近更新 更多