Module: OpenapiRuby::Middleware::Installer

Defined in:
lib/openapi_ruby/middleware/installer.rb

Overview

Installs the runtime validation middleware onto a Rack stack. Anything responding to #use works — Rails' app.middleware and Hanami's config.middleware share that much API, so no host branching is needed.

Class Method Summary collapse

Class Method Details

.install!(stack, root: OpenapiRuby.app_root) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/openapi_ruby/middleware/installer.rb', line 11

def install!(stack, root: OpenapiRuby.app_root)
  config = OpenapiRuby.configuration

  return if ENV["OPENAPI_RUBY_GENERATING"]
  return if config.request_validation == :disabled && config.response_validation == :disabled

  config.schemas.each do |name, schema_config|
    schema_path = resolve_schema_path(config, name, root)
    next unless schema_path && File.exist?(schema_path)

    resolver = SchemaResolver.new(
      spec_path: schema_path,
      strict_reference_validation: config.strict_reference_validation
    )

    prefix = schema_config[:prefix]

    if config.request_validation != :disabled
      stack.use RequestValidation,
        schema_resolver: resolver,
        mode: config.request_validation,
        prefix: prefix
    end

    if config.response_validation != :disabled
      stack.use ResponseValidation,
        schema_resolver: resolver,
        mode: config.response_validation,
        prefix: prefix
    end
  end
end

.resolve_schema_path(config, schema_name, root) ⇒ Object



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

def resolve_schema_path(config, schema_name, root)
  ext = (config.schema_output_format == :json) ? "json" : "yaml"
  File.join(root.to_s, config.schema_output_dir, "#{schema_name}.#{ext}")
end