简体   繁体   English

Swagger`无法自动加载常量`

[英]Swagger `Unable to autoload constant`

Swagger docs are failing to build, with the error: Swagger文档无法构建,错误:

LoadError: Unable to autoload constant ThingsController, expected /path/to/my/app/controllers/api/things_controller.rb to define it

The weird thing is, the class is defined there, and the app is working: only swagger fails. 奇怪的是,类那里定义,应用程序正在工作:只有招摇失败。

things_controller.rb contains things_controller.rb包含

class Api::ThingsController < ActionController::Base
  swagger_controller :things, "Things"

  swagger_api :index do
    summary "Returns list of things"
  end
end

Any ideas why Swagger would be unable to find a functioning class in the file where it is defined? 任何想法为什么Swagger将无法在定义它的文件中找到一个正常运行的类?

By convention in rails (and this is enforced by autoloader), file paths should match namespaces 按惯例,在rails中(这是由自动加载器强制执行的),文件路径应该与命名空间匹配

Additionally, you probably have autoloader kicking in when trying to load a things_controller class (my guess is that it happens from within Act things_controller). 另外,在尝试加载thing_controller类时,你可能已经启动了自动加载器(我的猜测是它发生在Act thing_controller中)。 It looks for a things_controller.rb file in load path, find it in app/controller/ and thus load that file so it can find the ThingsController class. 它在加载路径中查找things_controller.rb文件,在app / controller /中找到它,然后加载该文件,以便它可以找到ThingsController类。

it should be in app/controller/things_controller.rb. 它应该在app / controller / things_controller.rb中。

class ThingsController < ActionController::Base
  swagger_controller :things, "Things"

  swagger_api :index do
    summary "Returns list of things"
  end
end

Refer this link ( https://coderwall.com/p/bjk3pa/documenting-rails-based-rest-api-using-swagger-ui ) 请参阅此链接( https://coderwall.com/p/bjk3pa/documenting-rails-based-rest-api-using-swagger-ui

This had a very strange conclusion: there are two ThingsController s, one in the API namespace, Api::ThingsController and one in the base namespace ThingsController . 这有一个非常奇怪的结论:有两个ThingsController ,一个在API命名空间, Api::ThingsController ,另一个在基本命名空间ThingsController The one in the base namespace was removed but was still referred to by the routes.rb , which was a bug in the codebase. 基本命名空间中的那个被删除但仍然被routes.rb ,这是代码库中的一个错误。

The error being reported was that it couldn't find Api::ThingsController in /path/to/my/app/controllers/api/things_controller.rb but instead it was failing to find ThingsController in /path/to/my/app/controllers/things_controller.rb . 报告的错误是它无法在/path/to/my/app/controllers/api/things_controller.rb找到Api::ThingsController而是/path/to/my/app/controllers/things_controller.rb找不到ThingsController /path/to/my/app/controllers/things_controller.rb

My conclusion is that there is a bug in Rails error reporting in the lazy loader. 我的结论是延迟加载器中的Rails错误报告存在错误。 It reports the wrong error. 它报告错误的错误。 If you get a nonsensical message, check that there isn't a name clash . 如果您收到无意义的消息,请检查是否存在名称冲突

I think this is unrelated to Swagger, but triggered by it because (I guess) it spiders the source tree looking for Swagger annotations. 我认为这与Swagger无关,但是由它引发,因为(我猜)它会寻找源代码树寻找Swagger注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM