Module: Committee::Drivers

Defined in:
lib/committee/drivers.rb,
lib/committee/drivers/driver.rb,
lib/committee/drivers/schema.rb,
lib/committee/drivers/open_api_2.rb,
lib/committee/drivers/open_api_3.rb,
lib/committee/drivers/hyper_schema.rb,
lib/committee/drivers/open_api_2/link.rb,
lib/committee/drivers/hyper_schema/link.rb,
lib/committee/drivers/open_api_2/driver.rb,
lib/committee/drivers/open_api_2/schema.rb,
lib/committee/drivers/open_api_3/driver.rb,
lib/committee/drivers/open_api_3/schema.rb,
lib/committee/drivers/hyper_schema/driver.rb,
lib/committee/drivers/hyper_schema/schema.rb,
lib/committee/drivers/open_api_2/schema_builder.rb,
lib/committee/drivers/open_api_2/header_schema_builder.rb,
lib/committee/drivers/open_api_2/parameter_schema_builder.rb

Defined Under Namespace

Modules: HyperSchema, OpenAPI2, OpenAPI3 Classes: Driver, Schema

Class Method Summary collapse

Class Method Details

.driver_from_name(name) ⇒ Object

Gets a driver instance from the specified name. Raises ArgumentError for an unknown driver name.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/committee/drivers.rb', line 9

def self.driver_from_name(name)
  case name
  when :hyper_schema
    Committee::Drivers::HyperSchema::Driver.new
  when :open_api_2
    Committee::Drivers::OpenAPI2::Driver.new
  when :open_api_3
    Committee::Drivers::OpenAPI3::Driver.new
  else
    raise ArgumentError, %{Committee: unknown driver "#{name}".}
  end
end

.load_from_data(hash, schema_path = nil, parser_options: {}) ⇒ Committee::Driver

load and build drive from Hash object

Parameters:

  • hash (Hash)

Returns:

  • (Committee::Driver)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/committee/drivers.rb', line 57

def self.load_from_data(hash, schema_path = nil, parser_options: {})
  if hash['openapi']&.start_with?('3.0.')
    # From the next major version, we want to ensure `{ strict_reference_validation: true }`
    # as a parser option here, but since it may break existing implementations, just warn
    # if it is not explicitly set. See: https://github.com/interagent/committee/issues/343#issuecomment-997400329
    opts = parser_options.dup

    Committee.warn_deprecated_until_6(!opts.key?(:strict_reference_validation), 'openapi_parser will default to strict reference validation ' +
    'from next version. Pass config `strict_reference_validation: true` (or false, if you must) ' +
    'to quiet this warning.')
    opts[:strict_reference_validation] ||= false
    
    openapi = OpenAPIParser.parse_with_filepath(hash, schema_path, opts)
    return Committee::Drivers::OpenAPI3::Driver.new.parse(openapi)
  end

  driver = if hash['swagger'] == '2.0'
             Committee::Drivers::OpenAPI2::Driver.new
           else
             Committee::Drivers::HyperSchema::Driver.new
           end

  # TODO: in the future, pass `opts` here and allow optionality in other drivers?
  driver.parse(hash)
end

.load_from_file(schema_path, parser_options: {}) ⇒ Committee::Driver

load and build drive from file

Parameters:

  • schema_path (String)

Returns:

  • (Committee::Driver)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/committee/drivers.rb', line 40

def self.load_from_file(schema_path, parser_options: {})
  @__load_from_file_cache ||= {}
  @__load_from_file_cache[cache_key(schema_path, parser_options)] ||= begin
    case File.extname(schema_path)
    when '.json'
      load_from_json(schema_path, parser_options: parser_options)
    when '.yaml', '.yml'
      load_from_yaml(schema_path, parser_options: parser_options)
    else
      raise "Committee only supports the following file extensions: '.json', '.yaml', '.yml'"
    end
  end
end

.load_from_json(schema_path, parser_options: {}) ⇒ Committee::Driver

load and build drive from JSON file

Parameters:

  • schema_path (String)

Returns:

  • (Committee::Driver)


25
26
27
# File 'lib/committee/drivers.rb', line 25

def self.load_from_json(schema_path, parser_options: {})
  load_from_data(JSON.parse(File.read(schema_path)), schema_path, parser_options: parser_options)
end

.load_from_yaml(schema_path, parser_options: {}) ⇒ Committee::Driver

load and build drive from YAML file

Parameters:

  • schema_path (String)

Returns:

  • (Committee::Driver)


32
33
34
35
# File 'lib/committee/drivers.rb', line 32

def self.load_from_yaml(schema_path, parser_options: {})
  data = YAML.respond_to?(:unsafe_load_file) ? YAML.unsafe_load_file(schema_path) : YAML.load_file(schema_path)
  load_from_data(data, schema_path, parser_options: parser_options)
end