Sha256: 6e04d9359b60203c6fb3633a6884508e52fb36e3c62f71bcc028f9d09341cec5

Contents?: true

Size: 927 Bytes

Versions: 1

Compression:

Stored size: 927 Bytes

Contents

require "openapi3_parser"

class SchemaSerializer::Definition
  module FromOpenapi3Parser
    class << self
      def build(document)
        SchemaSerializer::Definition.new(schemas_to_hash(document.components.schemas))
      end

      private

        def schemas_to_hash(schemas)
          schemas.each_with_object({}) {|(name, node), obj|
            obj[name.to_s] = node_to_hash(node)
          }
        end

        def node_to_hash(node)
          obj = { "type" => node.type, "nullable" => node.nullable? }
          case obj["type"]
          when "array"
            obj["items"] = node_to_hash(node.items)
          when nil
            obj["required"] = node.required.node_data
            obj["properties"] = node.properties.each_with_object({}) {|(name, property), properties|
              properties[name.to_s] = node_to_hash(property)
            }
          end
          obj
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
schema_serializer-0.0.7 lib/schema_serializer/definition/from_openapi3_parser.rb