Class: GrapeOAS::TypeResolvers::ArrayResolver

Inherits:
Object
  • Object
show all
Extended by:
Base
Defined in:
lib/grape_oas/type_resolvers/array_resolver.rb

Overview

Resolves array types like “[String]”, “[Integer]”, “[MyApp::Types::UUID]”.

Grape converts ‘type: [SomeClass]` to the string “[SomeClass]” for documentation. This resolver:

  1. Detects the array pattern via regex

  2. Extracts the inner type name

  3. Attempts to resolve it back to the actual class via Object.const_get

  4. If resolved, extracts rich metadata (Dry::Types format, primitive, etc.)

  5. Falls back to string-based inference if class not available

Examples:

Resolving a Dry::Type array

# Input: "[MyApp::Types::UUID]" (string from Grape)
# Resolution: Object.const_get("MyApp::Types::UUID") -> Dry::Type
# Output: Schema(type: "array", items: Schema(type: "string", format: "uuid"))

Constant Summary collapse

TYPED_ARRAY_PATTERN =
Constants::TypePatterns::TYPED_ARRAY

Class Method Summary collapse

Methods included from Base

build_schema, handles?, infer_format_from_name, primitive_to_schema_type, resolve_class

Class Method Details

.build_schema(type) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/grape_oas/type_resolvers/array_resolver.rb', line 32

def build_schema(type)
  inner_type_name = extract_inner_type(type)
  return nil unless inner_type_name

  # Try to resolve the string to an actual class
  resolved_class = resolve_class(inner_type_name)

  items_schema = if resolved_class
                   build_schema_from_class(resolved_class)
                 else
                   build_schema_from_string(inner_type_name)
                 end

  ApiModel::Schema.new(
    type: Constants::SchemaTypes::ARRAY,
    items: items_schema,
  )
end

.handles?(type) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/grape_oas/type_resolvers/array_resolver.rb', line 26

def handles?(type)
  return false unless type.is_a?(String)

  type.match?(TYPED_ARRAY_PATTERN)
end