Module: GrapeOAS::Constants

Defined in:
lib/grape_oas/constants.rb

Overview

Central location for constants used throughout the gem

Defined Under Namespace

Modules: Defaults, HttpMethods, MimeTypes, NullableStrategy, SchemaTypes, TypePatterns

Constant Summary collapse

MAX_ENUM_RANGE_SIZE =

Maximum number of elements to expand from a non-numeric Range into an enum array. Prevents OOM on wide string ranges (e.g. “a”..“zzzzzz”).

100
RUBY_TYPE_MAPPING =

Ruby class to schema type mapping. Used for automatic type inference from parameter declarations. Note: String is not included as it’s the default fallback.

{
  Integer => SchemaTypes::INTEGER,
  Float => SchemaTypes::NUMBER,
  TrueClass => SchemaTypes::BOOLEAN,
  FalseClass => SchemaTypes::BOOLEAN,
  Array => SchemaTypes::ARRAY,
  Hash => SchemaTypes::OBJECT,
  File => SchemaTypes::FILE
}.tap do |mapping|
  mapping.default_proc = lambda do |_hash, key|
    key.is_a?(Class) && key.to_s == "BigDecimal" ? SchemaTypes::NUMBER : nil
  end
end.freeze
PRIMITIVE_TYPE_MAPPING =

String type name to schema type and format mapping (lowercase). Supports lookup with any case via primitive_type helper. Each entry contains :type and optional :format for OpenAPI schema generation.

{
  "float" => { type: SchemaTypes::NUMBER, format: "float" },
  "bigdecimal" => { type: SchemaTypes::NUMBER, format: "double" },
  "string" => { type: SchemaTypes::STRING },
  "integer" => { type: SchemaTypes::INTEGER, format: "int32" },
  "number" => { type: SchemaTypes::NUMBER, format: "double" },
  "boolean" => { type: SchemaTypes::BOOLEAN },
  "grape::api::boolean" => { type: SchemaTypes::BOOLEAN },
  "trueclass" => { type: SchemaTypes::BOOLEAN },
  "falseclass" => { type: SchemaTypes::BOOLEAN },
  "array" => { type: SchemaTypes::ARRAY },
  "hash" => { type: SchemaTypes::OBJECT },
  "object" => { type: SchemaTypes::OBJECT },
  "file" => { type: SchemaTypes::FILE },
  "rack::multipart::uploadedfile" => { type: SchemaTypes::FILE }
}.freeze

Class Method Summary collapse

Class Method Details

.format_for_type(key) ⇒ String?

Resolves the default format for a given type. Returns nil if no specific format applies (e.g., for strings, booleans).

Parameters:

  • key (String, Symbol, Class)

    The type name to resolve format for

Returns:

  • (String, nil)

    The OpenAPI format or nil if not applicable



126
127
128
129
# File 'lib/grape_oas/constants.rb', line 126

def self.format_for_type(key)
  entry = PRIMITIVE_TYPE_MAPPING[key.to_s.downcase]
  entry&.fetch(:format, nil)
end

.primitive_type(key) ⇒ String?

Resolves a primitive type name to its OpenAPI schema type. Normalizes the key to lowercase for consistent lookup.

Parameters:

  • key (String, Symbol, Class)

    The type name to resolve

Returns:

  • (String, nil)

    The OpenAPI schema type or nil if not found



116
117
118
119
# File 'lib/grape_oas/constants.rb', line 116

def self.primitive_type(key)
  entry = PRIMITIVE_TYPE_MAPPING[key.to_s.downcase]
  entry&.fetch(:type, nil)
end