Sha256: c1e03dbe878dcc593fe4b80de5d4f39d467880ae3bccb92752677e6854cbf7ba

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require "dry/schema/extensions/json_schema/schema_compiler"

module Dry
  module Schema
    # @api private
    module OpenAPI
      # @api private
      class SchemaCompiler < JSONSchema::SchemaCompiler
        UnknownConversionError = Class.new(StandardError)

        def to_hash
          transform_json_schema_hash!(super)
        end

        private

        def transform_json_schema_hash!(hash)
          hash.delete(:$schema)
          transform_nullables!(hash)
        end

        def transform_nullables!(hash)
          deep_transform_values!(hash) do |input|
            next input unless input.respond_to?(:key?)
            next input unless input[:type].respond_to?(:each)

            types = input[:type]
            input[:nullable] = true if types.delete("null")

            if types.length == 1
              input[:type] = types.first
              input
            else
              raise UnknownConversionError, "cannot map type #{types.inspect}"
            end
          end
        end

        def deep_transform_values!(hash, &block)
          case hash
          when Hash
            hash.transform_values! { |value| deep_transform_values!(yield(value), &block) }
          when Array
            hash.map! { |e| deep_transform_values!(e, &block) }
          else
            yield(hash)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-extensions-1.0.0 lib/dry/schema/extensions/open_api/schema_compiler.rb