Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecation warning in Rails 6 project #70

Open
biske opened this issue Nov 4, 2020 · 5 comments
Open

Deprecation warning in Rails 6 project #70

biske opened this issue Nov 4, 2020 · 5 comments

Comments

@biske
Copy link

biske commented Nov 4, 2020

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.

@zhandao
Copy link
Owner

zhandao commented Nov 4, 2020

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?

@otaviogaiao
Copy link

I'm on Rails 6 (6.0.3.3) and I have found this problem.

@bopm
Copy link
Contributor

bopm commented Apr 1, 2021

autoloader = Rails.autoloaders.main
autoloader.ignore(Rails.root.join('app', 'api_doc').to_s)

@bopm
Copy link
Contributor

bopm commented May 18, 2021

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.

@alanq
Copy link

alanq commented Jun 8, 2021

This is needed in Rails 6.1.3 or else all descendants of the base_doc_classes are ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants