Class: SpecForge::Documentation::OpenAPI::V30::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_forge/documentation/openapi/v3_0/schema.rb

Overview

Represents an OpenAPI 3.0 Schema object

Handles schema definitions for data types, converting internal type representations to OpenAPI-compliant schema objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Schema

Creates a new OpenAPI schema object

Parameters:

  • options (Hash) (defaults to: {})

    Schema configuration options

Options Hash (options):

  • :type (String)

    The data type to convert to OpenAPI format

  • :content (Object)

    The content/items for arrays or properties for objects



46
47
48
49
# File 'lib/spec_forge/documentation/openapi/v3_0/schema.rb', line 46

def initialize(options = {})
  @type, @format = transform_type(options[:type])
  @content = options[:content]
end

Instance Attribute Details

#contentObject? (readonly)

The schema content (for arrays/objects)

Returns:

  • (Object, nil)

    The schema content



35
36
37
# File 'lib/spec_forge/documentation/openapi/v3_0/schema.rb', line 35

def content
  @content
end

#formatString? (readonly)

The schema format (date-time, int64, etc.)

Returns:

  • (String, nil)

    The OpenAPI schema format



28
29
30
# File 'lib/spec_forge/documentation/openapi/v3_0/schema.rb', line 28

def format
  @format
end

#typeString? (readonly)

The schema type (string, integer, object, etc.)

Returns:

  • (String, nil)

    The OpenAPI schema type



21
22
23
# File 'lib/spec_forge/documentation/openapi/v3_0/schema.rb', line 21

def type
  @type
end

Instance Method Details

#to_hHash

Converts the schema to an OpenAPI-compliant hash

Returns:

  • (Hash)

    OpenAPI-formatted schema object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spec_forge/documentation/openapi/v3_0/schema.rb', line 56

def to_h
  base = {
    type:,
    format:
  }.compact_blank!

  # Add items for arrays
  if type == "array" && content.present?
    # Content is an array like [{type: "string"}], take first element as items schema
    items_type = content.first&.dig(:type) || "object"
    base[:items] = {type: items_type}
  end

  base
end