Sha256: 4c2ac1b6b1c43c7823515e3842df903f2f90fa29457511490b88cfb545ebb4bb

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'modern/descriptor/converters/output/base'

require 'json'
# TODO: is there an alternative implementation of #as_json with less junk?
require 'active_support/concern'
require 'active_model/serialization'
require 'active_model/serializers/json'

# it gets confused by the blank line after the retval reassignment.
# rubocop:disable Layout/EmptyLinesAroundArguments

module Modern
  module Descriptor
    module Converters
      module Output
        JSON = Base.new(
          media_type: "application/json",
          converter: proc do |_type, retval, _output_converter|
            retval =
              if retval.is_a?(Hash)
                retval.compact
              elsif retval.is_a?(Dry::Struct)
                retval.to_h.compact
              else
                retval
              end

            if retval.respond_to?(:as_json)
              ::JSON.generate(retval.as_json)
            else
              ::JSON.generate(retval)
            end
          end
        )

        # We use this because we pre-bake the OpenAPI3 spec JSON and
        # want the content type. However, our route invokes
        # {Modern::Response#bypass!}, so this will never be called.
        JSONBypass = Base.new(
          media_type: "application/json",
          converter: proc { raise "this should never be called!" }
        )
      end
    end
  end
end

# rubocop:enable Layout/EmptyLinesAroundArguments

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
modern-0.5.0 lib/modern/descriptor/converters/output/json.rb
modern-0.4.6 lib/modern/descriptor/converters/output/json.rb