Module: OpenapiRuby

Defined in:
lib/openapi_ruby.rb,
lib/openapi_ruby.rb,
lib/openapi_ruby/host.rb,
lib/openapi_ruby/engine.rb,
lib/openapi_ruby/errors.rb,
lib/openapi_ruby/hanami.rb,
lib/openapi_ruby/serving.rb,
lib/openapi_ruby/version.rb,
lib/openapi_ruby/rack_app.rb,
lib/openapi_ruby/rake_tasks.rb,
lib/openapi_ruby/dsl/context.rb,
lib/openapi_ruby/configuration.rb,
lib/openapi_ruby/core/document.rb,
lib/openapi_ruby/adapters/rspec.rb,
lib/openapi_ruby/components/base.rb,
lib/openapi_ruby/testing/coverage.rb,
lib/openapi_ruby/adapters/minitest.rb,
lib/openapi_ruby/components/loader.rb,
lib/openapi_ruby/core/ref_resolver.rb,
lib/openapi_ruby/testing/transport.rb,
lib/openapi_ruby/controller_helpers.rb,
lib/openapi_ruby/dsl/metadata_store.rb,
lib/openapi_ruby/testing/assertions.rb,
lib/openapi_ruby/components/registry.rb,
lib/openapi_ruby/middleware/coercion.rb,
lib/openapi_ruby/dsl/response_context.rb,
lib/openapi_ruby/middleware/installer.rb,
lib/openapi_ruby/core/document_builder.rb,
lib/openapi_ruby/dsl/operation_context.rb,
lib/openapi_ruby/generator/schema_writer.rb,
lib/openapi_ruby/middleware/path_matcher.rb,
lib/openapi_ruby/testing/request_builder.rb,
lib/openapi_ruby/middleware/error_handler.rb,
app/controllers/openapi_ruby/ui_controller.rb,
lib/openapi_ruby/testing/request_validator.rb,
lib/openapi_ruby/components/key_transformer.rb,
lib/openapi_ruby/middleware/schema_resolver.rb,
lib/openapi_ruby/testing/response_validator.rb,
lib/openapi_ruby/adapters/context_resolution.rb,
lib/openapi_ruby/generator/rake_task_support.rb,
lib/openapi_ruby/generator/autorun_suppressor.rb,
lib/openapi_ruby/middleware/request_validation.rb,
app/controllers/openapi_ruby/schemas_controller.rb,
lib/openapi_ruby/middleware/response_validation.rb,
lib/openapi_ruby/generator/test_schema_suppressor.rb,
lib/generators/openapi_ruby/install/install_generator.rb,
lib/generators/openapi_ruby/component/component_generator.rb

Overview

Rails wasn't loaded when openapi_ruby was required (e.g. the schema-generation subprocess requires openapi_ruby/rspec before any spec file boots Rails). Defer loading via autoload so the mount OpenapiRuby::Engine in routes resolves once Rails arrives.

Defined Under Namespace

Modules: Adapters, Components, ControllerHelpers, Core, DSL, Generator, Generators, Hanami, Middleware, RakeTasks, Serving, Testing Classes: AmbiguousApiPath, Configuration, ConfigurationError, DuplicateComponentError, Engine, Error, InvalidDocumentError, MultipleApiPaths, RackApp, RequestValidationError, ResponseValidationError, SchemaValidationError, SchemasController, UiController

Constant Summary collapse

VERSION =
"4.2.0"

Class Method Summary collapse

Class Method Details

.app_rootObject

Application root, used to resolve the configured (relative) schema_output_dir. Falls back to the working directory: a bare Rack app has no root of its own, and the generation subprocess loads test files without booting an app at all.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/openapi_ruby/host.rb', line 35

def app_root
  root = if defined?(::Rails) && ::Rails.respond_to?(:root) && ::Rails.root
    ::Rails.root
  elsif hanami_booted?
    ::Hanami.app.root
  else
    Dir.pwd
  end

  root.to_s
end

.configurationObject



20
21
22
# File 'lib/openapi_ruby.rb', line 20

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



24
25
26
# File 'lib/openapi_ruby.rb', line 24

def configure
  yield(configuration)
end

.hanami_host?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/openapi_ruby/host.rb', line 23

def hanami_host?
  host == :hanami
end

.hostObject

Which framework the gem is running inside. Rails wins a tie: an app with both constants loaded is a Rails app that happens to have another framework's gems in the bundle. :rack covers Sinatra, Roda, and bare Rack — anything whose only shared surface is the Rack SPEC.



9
10
11
12
13
14
15
16
17
# File 'lib/openapi_ruby/host.rb', line 9

def host
  if defined?(::Rails)
    :rails
  elsif defined?(::Hanami)
    :hanami
  else
    :rack
  end
end

.rack_host?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/openapi_ruby/host.rb', line 27

def rack_host?
  host == :rack
end

.rails_host?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/openapi_ruby/host.rb', line 19

def rails_host?
  host == :rails
end

.reset_configuration!Object



28
29
30
# File 'lib/openapi_ruby.rb', line 28

def reset_configuration!
  @configuration = Configuration.new
end

.schema_generating?Boolean

True when the current process was started by openapi_ruby:generate (the rake task sets OPENAPI_RUBY_GENERATING=true in the subprocess).

Useful in consumer test helpers to guard test-framework requires that conflict when both rspec/rails and rails/test_help load in the same process (the FRAMEWORK=hybrid case):

# test/test_helper.rb
unless OpenapiRuby.schema_generating?
require "rails/test_help"
# ...other test-time setup...
end

Returns:

  • (Boolean)


44
45
46
# File 'lib/openapi_ruby.rb', line 44

def schema_generating?
  ENV["OPENAPI_RUBY_GENERATING"] == "true"
end