Sha256: dbf86cd66b42d78304ea43e32a262d59163860ab723f67dd5ff4047731a4e5c4

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

class OpenAPI::Loader::Denormalizer
  #
  # Denormalizes all the 'parameters' definitions
  # by moving them from the root OpenAPI object and path objects
  # right into the corresponding operation objects.
  #
  # @private
  #
  class Parameters < SimpleDelegator
    def call
      root_params = extract_from(self)
      paths.each do |path|
        path_params = extract_from(path, root_params)
        operations(path).each do |operation|
          parameters = extract_from(operation, path_params)
          operation["parameters"] = parameters if parameters.any?
        end
      end
    end

    private

    def paths
      Enumerator.new do |yielder|
        fetch("paths", {}).each_value do |path|
          yielder << path if path.is_a? Hash
        end
      end
    end

    def operations(path)
      Enumerator.new do |yielder|
        path.each_value { |item| yielder << item if item.is_a? Hash }
      end
    end

    def extract_from(data, default = [])
      custom = Array(data.delete("parameters")).select do |item|
        item.is_a?(Hash) && item["name"]
      end

      custom_names = custom.map { |item| item["name"] }
      default.reject { |item| custom_names.include? item["name"] } + custom
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
open_api-loader-0.0.1 lib/open_api/loader/denormalizer/parameters.rb