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 =
[: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
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 15

def self.attributes(example)
   = (example.)
  request_example_mode, response_example_mode = normalize_example_mode([:example_mode], example)
  response_additional_properties, request_additional_properties = resolve_additional_properties()
  response_hybrid_additional_properties, request_hybrid_additional_properties =
    resolve_hybrid_additional_properties()
  # Enum support: response_enum and request_enum can override the general enum
  base_enum = normalize_enum([:enum])

  {
    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: request_example_mode,
    response_example_mode: response_example_mode,
    example_key: resolve_example_key(, example),
    example_name: [:example_name] || RSpec::OpenAPI.example_name_builder.call(example),
    response_enum: normalize_enum([:response_enum]) || base_enum,
    request_enum: normalize_enum([:request_enum]) || base_enum,
    response_additional_properties: response_additional_properties,
    request_additional_properties: request_additional_properties,
    response_hybrid_additional_properties: response_hybrid_additional_properties,
    request_hybrid_additional_properties: request_hybrid_additional_properties,
  }
end

.coerce_example_mode_value(value, example) ⇒ Object

Raises:

  • (ArgumentError)


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

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



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 87

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



151
152
153
154
155
156
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 151

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



83
84
85
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 83

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

.normalize_additional_properties(hash) ⇒ Object



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

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



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

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.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rspec/openapi/extractors/shared_extractor.rb', line 105

def self.normalize_example_mode(value, example = nil)
  return [: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
      [: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



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

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



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

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_example_key(metadata, example) ⇒ Object



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

def self.resolve_example_key(, 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?
  example_key
end

.resolve_hybrid_additional_properties(metadata) ⇒ Object



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

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



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

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