Skip to content

Commit

Permalink
Extract common function
Browse files Browse the repository at this point in the history
  • Loading branch information
exoego committed Mar 28, 2024
1 parent 1100d1f commit 044ecd1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
1 change: 1 addition & 0 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'rspec/openapi/schema_merger'
require 'rspec/openapi/schema_cleaner'
require 'rspec/openapi/schema_sorter'
require 'rspec/openapi/key_transformer'

require 'rspec/openapi/minitest_hooks' if Object.const_defined?('Minitest')
require 'rspec/openapi/rspec_hooks' if ENV['OPENAPI'] && Object.const_defined?('RSpec')
Expand Down
25 changes: 25 additions & 0 deletions lib/rspec/openapi/key_transformer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class << RSpec::OpenAPI::KeyTransformer = Object.new
def symbolize(value)
case value
when Hash
value.to_h { |k, v| [k.to_sym, symbolize(v)] }
when Array
value.map { |v| symbolize(v) }
else
value
end
end

def stringify(value)
case value
when Hash
value.to_h { |k, v| [k.to_s, stringify(v)] }
when Array
value.map { |v| stringify(v) }
else
value
end
end
end
25 changes: 2 additions & 23 deletions lib/rspec/openapi/schema_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def edit(&block)
spec = read
block.call(spec)
ensure
write(stringify(spec))
write(RSpec::OpenAPI::KeyTransformer::stringify(spec))

Check notice

Code scanning / Rubocop

Do not use :: for method call. Note

Style/ColonMethodCall: Do not use :: for method calls.
end

private
Expand All @@ -24,30 +24,9 @@ def edit(&block)
def read
return {} unless File.exist?(@path)

symbolize(YAML.safe_load(File.read(@path))) # this can also parse JSON
RSpec::OpenAPI::KeyTransformer::symbolize(YAML.safe_load(File.read(@path))) # this can also parse JSON

Check notice

Code scanning / Rubocop

Do not use :: for method call. Note

Style/ColonMethodCall: Do not use :: for method calls.
end

def symbolize(value)
case value
when Hash
value.to_h { |k, v| [k.to_sym, symbolize(v)] }
when Array
value.map { |v| symbolize(v) }
else
value
end
end

def stringify(value)
case value
when Hash
value.to_h { |k, v| [k.to_s, stringify(v)] }
when Array
value.map { |v| stringify(v) }
else
value
end
end

Check notice

Code scanning / Rubocop

Don't use several empty lines in a row. Note

Layout/EmptyLines: Extra blank line detected.
# @param [Hash] spec
def write(spec)
Expand Down
17 changes: 2 additions & 15 deletions lib/rspec/openapi/schema_merger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,13 @@ class << RSpec::OpenAPI::SchemaMerger = Object.new
# @param [Hash] base
# @param [Hash] spec
def merge!(base, spec)
spec = normalize_keys(spec)
base.replace(normalize_keys(base))
spec = RSpec::OpenAPI::KeyTransformer::symbolize(spec)

Check notice

Code scanning / Rubocop

Do not use :: for method call. Note

Style/ColonMethodCall: Do not use :: for method calls.
base.replace(RSpec::OpenAPI::KeyTransformer::symbolize(base))

Check notice

Code scanning / Rubocop

Do not use :: for method call. Note

Style/ColonMethodCall: Do not use :: for method calls.
merge_schema!(base, spec)
end

private

def normalize_keys(spec)
case spec
when Hash
spec.to_h do |key, value|
[key.to_sym, normalize_keys(value)]
end
when Array
spec.map { |s| normalize_keys(s) }
else
spec
end
end

# Not doing `base.replace(deep_merge(base, spec))` to preserve key orders.
# Also this needs to be aware of OpenAPI details because a Hash-like structure
# may be an array whose Hash elements have a key name.
Expand Down

0 comments on commit 044ecd1

Please sign in to comment.