Class: SharedExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/openapi/extractors/shared_extractor.rb

Overview

Shared extractor for extracting OpenAPI metadata from RSpec examples

Constant Summary collapse

VALID_EXAMPLE_MODES =
%i[none single multiple].freeze
EXAMPLE_MODE_MULTIPLE_SHORTHAND_WARNING =
<<~MSG.tr("\n", ' ').strip.freeze
  [rspec-openapi] DEPRECATION: example_mode: :multiple currently means
  { request: :single, response: :multiple }. A future major version will
  change it to { request: :multiple, response: :multiple } (both sides
  multi). Specify the hash form explicitly to lock in current behavior or
  opt in early.
MSG

Class Method Summary collapse

Class Method Details

.attributes(example) ⇒ Object



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
43
44
45
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 15

def self.attributes(example)
   = (example.)
  summary = [:summary] || RSpec::OpenAPI.summary_builder.call(example)
  tags = [:tags] || RSpec::OpenAPI.tags_builder.call(example)
  formats = [:formats] || RSpec::OpenAPI.formats_builder.curry.call(example)
  operation_id = [:operation_id]
  required_request_params = [:required_request_params] || []
  security = [:security]
  description = [:description] || RSpec::OpenAPI.description_builder.call(example)
  deprecated = [:deprecated]
  request_example_mode, response_example_mode = normalize_example_mode([:example_mode], example)
  example_name = [:example_name] || RSpec::OpenAPI.example_name_builder.call(example)
  raw_example_key = [:example_key] || example_name
  example_key = RSpec::OpenAPI::ExampleKey.normalize(raw_example_key)
  example_key = 'default' if example_key.nil? || example_key.empty?

  # Enum support: response_enum and request_enum can override the general enum
  base_enum = normalize_enum([:enum])
  response_enum = normalize_enum([:response_enum]) || base_enum
  request_enum = normalize_enum([:request_enum]) || base_enum

  response_additional_properties, request_additional_properties = resolve_additional_properties()
  response_hybrid_additional_properties, request_hybrid_additional_properties =
    resolve_hybrid_additional_properties()

  [summary, tags, formats, operation_id, required_request_params, security, description, deprecated,
   request_example_mode, response_example_mode,
   example_key, example_name, response_enum, request_enum, response_additional_properties,
   request_additional_properties, response_hybrid_additional_properties,
   request_hybrid_additional_properties,]
end

.coerce_example_mode_value(value, example) ⇒ Object

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 120

def self.coerce_example_mode_value(value, example)
  raise ArgumentError, example_mode_error(value, example) unless value.is_a?(String) || value.is_a?(Symbol)

  mode = value.to_s.strip.downcase.to_sym
  return mode if VALID_EXAMPLE_MODES.include?(mode)

  raise ArgumentError, example_mode_error(value, example)
end

.collect_openapi_metadata(metadata) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 80

def self.()
  [].tap do |result|
    current = 

    while current
      [current[:example_group], current].each do |meta|
        result.unshift(meta[:openapi]) if meta&.dig(:openapi)
      end

      current = current[:parent_example_group]
    end
  end
end

.example_mode_error(value, example) ⇒ Object



144
145
146
147
148
149
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 144

def self.example_mode_error(value, example)
  context = example&.full_description
  context = " (example: #{context})" if context
  "example_mode must be a Symbol/String in #{VALID_EXAMPLE_MODES.inspect} " \
    "or a Hash with :request/:response keys, got #{value.inspect}#{context}"
end

.merge_openapi_metadata(metadata) ⇒ Object



76
77
78
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 76

def self.()
  ().reduce({}, &:merge)
end

.normalize_additional_properties(hash) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 68

def self.normalize_additional_properties(hash)
  return nil if hash.nil? || hash.empty?

  hash.each_with_object({}) do |(path, schema), result|
    result[path.to_s] = RSpec::OpenAPI::KeyTransformer.symbolize(schema)
  end
end

.normalize_enum(enum_hash) ⇒ Object



61
62
63
64
65
66
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 61

def self.normalize_enum(enum_hash)
  return nil if enum_hash.nil? || enum_hash.empty?

  # Convert all keys to strings for consistent lookup
  enum_hash.transform_keys(&:to_s)
end

.normalize_example_mode(value, example = nil) ⇒ Object

Returns [request_mode, response_mode]. Accepts either a bare Symbol/String (applied to both sides, except :multiple which is treated as a backward-compat shorthand for { request: :single, response: :multiple } and emits a one-time deprecation warning) or a Hash with :request / :response keys.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 98

def self.normalize_example_mode(value, example = nil)
  return %i[single single] if value.nil?

  case value
  when Hash
    [
      normalize_example_mode_hash_value(value, :request, example),
      normalize_example_mode_hash_value(value, :response, example),
    ]
  when Symbol, String
    mode = coerce_example_mode_value(value, example)
    if mode == :multiple
      warn_example_mode_multiple_shorthand
      %i[single multiple]
    else
      [mode, mode]
    end
  else
    raise ArgumentError, example_mode_error(value, example)
  end
end

.normalize_example_mode_hash_value(hash, key, example) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 129

def self.normalize_example_mode_hash_value(hash, key, example)
  raw = hash[key]
  raw = hash[key.to_s] if raw.nil?
  return :single if raw.nil?

  coerce_example_mode_value(raw, example)
end

.resolve_additional_properties(metadata) ⇒ Object



47
48
49
50
51
52
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 47

def self.resolve_additional_properties()
  base = normalize_additional_properties([:additional_properties])
  response = normalize_additional_properties([:response_additional_properties]) || base
  request = normalize_additional_properties([:request_additional_properties]) || base
  [response, request]
end

.resolve_hybrid_additional_properties(metadata) ⇒ Object



54
55
56
57
58
59
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 54

def self.resolve_hybrid_additional_properties()
  base = normalize_additional_properties([:hybrid_additional_properties])
  response = normalize_additional_properties([:response_hybrid_additional_properties]) || base
  request = normalize_additional_properties([:request_hybrid_additional_properties]) || base
  [response, request]
end

.warn_example_mode_multiple_shorthandObject



137
138
139
140
141
142
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 137

def self.warn_example_mode_multiple_shorthand
  return if @warned_example_mode_multiple_shorthand

  @warned_example_mode_multiple_shorthand = true
  Kernel.warn(EXAMPLE_MODE_MULTIPLE_SHORTHAND_WARNING)
end