zero-rails_openapi
zero-rails_openapi copied to clipboard
Deprecation warning in Rails 6 project
I have a Rails 6 application and the initializer for open api. Here's the content:
# frozen_string_literal: true
require 'open_api'
OpenApi::Config.class_eval do
# Part 1: configs of this gem
self.file_output_path = 'public/open_api'
# Part 2: config (DSL) for generating OpenApi info
open_api :v1, base_doc_classes: [Api::V1::BaseController]
info version: '1.0.0', title: 'Petstore API'
server 'http://localhost:3000', desc: 'Local server for personal use'
# bearer_auth :Authorization
OpenApi.write_docs
end
When the application is loaded (running bin/rspec
for example) it displays the warning:
OpenApi loaded OpenApi `v1.json` has been generated.
DEPRECATION WARNING: Initialization autoloaded the constants Api, Api::V1, ApiErrorHandler, Api::V1::BaseController, Api::V1::Clients, ApplicationHelper, ApplicationController, InheritedResources::Base, Api::V1::Clients::UsersController, Api::V1::PasswordsController, Api::V1::ServiceProfessionals, Api::V1::ServiceProfessionals::UsersController, Api::V1::SessionsController, Api::V1::SocialsController, Api::V1::SpecializationsController, Api::V1::UsersController, ActionText::ContentHelper, and ActionText::TagHelper.
Being able to do this is deprecated. Autoloading during initialization is going to be an error condition in future versions of Rails.
Reloading does not reboot the application, and therefore code executed during initialization does not run again. So, if you reload Api, for example, the expected changes won't be reflected in that stale Module object.
These autoloaded constants have been unloaded.
Please, check the "Autoloading and Reloading Constants" guide for solutions. (called from <top (required)> at /app/config/environment.rb:7)
Is there a way to solve this? From my perspective, it doesn't make any problem since the specification file is generated only once, but still I'm raising it here.
Oh, I'm also using Rails 6 (6.0.3.3), but I haven't encountered this problem.
Autoloading during initialization is going to be an error condition in future versions of Rails.
Can you try to change generate specification file in the initializer (OpenApi.write_docs
) to manual generation?
I'm on Rails 6 (6.0.3.3) and I have found this problem.
autoloader = Rails.autoloaders.main
autoloader.ignore(Rails.root.join('app', 'api_doc').to_s)
Actually, allow me to correct myself. After digging into the nature of this problem, and reading this thread, I have an actual solution to that deprecation message:
require 'open_api'
require Rails.root.join('app', 'api_doc', 'api_doc').to_s
OpenApi::Config.class_eval do
self.file_output_path = 'public/api_docs'
open_api :docs, base_doc_classes: [ApiDoc]
<...>
end
autoloader = Rails.autoloaders.main
autoloader.ignore(Rails.root.join('app', 'api_doc').to_s)
So you just need to require your ApiDoc
directly, because the warning is about autoloading in base_doc_classes
. And because this code is not meant to be used outside of the generator, you don't need to care about code reloading corner-cases here.
This is needed in Rails 6.1.3 or else all descendants of the base_doc_classes are ignored.