Sha256: 539f0c0eceff6445e9a5e90ef5a4e403fafc042d2423bc58ffddd0b9a4be7ed2

Contents?: true

Size: 1.1 KB

Versions: 7

Compression:

Stored size: 1.1 KB

Contents

class << RSpec::OpenAPI::SchemaMerger = Object.new
  # @param [Hash] base
  # @param [Hash] spec
  def reverse_merge!(base, spec)
    spec = normalize_keys(spec)
    deep_reverse_merge!(base, spec)
  end

  private

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

  # Not doing `base.replace(deep_merge(base, spec))` to preserve key orders
  # TODO: Perform more intelligent merges like rerouting edits / merging types
  # Should we probably force-merge `summary` regardless of manual modifications?
  def deep_reverse_merge!(base, spec)
    spec.each do |key, value|
      if base[key].is_a?(Hash) && value.is_a?(Hash)
        deep_reverse_merge!(base[key], value)
      elsif !base.key?(key)
        base[key] = value
      elsif base[key].is_a?(Array) && value.is_a?(Array)
        if key == "parameters"
          # merge arrays
          base[key] |= value
        end
      else
        # no-op
      end
    end
    base
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rspec-openapi-0.4.3 lib/rspec/openapi/schema_merger.rb
rspec-openapi-0.4.2 lib/rspec/openapi/schema_merger.rb
rspec-openapi-0.4.1 lib/rspec/openapi/schema_merger.rb
rspec-openapi-0.4.0 lib/rspec/openapi/schema_merger.rb
rspec-openapi-0.3.20 lib/rspec/openapi/schema_merger.rb
rspec-openapi-0.3.19 lib/rspec/openapi/schema_merger.rb
rspec-openapi-0.3.18 lib/rspec/openapi/schema_merger.rb