Sha256: 681eaccaeade3c77c06b4665d802a9ff9a6e3875aa325bae8712e93c3e1a192a

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

class OpenAPI::Loader::Translator
  #
  # Builds 'requestBody' from 'consumes' and 'formData' parameters
  #
  # @private
  #
  class ConvertForms < SimpleDelegator
    def call
      operations.each { |item| update_body(item) }
    end

    private

    FORMS = %w[application/x-www-form-urlencoded multipart/form-data].freeze

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

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

    def update_body(operation)
      schema = form_schema(operation)
      form_encodings(operation).each do |encoding|
        operation["requestBody"] ||= {}
        operation["requestBody"]["content"] ||= {}
        operation["requestBody"]["content"][encoding] = { "schema" => schema }
      end
    end

    def form_encodings(operation)
      encodings = Array operation.delete("consumes")
      forms, others = encodings.partition { |item| FORMS.include? item }
      operation["consumes"] = others if others.any?
      forms
    end

    def form_schema(operation)
      properties = form_properties(operation)
      schema = { "type" => "object" }
      schema["properties"] = properties if properties.any?
      schema
    end

    def form_properties(operation)
      form_params(operation).each_with_object({}) do |item, obj|
        name = item["name"]
        data = item.reject { |key| %w[in name].include? key }
        data["type"] = "string" if data["type"] == "file"
        obj[name] = data
      end
    end

    def form_params(operation)
      params = Array operation.delete("parameters")
      forms, others = params.partition { |item| item["in"] == "formData" }
      operation["parameters"] = others if others.any?
      forms
    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/translator/convert_forms.rb